I cannot use string or char input. If I use input like "c" then
int x,y,z;
while (1)
{
z=scanf("%d%d", &x,&y);
if(z == 0){
printf("correct!\n");
}
else{
printf("Incorrect!\n");
}
}
CodePudding user response:
Check properly against success, not 0.
On failure, consume characters until end-of line. If scanf("%c", ...)
or the like not allowed due to "I cannot use string or char input.", you are out of luck.
Exit the loop on success or end-of-file
do {
z = scanf("%d%d", &x,&y);
//if(z == 0){
if (z == 2) {
printf("correct!\n");`
} else if (z != EOF) {
printf("Incorrect!\n");
char ch;
while ( scanf("%c", &ch) == 1 && ch != '\n') {
;
}
}
} while (z != 2 && z != EOF);
CodePudding user response:
I don't know what exactly you are looking for. I also don't know exactly what this code does, but if you want to stop the infinite loop, hope this helps.
#include <stdio.h>
int main () {
int x,y,z;
while (1) {
z = scanf("%d%d", &x,&y);
if(z == 0) {
printf("correct!\n");
break;
} else {
printf("Incorrect!\n");
}
}
}