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?
scanf_s("%d", playerNumbers);->scanf_s("%d", &playerNumbers);else { player = &j3; }.char tempo[50];andfgets(tempo, BUFFER_SIZE, stdin);, so hopefully yourBUFFER_SIZEis no more than 50. Also,tempomight not contain a newline, although it should contain a null terminator (assumingfgetsreturned something other thanNULL).