# include & lt; Stdio. H>
#include
#include
Int main (viod)
{
Char guesture [3] [10]={" scissor ", "stone", "cloth"};
Int man and computer, the result, ret;
Srand (time (NULL));
While (1) {
Computer=rand () % 3;
Agin:
Printf (" Input your gusture (0 - scissor 1 - stone 2 - cloth) : ");
Ret=the scanf (" % d ", & amp; Man);
If (ret!=1 | | man<0 | | man> 2) {
Printf (" Invalid input, plese input agin! \n");
Goto agin.
}
Printf (" Your gusture: % s \ t Computer 's gusture: % s \ n ", guesture [man], guesture [Computer]);
Result=(man - computer + 4) % 3-1;
If (result> 0)
Printf (" You win! \n");
Else if (result==0)
Printf (" the Draw! \n");
The else
Printf (" You lost! \n");
}
return 0;
}
~
Normal output is as follows:
But when the input character is repeated back agin statement:
CodePudding user response:
Inputs such as 3.2, DE, f any non integer as above error occursCodePudding user response:
Illegal input is the scanf cycle using buffer data directly cause the next time, you can reference this articlehttps://blog.csdn.net/yong_ss/article/details/79260815? Utm_source=blogxgwz0
CodePudding user response:
# include & lt; Stdio. H>
#include
#include
//int main (viod)
Int main (void)
{
Char guesture [3] [10]={" scissor ", "stone", "cloth"};
Int man and computer, the result, ret;
Srand (time (NULL));
While (1) {
Computer=rand () % 3;
Agin:
Printf (" Input your gusture (0 - scissor 1 - stone 2 - cloth) : ");
Ret=the scanf (" % d ", & amp; Man);
If (ret!=1 | | man<0 | | man> 2) {
Printf (" Invalid input, plese input agin! \n");
While (getchar ()! )
='\ n';
Goto agin.
}
Printf (" Your gusture: % s \ t Computer 's gusture: % s \ n ", guesture [man], guesture [Computer]);
Result=(man - computer + 4) % 3-1;
If (result> 0)
Printf (" You win! \n");
Else if (result==0)
Printf (" the Draw! \n");
The else
Printf (" You lost! \n");
}
return 0;
}
For your reference ~
The reason was that the input data that does not match the % d, ret returns 0, but the content of the last input in the input buffer, you need to put the wrong input from the input buffer, otherwise, the next time the scanf automatically matching the last input error, so has been in print error input;
Solution is to use getchar () to remove;