I can't seem to find where the error is in this code. When I input 'Y' it is skipping over the first if statement and calculating the else statement below it. Do I have a dangling else problem or is something else? I am a beginner so I wouldn't know it if I saw it. The code doesn't have to be pretty or perfect, it only has to work. Do I need an if else statement instead of an if? When i use an if else it tells me I have a syntax error. As you can see I am trying to calculate discounts and sales tax depending on whether or not the buyer is a teacher. How much more do I have to type to submit a question. I am new to this also so I apologize if it is not formatted correctly. If the purchase price is greater than 100 the discount is 12% if less than 100 the discount is 10%. The program is to print out a receipt with the discount, tax and total price.
#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);
}
Aucun commentaire:
Enregistrer un commentaire