【问题标题】:Looping Switch Menu in CC中的循环切换菜单
【发布时间】:2018-12-11 01:26:06
【问题描述】:

我改进了 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);

        }

【问题讨论】:

  • 我在代码中看不到任何if 语句。你能提供一个minimal reproducible example吗?
  • 在第一次迭代结束时,您正在 breaking 每个 for 循环。并且可能你的意思是case 声明而不是if
  • break 指令移出循环!
  • 将“类别”问题从switch 语句中移出。

标签: c loops switch-statement


【解决方案1】:

您需要从 for 循环中删除 break 语句并进入 switch。您正在打破 forloop 而不是开关。每个案例的 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);

【讨论】:

  • 谢谢,但我遇到了一个新问题,我的循环语句似乎不起作用,我也尝试将其更改为 while(Hault):,但不幸的是它不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
相关资源
最近更新 更多