c - C 语言中的循环切换菜单

标签 c loops switch-statement

我改进了 switch 语句,并且一切正常,除非我想选择另一个选项,它只是重复打印语句,而不是我选择另一个类别。所以基本上它只循环一次。之前我使用 if 语句来查看该人是否想进入另一个类别,但现在我添加了退出功能,以便用户可以停止输入。

while (1)
{
printf("Please enter the corresponding number to the the category on the list\n");
printf("===========\n");
printf("1.Beverage\n2.Pastry\n3.Canned Goods\n4.Dairy\n5.Baking Goods\n6.Frozen Goods\n7.Meat\n8.Produce\n9.Cleaners\n10.Paper Goods\n11.Personal Care\n12.Other\n13.Save and Exit\n\n");
printf("Enter the number here: ");
scanf("%d",&Num);
printf("\n\n");

 switch(Num){

case 1:
for(a=0;Stop!=0;a++){
printf("Please Enter Item Name\n");
scanf("%s",Items[a].Beverage);
printf("Please Enter The Amount of Each Item\n");
scanf("%d",&ItemsNum[a].BeverageNum);
printf("Would you like to enter another item if yes type 1  if not type 0\n");
scanf("%d",&Stop);}
break;

case 2:
for(b=0;Stop!=0;b++){
printf("Please Enter Item Name\n");
scanf("%s",Items[b].Pastry);
printf("Please Enter The Amount of Each Item\n");
scanf("%d",&ItemsNum[b].PastryNum);
printf("Would you like to enter another item if yes type 1  if not type 0\n");
scanf("%d",&Stop);}
break;

case 3:
for(c=0;Stop!=0;c++){
printf("Please Enter Item Name\n");
scanf("%s",Items[c].CannedGoods);
printf("Please Enter The Amount of Each Item\n");
scanf("%d",&ItemsNum[c].CannedGoodsNum);
printf("Would you like to enter another item if yes type 1  if not type 0\n");
scanf("%d",&Stop);}
break;

case 4:
for(d=0;Stop!=0;d++){
printf("Please Enter Item Name\n");
scanf("%s",Items[d].Dairy);
printf("Please Enter The Amount of Each Item\n");
scanf("%d",&ItemsNum[d].DairyNum);
printf("Would you like to enter another item if yes type 1  if not type 0\n");
scanf("%d",&Stop);}
break;

case 5:
for(e=0;Stop!=0;e++){
printf("Please Enter Item Name\n");
scanf("%s",Items[e].BakingGoods);
printf("Please Enter The Amount of Each Item\n");
scanf("%d",&ItemsNum[e].BakingGoodsNum);
printf("Would you like to enter another item if yes type 1  if not type 0\n");
scanf("%d",&Stop);}
break;

case 6:
for(f=0;Stop!=0;f++){
printf("Please Enter Item Name\n");
scanf("%s",Items[f].FrozenGoods);
printf("Please Enter The Amount of Each Item\n");
scanf("%d",&ItemsNum[f].FrozenGoodsNum);
printf("Would you like to enter another item if yes type 1  if not type 0\n");
scanf("%d",&Stop);}
break;

case 7:
for(g=0;Stop!=0;g++){
printf("Please Enter Item Name\n");
scanf("%s",Items[g].Meat);
printf("Please Enter The Amount of Each Item\n");
scanf("%d",&ItemsNum[g].MeatNum);
printf("Would you like to enter another item if yes type 1  if not type 0\n");
scanf("%d",&Stop);}
break;

case 8:
for(h=0;Stop!=0;h++){
printf("Please Enter Item Name\n");
scanf("%s",Items[h].Produce);
printf("Please Enter The Amount of Each Item\n");
scanf("%d",&ItemsNum[h].ProduceNum);
printf("Would you like to enter another item if yes type 1  if not type 0\n");
scanf("%d",&Stop);}
break;

case 9:
for(i=0;Stop!=0;i++){
printf("Please Enter Item Name\n");
scanf("%s",Items[i].Cleaners);
printf("Please Enter The Amount of Each Item\n");
scanf("%d",&ItemsNum[i].CleanersNum);
printf("Would you like to enter another item if yes type 1  if not type 0\n");
scanf("%d",&Stop);}
break;

case 10:
for(j=0;Stop!=0;j++){
printf("Please Enter Item Name\n");
scanf("%s",Items[j].PaperGoods);
printf("Please Enter The Amount of Each Item\n");
scanf("%d",&ItemsNum[j].PaperGoodsNum);
printf("Would you like to enter another item if yes type 1  if not type 0\n");
scanf("%d",&Stop);}
break;

case 11:
for(k=0;Stop!=0;k++){
printf("Please Enter Item Name\n");
scanf("%s",Items[k].PersonalCare);
printf("Please Enter The Amount of Each Item\n");
scanf("%d",&ItemsNum[k].PersonalCareNum);
printf("Would you like to enter another item if yes type 1  if not type 0\n");
scanf("%d",&Stop);}
break;

case 12:
for(l=0;Stop!=0;l++){
printf("Please Enter Item Name\n");
scanf("%s",Items[l].Other);
printf("Please Enter The Amount of Each Item\n");
scanf("%d",&ItemsNum[l].OtherNum);
printf("Would you like to enter another item if yes type 1  if not type 0\n");
scanf("%d",&Stop);}
break;


case 13:
  printf("\n\n\t\t\tThanks for your time!\n\n\n");
                exit(0);

        }

最佳答案

您需要将break语句从for循环中删除并放入switch中。您正在破坏 forloop 而不是 switch。每个案例的 for 循环只会执行一次,然后由于您放置中断的位置而移动到下一个案例。 更改语句

case NUM:
for(a=0;Stop!=0;a++){
//doing prompts
break;
}

case NUM:
for(a=0;Stop!=0;a++){
//doing prompts
}break; //break stops the switch preventing the default from being executed incorrectly

您也可以在 do while 循环末尾说 while(Hault);

关于c - C 语言中的循环切换菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53716078/

相关文章:

c - 用C语言在用户指定的列中打印一维数组

崩溃的 C 程序

java - Java 中嵌套 Switch 语句的替代方案

oop - switch 语句不好?

java - 在 switch 语句中使用 ctrl-z 撤消的未弃用方法

c - 在 posix 上限制进程的脚本

c - 我无法使用 Wemos D1 mini 控制继电器

python - 如何排除负数?

c - 这个for循环是如何工作的?

c++ - 重置类(class)成员