-1

所以我正在编写一个收银机程序。我已经完成了大部分工作。我的程序快完成了。但是我有一个问题。如果用户想去外卖,这个程序要求用户选择产品并计算最后的总金额。但是当用户必须输入现金支付时,我遇到了一个问题。如果用户提供的现金超过用户选择购买的产品总金额,它会计算并退回用户找零,但如果现金用户给定的数量少于用户选择的产品总量,它应该显示“给定的数量不足。添加更多”。但它以负数形式显示整数值。它以负数形式显示变化。我有给出了底部的输出示例。请帮助我。它太令人沮丧了。'<' 运算符似乎不起作用。!=,==,> 工作正常。谢谢!

#include <stdio.h>
#include<conio.h>
#include<stdlib.h>

int main()
{
int a=8,b=10,c=20,d=50,e=30,x,subtotal1=0,subtotal2=0,subtotal3=0,subtotal4=0,subtotal5=0,shopping=1,cash;
int total=subtotal1+subtotal2+subtotal3+subtotal4+subtotal5;
char choice,code,choice2;
printf("                                           WELCOME TO KFC!           \n");
printf("Here are our sample list of products you might be interested in.\n\n");
while(shopping)
{
    printf("\nChoose your product   Enter product code i.e:A,C etc\n\n");
    printf("[A]\n");
    printf("[B]\n");
    printf("[C]\n");
    printf("[D]\n");
    printf("[E]\n");
    printf("[Q] for Quitting\n\n");




    code=getch();

    if(code=='q' || code=='Q')exit(0);

    if(code=='a' || code=='A')
    {
        printf("You have chosen 'A' and price is 8$\n\n");
        printf("Enter Quantity:\n\n");
        scanf("%d",&x);
        subtotal1=a*x;
        printf("Subtotal price is %d\n\n",subtotal1);
    }
    if(code=='b' || code=='B')
    {
        printf("You have chosen 'B' and price is 10$\n");
        printf("Enter Quantity:\n");
        scanf("%d",&x);
        subtotal2=b*x;
        printf("Subtotal price is %d\n",subtotal2);
    }
    if(code=='c' || code=='C')
    {
        printf("You have chosen 'C' and price is 20$\n");
        printf("Enter Quantity:\n");
        scanf("%d",&x);
        subtotal3=c*x;
        printf("Subtotal price is %d\n",subtotal3);
    }
    if(code=='d' || code=='D')
    {
        printf("You have chosen 'D' and price is 50$\n");
        printf("Enter Quantity:\n");
        scanf("%d",&x);
        subtotal4=d*x;
        printf("Subtotal price is %d\n",subtotal4);
    }
    if(code=='E' || code=='e')
    {
        printf("You have chosen 'E' and price is 30$\n");
        printf("Enter Quantity:\n");
        scanf("%d",&x);
        subtotal5=e*x;
        printf("Subtotal price is %d\n",subtotal5);
    }

     printf("Do you want to shop more?\n");
     choice=getch();

     if(choice=='y' || choice=='Y')shopping=1;
     else if(choice=='n' || choice=='N')
     {
         total=subtotal1+subtotal2+subtotal3+subtotal4+subtotal5;
         printf("Do you want to add them to takeout?\n\n");
         choice2=getch();
         if(choice2=='y' || choice2=='Y')
         {

             printf("Enter cash:\n");
             scanf("%d",&cash);
             total=subtotal1+subtotal2+subtotal3+subtotal4+subtotal5;
             if(total < ("%d",&cash))
             {
              printf("You have given %d\n",cash);

              printf("Your change is %d\n",cash-total);
             }
            else printf("Insufficient amount given.Add more.\n");

             shopping=0;
         }
         else shopping=1;
     }

}    

}   


                                       WELCOME TO KFC!
Here are our sample list of products you might be interested in.


Choose your product   Enter product code i.e:A,C etc

[A]
[B]
[C]
[D]
[E]
[Q] for Quitting

You have chosen 'A' and price is 8$

Enter Quantity:

12
Subtotal price is 96

Do you want to shop more?
Do you want to add them to takeout?

Enter cash:
90
You have given 90
Your change is -6

但它应该显示

“给定的数量不足。添加更多”

它没有那样做。为什么?

4

1 回答 1

2

在您的代码中,

 if(total < ("%d",&cash))

是错的。它没有做任何有意义的事情。你可能想写

 if(total < cash)

FWIW,if(total < ("%d",&cash))编译,因为没有语法错误,这使用了逗号运算符的属性,它只使用 RHS 操作数作为结果。

但是,由于代码涉及关系运算符的约束违规,根据第 §6.5.8 章C11,此代码因此会调用未定义的行为。

于 2017-01-07T15:07:02.840 回答