Меню переключателя циклов в C - PullRequest
0 голосов
/ 11 декабря 2018

Я улучшил оператор switch, и у меня все работает, за исключением того, что если я хочу выбрать другой вариант, он просто повторяет операторы print вместо того, чтобы я выбирал другую категорию.Так что в основном это происходит только один раз.Раньше я использовал оператор 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);

        }

1 Ответ

0 голосов
/ 11 декабря 2018

Вам необходимо удалить операторы break из циклов for в коммутатор.Вы ломаете петлю, а не выключатель.Ваши циклы 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

Также вы можете просто сказать while(Hault); в конце цикла do while.

...