'<' 在 if 语句中不起作用

'<' not working in if statement

所以我在写一个收银机program.I已经完成了大部分everything.My程序差不多finish.But我有一个problem.this程序要求用户选择产品并计算total sum in the end if user wishes to takeout.but 当用户必须输入现金支付时;我面临一个 problem.if 用户给的现金超过用户的产品总金额选择购买,计算和returns用户零钱,但如果用户给的现金少于用户选择的商品总数,应该显示"Insufficient amount given.Add more",但相反以负数显示整数值 form.it 以负数显示变化 form.i 在 bottom.please 帮助 me.its 中给出了一个输出示例,如此令人沮丧。 '<' 运算符似乎不起作用。!=,==,> 有效 fine.Thank 你!

#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

但应该显示

"Insufficient amount given.Add more"

它没有 that.why?

在您的代码中,

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

错了。它不会做任何有意义的事情。你可能想写

 if(total < cash)

FWIW,if(total < ("%d",&cash)) 编译,因为没有语法错误,这里使用 comma operator 的 属性 结果只使用 RHS 操作数。

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