I believe that I am making a silly mistake that I just can't see. I need a fresh pair of eyes to look over my code and point out what I'm doing wrong. This is what I want to happen: The user has to enter an integer (1 or 2) If anything but an integer is entered the program prints "Invalid input, exiting game" and the program terminates. Now, if an integer is entered but it is not (1 or 2) I wan't to keep prompting the user to "Enter a valid number 1 or 2." The problem I'm having is that if an integer is entered but it is not (1 or 2) I have to enter the value again before the program continues: For example here is a sample walk through:
Enter 1 for X and 2 for O: 3 <return> // user entered 3 and pressed return
3 <return>// user entered 3 again and pressed return
Enter a valid number 1 or 2 to continue // now the program is running, but it should
// have run after the first 3 was entered
Code:
char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
printf("Enter 1 for X and 2 for O: ");
scanf("%d",&move);
if(scanf("%d",&move)!=1){
printf("Invalid Input exiting game\n");
exit(1);
}
while(move!=1 && move !=2){
printf("Enter a valid number 1 or 2 to continue\n");
scanf("%d",&move);
}
if(move==1 || move== 2){
printf("\n\n");
printf("%c |%c| %c \n",board[0][0],board[0][1],board[0][2]);
printf("----------\n");
printf("%c |%c| %c \n",board[1][0],board[1][1],board[1][2]);
printf("----------\n");
printf("%c |%c| %c \n\n",board[2][0],board[2][1],board[2][2]);
}
return;
What am I doing wrong?