The code down below reads the inputs of the user and if the the input given by the user is between 1 and 9 either the playerselect number k is updated to 1 or 2. If the input is out of bounds the input is re-prompted to the user. However there is a problem where after 2 prompts the k value is updated. Instead of code resulting in Player 1 input twice it should be Player 1 input and then Player 2 input.
int main(void)
{
char playerSelect[][2] = {"1", "2"};
int k;
for(int i=1; i <= 18; i++){
printf("\nPlayer %s input:", playerSelect[k]);
scanf("%d",&input);
if ((i % 2 == 0) && (1<= input <= 9)){k = 0;}
else if ((i % 2 == 1) && (1<= input <= 9)){k = 1;}
else {printf("Input out of bounds please input again");}
}
return 0;
}
Output:
Player 1 input:10
Input out of bounds please input again
Player 1 input:2
Player 1 input:2
Player 2 input:4
Wanted Output
Player 1 input:10
Input out of bounds please input again
Player 1 input:2
Player 2 input:2
Player 1 input:4
Aucun commentaire:
Enregistrer un commentaire