0

I'm creating a AI player for a simple card game.

There will be an input by scanf of the number of legal cards. Then I want to create an array of that size.

However I have added an if statement to prevent errors if the user input is 0. (Because you cannot create an array point to something of size 0.)

My problem is... because I have created it in a if statement, I cannot access it outside of the if statement. This is shown by the warning "Use of undeclared identifier 'legal_cards'" when I try compile with Xcode on my Mac.

I'd love advice on how to better code this program. (I still need to use scanf to get input, but maybe there is a better way o

Is the only way to have all my code that relates to the array in the if statement? Or can I use it later (with another if statement checking to make sure (n_legal_cards > 0)?

#include <stdio.h>

int main (void) {
    int n_legal_cards;

    printf("How many legal cards are there?\n");
    scanf("%d", &n_legal_cards);

    if (n_legal_cards > 0) {
        int legal_cards[n_legal_cards];
    }

    /*
    ...
    [scanning other variables in here]
    [a little bit of other code here]
    ...
    */


    if (n_legal_cards > 0) {
        int rounds_suit = legal_cards[0];
    }

    return 0;
}
3
  • If the array size is given as zero or less than zero, you don't want to continue with your program. Do you? In that case why don't you check if the size is a valid value and return or exit if it is not. Then declare array in main block itself.
    – niyasc
    Commented May 4, 2018 at 5:48
  • Instead of using variable length arrays (VLA), consider using a suitable allocated piece of dynamic memory. If you insist on using VLAs, I recommend to clarify that in your question explicitly, maybe giving a reason.
    – Yunnosch
    Commented May 4, 2018 at 5:51
  • 1
    avoid using scanf for input, use fgets then sscanf instead
    – AndersK
    Commented May 4, 2018 at 6:56

2 Answers 2

3

You could use dynamic memory allocation, which will allow you to declare an array of unknown size to be used later at runtime.

Here is an example of what you could do :

#include <stdio.h>

int main (void) {
int n_legal_cards;
int* legal_cards;

printf("How many legal cards are there?\n");
scanf("%d", &n_legal_cards);

if (n_legal_cards > 0) {

    legal_cards = malloc(n_legal_cards * sizeof(int));

}


 /*
 ...
[scanning other variables in here]
[a little bit of other code here]
...
 */


if (n_legal_cards > 0) {
int rounds_suit = legal_cards[0];
}


    return 0;
}
3
  • In the last if statement I would check if(legal_cards != NULL){...} instead.
    – NeverToLow
    Commented May 4, 2018 at 5:59
  • I didn't really read the last part of the code.. I just considered the dynamic allocation part
    – wookiekim
    Commented May 4, 2018 at 6:03
  • This is exactly what I would looking for, but could not figure out how! Thanks a heap! :) :)
    – Ben Warren
    Commented May 4, 2018 at 6:13
1

So if i get you right, 0 is an invalid user-input. So just check for that in a loop until the user enters a number > 0

//init the variable with 0
int n_legal_cards = 0;

//loop scanf until number > 0 is entered
while (true) {

    printf ("How many legal cards are there?\n");
    scanf ("%d", &n_legal_cards);

    if (n_legal_cards <= 0)
        printf ("Please enter a number > 0\n");
    else
        break;

}

//then init your array
int legal_cards[n_legal_cards];
1
  • Thanks so much for your answer! Just for clarification 0 would be a valid user input. (i.e. there could be 0 legal cards). In that case there would be no specific suit for the round.
    – Ben Warren
    Commented May 4, 2018 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.