所以我正在写一个收银机程序。我已经完成了大部分工作。我的程序几乎完成了。但我有一个问题。这个程序要求用户选择产品并在用户希望时最终计算总金额当用户必须输入现金支付时;我遇到问题。如果用户提供的现金超过用户选择购买的产品总数,它会计算并返回用户的更改,但如果用户提供的现金少于用户选择的产品总量,则应显示"给定的金额不足。添加更多"。而是显示负数形式的整数值。以负面形式显示变化。我给出了底部输出的一个例子。请帮助我。如此令人讨厌。 '<'运算符似乎不起作用。!=,==,>工作正常。谢谢!
#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
但它应该显示
&#34;金额不足。添加更多&#34;
它没有那样做。为什么?
答案 0 :(得分:2)
在您的代码中,
if(total < ("%d",&cash))
错了。它没有任何有意义的。你可能想写
if(total < cash)
FWIW,if(total < ("%d",&cash))
编译,因为没有语法错误,这使用comma operator的属性,该属性仅使用RHS操作数作为结果。
但是,由于代码涉及对关系运算符的约束违反,根据章节§6.5.8,C11
,此代码因此调用未定义的行为。