使用字符串时遇到麻烦

时间:2019-10-21 14:13:04

标签: c

Q)帮助我们的食堂并制定现金计划 用户从5个有价格的选项中选择项目的柜台。 该程序一直询问直到用户点击“ q”。检查后 计算总金额并向用户显示购买的物品。

#include <stdio.h>
int main()
{
    char menu[5][15]={"Pizza","Burger","Ice Cream","Pasta","Cold Drink"};
    int price[5]={1000,500,300,100,50};
    int i,amount=0,total=0,item=0;
    printf("\t\t=========ALI FAST FOOD========");
    printf("\n\t\tPlease select any option from below\n");
    for (i=0;i<5;i++)
    {
        printf("\nAvailable items are %s press %d",menu[i],item+1);
        printf("\tPrice is %d",price[i]);
        printf("\n");
        item++;
    }
    int choice;
    int start;
    printf("Enter number to start buying food");
    do {

    scanf("\n%d ",&choice);
    if (choice==1)
        {
            static int count=0;
            amount = amount+price[0];
            printf("\nYou bought %d %s",count+1,menu[0]);
            count++;
        }
        else if (choice==2)
        {
            static int count=0;
            amount = amount+price[1];
            printf("\nYou bought %d %s",count+1,menu[1]);
            count++;
        }
        else if (choice==3)
        {
            static int count=0;
            amount = amount+price[2];
            printf("\nYou bought %d %s",count+1,menu[2]);
            count++;
        }
        else if (choice==4)
        {
            static int count=0;
            amount = amount+price[3];
            printf("\nYou bought %d %s",count+1,menu[3]);
            count++;
    }
    else if (choice==5)
        {
            int count=0;
            amount = amount+price[4];
            printf("\nYou bought %d %s",count+1,menu[4]);
            count++;
    }

    else if (choice==0 || choice>=7)
    {
        printf("\nEnter correct option please\n");

    }
}

     while (choice!=6);


    total = total + amount;
    printf("\nTotal amount to be pay is %d",total);
    printf("\nThank you for visiting our canteen have a nice day !");
}

在这种情况下,我需要在用户输入“ q”而不是“ 6”时结束程序。我无法执行此操作,请帮助我解决此问题。

1 个答案:

答案 0 :(得分:0)

  • 如“ Cid”建议的那样,请尝试将输入作为字符处理而不是 整数。
  • 此外,如“ JohnBode”所建议的,在“%c”之前使用空格,并且 不在“%c”之前和之后(我怀疑您尝试使用空白 以及“%c”之后的空格)。让我知道我是否记错了。

将其放在一起,您的代码应如下所示。...

#include <stdio.h>
int main()
{
   char menu[5][15]={"Pizza","Burger","Ice Cream","Pasta","Cold Drink"};
   int price[5]={1000,500,300,100,50};
   int i,amount=0,total=0,item=0;
   printf("\t\t=========ALI FAST FOOD========");
   printf("\n\t\tPlease select any option from below\n");
   for (i=0;i<5;i++)
   {
      printf("\nAvailable items are %s press %d",menu[i],item+1);
      printf("\tPrice is %d",price[i]);
      printf("\n");
      item++;
   }
   char choice;
   int start;
   printf("Enter number to start buying food\n");
   do {
     scanf(" %c",&choice);
     if (choice=='1')
     {
        static int count=0;
        amount = amount+price[0];
        printf("\nYou bought %d %s\n",count+1,menu[0]);
        count++;
     }
    else if (choice=='2')
    {
        static int count=0;
        amount = amount+price[1];
        printf("\nYou bought %d %s\n",count+1,menu[1]);
        count++;
    }
    else if (choice=='3')
    {
        static int count=0;
        amount = amount+price[2];
        printf("\nYou bought %d %s\n",count+1,menu[2]);
        count++;
    }
    else if (choice=='4')
    {
        static int count=0;
        amount = amount+price[3];
        printf("\nYou bought %d %s\n",count+1,menu[3]);
        count++;
    }
   else if (choice=='5')
   {
        int count=0;
        amount = amount+price[4];
        printf("\nYou bought %d %s\n",count+1,menu[4]);
        count++;
   }
   //checking for lower and upper case "q"
   else if (choice=='0' || choice >= '6' && (choice != 'q' && choice != 'Q'))
   {
    printf("\nEnter correct option please\n");

   }
} while (choice!='q' && choice!='Q');//checking for lower and upper case "q"


  total = total + amount;
  printf("\nTotal amount to be pay is %d\n",total);
  printf("\nThank you for visiting our canteen have a nice day !\n");
}