The OP is getting infinite loop when alphabetic character is entered. To avoid that you can get string input (maybe fgets
) and check if all input characters are digit and then convert it to int using atoi
etc.
Codewise you can do something like this:-
char numbuf[MAX_LEN];
int op,success=0;
while( fgets(numbuf,sizeof numbuf, stdin) ){
if(sscanf(numbuf, "%d",&op) == 1){
success=1;
break;
}else
fprintf(stderr,"%s","Error in input, give right one\n");
}
if( success )
// At this point you are sure that you have an int in `op`
success = 0;
Full code will be something like this:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100
int x;
int main()
{MENU:
printf("Welcome to our menu\n");
printf("What do you want to do ?\nSelect your option\n\n");
printf("1. Binary to decimal converter\n");
printf("2. Lorem ipsum\n");
printf("3. Lorem ipsum\n");
printf("4. About us\n");
printf("5. Quit\n\n");
printf("Input your option:");
char numbuf[MAX_LEN];
int success = 0;
while( fgets(numbuf,sizeof numbuf, stdin) ){
numbuf[strcspn(numbuf, "\n")] = 0;
if(sscanf(numbuf, "%d",&x) == 1){
success = 1;
break;
}
else
fprintf(stderr,"%s","Error in input, give right one\n");
}
if( success == 0){
exit(1);
}
success = 0;
if (x==1){
printf("This module doesn't exist yet\n\n");
goto MENU;
}
else if (x==2){
printf("This module doesn't exist yet\n\n");
goto MENU;
}
else if (x==3){
printf("This module doesn't exist yet\n\n");
goto MENU;
}
else if (x==4){
printf("About us\n\n");
printf("Team Members");
goto MENU;
}
else if (x==5){
printf("Thank you for using this program");
exit(0);
}
else{
printf("Invalid input");
goto MENU;
}
}
Still I don't understand why the infinite loop?
scanf
consumes input if it matches format string. Here it doesn't.
So scanf
stops consuming it. The non-matching input is still in the
input buffer. As a result, it is being skipped and you get the propmpt
frequently. You can also get rid of this by clearing the input buffer.
Use of goto
is not something wrong but it generates code that is hard to maintain and debugging is much more difficult. We can use series of if else
to simple switch-case
statement that is much more clear to understand. Look at this code to notice these two things.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100
int main()
{
int x;
while(1){
printf("Welcome to our menu\n");
printf("What do you want to do ?\nSelect your option\n\n");
printf("1. Binary to decimal converter\n");
printf("2. Lorem ipsum\n");
printf("3. Lorem ipsum\n");
printf("4. About us\n");
printf("5. Quit\n\n");
printf("Input your option:");
char numbuf[MAX_LEN];
int success = 0;
while( fgets(numbuf,sizeof numbuf, stdin) ){
numbuf[strcspn(numbuf, "\n")] = 0;
if(sscanf(numbuf, "%d",&x) == 1){
success = 1;
break;
}
else
fprintf(stderr,"%s","Error in input, give right one\n");
}
if( success == 0){
exit(1);
}
success = 0;
switch(x){
case 1:
case 2:
case 3: printf("This module doesn't exist yet\n\n");
continue;
case 4: printf("About us\n\nTeam Members");
continue;
case 5: printf("Thank you for using this program");
exit(0);
default:printf("Invalid input");
}
}
return 0;
}
Jokes apart:
