1

I have to make a game, playable with 2 or 3 players, for that I need a structure which I named Player. I wanted to ask the user for the number of "players", and do a for loop to generate players based on the number they enter, like this:

int playerNumbers = 0;
while (playerNumbers != 2 || playerNumbers != 3)
{
        printf("How many players are you ? 2 or 3 ? : \n");
        scanf_s("%d", playerNumbers); 

/* Error here: Unhandled exception to 0x7A72F2F6 (ucrtbased.dll) in Wheel.exe: 
An invalid parameter was passed to a function that considers invalid parameters
to be an unrecoverable cause of error. */

}
  // La boucle se répète 2 ou 3 fois
for (int i = 0; i < playerNumbers; i++)
{
        Player* player;
        if (i == 0)
        {
                player = &j1;
        }
        else if (i == 1)
        {
                player = &j2;
        }
        player = &j3;

        char tempo[50];
        printf("Enter your name : ");
        fgets(tempo, BUFFER_SIZE, stdin);
        i = 0;
        while (tempo[i] != '\n') {
                i++;
        }
        player->name = malloc(i * sizeof(char));
        strncpy_s(player->name, i, tempo, i);
        player->bank = 0;
        player->score = 0;
        player->ExtraTurn = 0;
        player->win = 0;
}

Problem: Error on my scanf_s line (see the comment), and finally I have a window that opens and basically tells me that in a file "input.h" at line 1567 Expression : result_pointer != nullptr

I never had to deal with these errors, and wondered if there was a need for a double pointer?

4
  • 6
    scanf_s("%d", playerNumbers); -> scanf_s("%d", &playerNumbers); Commented Apr 23, 2020 at 13:02
  • 1
    Pretty sure you want to say else { player = &j3; }. Commented Apr 23, 2020 at 13:04
  • Use an array and you will have fewer worries. Commented Apr 23, 2020 at 13:10
  • You have char tempo[50]; and fgets(tempo, BUFFER_SIZE, stdin);, so hopefully your BUFFER_SIZE is no more than 50. Also, tempo might not contain a newline, although it should contain a null terminator (assuming fgets returned something other than NULL). Commented Apr 23, 2020 at 13:13

2 Answers 2

1

As UnholySheep pointed out, when trying to scanf an integer, you need to use the & (address-of) operator.


Your loop to test the input is wrong:

while (playerNumbers != 2 || playerNumbers != 3)

One of those two conditions is ALWAYS true, so the loop never terminates.
It should be:

while (playerNumbers != 2 && playerNumbers != 3)


Also, you really made that loop longer and more complicated than necessary. Here is a shorter, better version.

Player player[playerNumbers];
for (int i = 0; i < playerNumbers; i++)
{
    char tempo[50];
    printf("Enter your name : ");
    fgets(tempo, BUFFER_SIZE, stdin);
    player[i] = (Player){ .bank = 0, .score = 0, .ExtraTurn = 0, .win = 0, .name = _strdup(tempo) };
}
Sign up to request clarification or add additional context in comments.

1 Comment

Waw, I didn't know this way to initialize a structure! Thank you ! On the other hand, on Player player [playerNumbers], I am asked for a fixed variable, actually in my code it can be equal to 2 or 3. Should I replace it with 3 simply since it is the maximum number of player ?
0

As pointed out in the comments, the argument to the scanf_s function corresponding to a %d format field should be the address of an integer variable (in your case, playerNumbers), rather than the variable itself, so you should add a & before the variable's name in the argument list.

Also, your test condition for the 'input' loop, while (playerNumbers != 2 || playerNumbers != 3) is wrong, as the condition will always evaluate to 'true': playerNumbers cannot be both 2 and 3 at the same time, so at least one of the != tests will be true! To fix this, change the || (or) operator to && (and), so that the loop exits if either test is 'false':

while (playerNumbers != 2 && playerNumbers != 3) // Keep asking if it's not 2 AND not 3
{
    printf("How many players are you ? 2 or 3 ? : \n");
    scanf_s("%d", &playerNumbers); // Not the added "&" - to get the ADDRESS of playerNumbers
}

Further, again as pointed out in the comments, you almost certainly want to enclose the player = &j3; line in an else block:

    if (i == 0) {
        player = &j1;
    }
    else if (i == 1) {
        player = &j2;
    }
    else {
        player = &j3;
    }

otherwise, the previous assignments to player will always be overridden!

1 Comment

I changed that! Thank you very much, it is very logical now and it is mostly stupid errors ^^, unfortunately my program still does not work (see answers above)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.