我似乎无法找到此代码中的错误的位置。当我输入'Y'时,它跳过第一个if语句并计算下面的else语句。我有其他问题还是其他问题?我是初学者,所以如果我看到它,我就不会知道它。代码不一定很漂亮或完美,它只需要工作。我是否需要if else语句而不是if?当我使用如果其他它告诉我我有一个语法错误。正如你所看到的,我正在试图根据买方是不是老师来计算折扣和销售税。我还需要输入多少来提交问题。我对此也很陌生,所以如果格式不正确,我表示歉意。如果购买价格大于100,则折扣为12%,如果低于100则折扣为10%。该计划将打印出折扣,税款和总价的收据。当我输入'Y'时,为什么这个程序跳过首先传递给else?
#include <stdio.h>
#include <stdlib.h>``
#define SALES_TAX 0.05
#define REG_DISCOUNT 0.10
#define DISCOUNT 0.12
int main()
{
double price, discount,total,x,tax;
printf("Enter purchase total=>");
scanf("%lf",&total);
printf("If you are a teacher enter Y if not enter N=>");
scanf("%lf",&x);
if(x=='Y'){
if(total<100)
{
discount = total * REG_DISCOUNT;
tax = (total - discount)* SALES_TAX;
price = total - discount + tax;
}
else{
discount = total * DISCOUNT;
tax = (total - discount)* SALES_TAX;
price = total + tax - discount;
}
printf("The total price of your items is = $%.2f.\n",total);
printf("Your 12 percent discount = $%.2f.\n",discount);
printf("You're total tax is $%.2f.\n",tax);
printf("You're final purchase price is $%.2f.\n",price);
}
else {
tax = total * SALES_TAX;
price = total + tax;
printf("The total price of your items is $%.2f.\n",total);
printf("Your total tax is $%.2f.\n",tax);
printf("Your total purchase price with tax is $%.2f.\n",price);
}
return(0);
}
TL; DR;使用调试器。 – Amit
你正在比较一个双重人物,这将无法正常工作。事实上,你的编译器可能会显示一个警告:在你确切地知道你为什么忽略它们之前,不要忽略警告。 – Evert
好吧,所以它应该是scanf(“%c”,&x)? –