gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:30:37 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com