我是新来的 而且我也很喜欢编程 我正在尝试创建一个菜单界面,在我输入内容后循环回到开头 这是我的代码
#include <stdio.h>
#include <stdlib.h>
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:");
scanf("%d",&x);
printf("\n");
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;
}
}
现在的问题是,每当我输入1到5之外的其他内容时,整个程序将不停循环。这是为什么 ?我能做些什么呢?
答案 0 :(得分:-1)
输入字母字符时,OP会变为无限循环。为了避免这种情况,您可以获取字符串输入(可能是fgets
)并检查所有输入字符是否为数字,然后使用atoi
等将其转换为int。
Codewise你可以这样做: -
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;
完整代码将是这样的: -
#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;
}
}
我仍然不明白为什么无限循环?
scanf
如果匹配格式字符串,则会消耗输入。在这里它没有。 所以scanf
停止消费它。不匹配的输入仍在 输入缓冲区。结果,它被跳过,你得到了提示 经常。您也可以通过清除输入缓冲区来消除这种情况。
使用goto
并没有错,但它会生成难以维护的代码,而且调试要困难得多。我们可以使用series of if else
简单switch-case
语句来更清楚地理解。看看这段代码,注意这两件事。
#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;
}
分开笑话:
答案 1 :(得分:-1)
你可以通过使用do while循环和切换案例来简化它。
试试这个
int main()
{
int x;
do
{
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:");
scanf("%d",&x);
printf("\n");
//Switch statement
switch(x)
{
case 1: printf("This module doesn't exist yet\n\n");
break;
case 2: printf("This module doesn't exist yet\n\n");
break;
case 3: printf("This module doesn't exist yet\n\n");
break;
case 4: printf("About us\n\n");
printf("Team Members");
break;
case 5: return 0;
default: printf("Invalid input");
}
}while(1);
}