C language doesn’t scan variable after reading that variable as string(or char) instead of integer
But the most weird is that it is entering in infinite loop
int main( ){
int answer;
printf("Hello\n");
while (answer != 3){
start();//It is declared above
scanf("%d",&answer);
switch (answer) {
case 1:{
showCenters();//Also declared
break;}
case 3:{
break;}
default:{
printf("Invalid input\nTry again\n\n");
break;}
}
}
return 0;
}
To find how C language works and fix the problem
>Solution :
You can add one more statement under the label default to avoid an infinite loop when a non-number is entered
default: {
printf( "Invalid input\nTry again\n\n" );
scanf( "%*[^\n]" );
break; }
}
If the user will try to interrupt the input you can also write
if ( scanf("%d",&answer) == EOF ) break;
to exit the while loop.
Pay attention to that the variable answer shall be initialized before the while loop.