【发布时间】:2017-05-22 05:27:11
【问题描述】:
所以我正在编写一个收银机程序。我已经完成了几乎所有事情。我的程序快完成了。但是我有一个问题。如果用户想去,这个程序要求用户选择产品并最终计算总金额外卖。但是当用户必须输入现金支付时,我遇到了一个问题。如果用户给的现金超过用户选择购买的产品总金额,它会计算并退回用户找零,但是如果用户给的现金少于用户选择的产品的总金额,它应该显示“给的金额不足。添加更多”。但它以负数形式显示整数值。它以负数形式显示变化.我在底部给出了一个输出示例。请帮助我。它太令人沮丧了。 ' 工作正常。谢谢!
#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
但它应该显示
“给的数量不足。添加更多”
它不这样做。为什么?
【问题讨论】:
-
你想多了...
total < ("%d",&cash)没意义,就做total < cash -
@JJJ 但被低估的逗号运算符的艺术使用!此外,任何理智的编译器必须已经为此行发出警告,因为它违反了约束。
-
如果你的 C 编译器给你一个警告,这意味着它在你的代码中发现了一个错误。这意味着您需要修复代码以便没有警告,并且您不会通过在任何地方拍打演员来修复它们。如果您的代码有警告,请这么说。询问如何修复警告,以及警告的含义。您的目标应该是在严格的警告选项下进行无警告编译。
标签: c if-statement operators relational-operators