Join 137,401 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,120 people online right now. Registration is fast and FREE... Join Now!
I am trying to perform the following with a sales tax program:
1. Modify the C program so that the user inputs the purchase amount. 2. Check the user’s input for validity. Here is my code, but it is not calculating the sales tax?? Help please!!
CODE
#include <stdlib.h> #include <stdio.h>
main() {
/*Some food items and all non-food items are subject to a statewide sales tax and local*/ /*district taxes. Due to differences in district taxes, each store uses a different tax rate*/
int iResponse = 0; int PurchaseAmount; char str_DelMar[] = "Del Mar", str_Encinitas[] = "Encinitas", str_LaJolla[] = "La Jolla"; /*The above is the three districts*/ double tax_DelMar = 7.25, tax_Encinitas = 7.5, tax_LaJolla = 7.75; /*The above is the distinct district taxes for the three districts*/ char s; char mychar;
/*This is to ensure the user of this program knows what the purpose of this program is*/ printf("\nThis is a program to calculate the taxes on articles\n"); printf("\npurchased at a price the user chooses, depending on the district.\n");
/*The user needs to choose which option they want*/ printf("\nFor the district Del Mar, please press 1.\n"); printf("\nFor the district Encinitas, please press 2.\n"); printf("\nFor the district La Jolla, please press 3.\n"); /*Added the option to exit for users*/ printf("\nTo exit, please press 0.\n"); scanf ("%d", &iResponse); /*Once the user chooses the district, the user must input the dollar amount of the purchase*/ printf("\nPlease enter the purchase amount.\n"); scanf ("\n%d", &PurchaseAmount);
if (iResponse == 1){ printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f\n ",PurchaseAmount,str_DelMar, PurchaseAmount * tax_DelMar / 100 ); } else {
if (iResponse == 2) printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f ",PurchaseAmount,str_Encinitas, PurchaseAmount * tax_Encinitas / 100 ); else
if (iResponse == 3) printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f ",PurchaseAmount,str_LaJolla, PurchaseAmount * tax_LaJolla / 100 ); } getch(); return 0; }
I am not sure why it is not calculating. Thanks for any help.
This post has been edited by Videege: 6 Sep, 2006 - 10:31 AM
Is it calculating an incorrect amount, or none at all?
It displays the following no matter what amount I choose or which district I choose.
"The tax for the purchase at price $0.00 in district <null> is $0.00"
I changed int PurchaseAmount to float PurchaseAmount, so at least the district is showing now, but I am unsure still why the purchase amount and total is not showing.
Is it calculating an incorrect amount, or none at all?
It displays the following no matter what amount I choose or which district I choose.
"The tax for the purchase at price $0.00 in district <null> is $0.00"
I changed int PurchaseAmount to float PurchaseAmount, so at least the district is showing now, but I am unsure still why the purchase amount and total is not showing.
I have been racking my brain all night on this as to why my purchase price and sales tax amount are not showing up. They are both showing as $0.00?? Does anyone have any suggestions??? Here is my code that I have...
CODE
#include <stdlib.h> #include <stdio.h>
main() {
/*Some food items and all non-food items are subject to a statewide sales tax and local*/ /*district taxes. Due to differences in district taxes, each store uses a different tax rate*/
int iResponse = 0; float PurchaseAmount; char str_DelMar[] = "Del Mar", str_Encinitas[] = "Encinitas", str_LaJolla[] = "La Jolla"; /*The above is the three districts*/ double tax_DelMar = 7.25, tax_Encinitas = 7.5, tax_LaJolla = 7.75; /*The above is the distinct district taxes for the three districts*/ char s; char mychar;
/*This is to ensure the user of this program knows what the purpose of this program is*/ printf("\nThis is a program to calculate the taxes on articles\n"); printf("\npurchased at a price the user chooses, depending on the district.\n");
/*The user needs to choose which option they want*/ printf("\nFor the district Del Mar, please press 1.\n"); printf("\nFor the district Encinitas, please press 2.\n"); printf("\nFor the district La Jolla, please press 3.\n"); /*Added the option to exit for users*/ printf("\nTo exit, please press 0.\n"); scanf ("%d", &iResponse); /*Once the user chooses the district, the user must input the dollar amount of the purchase*/ printf("\nPlease enter the purchase amount.\n"); scanf ("%d", &PurchaseAmount);
if (iResponse == 1){ printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f\n ",PurchaseAmount,str_DelMar, PurchaseAmount * tax_DelMar / 100 ); } else {
if (iResponse == 2) printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f\n ",PurchaseAmount,str_Encinitas, PurchaseAmount * tax_Encinitas / 100 ); else
if (iResponse == 3) printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f\n ",PurchaseAmount,str_LaJolla, PurchaseAmount * tax_LaJolla / 100 ); } getch(); return 0; }
The problem is that you are using the wrong format specifier in your scanf statement.
CODE
scanf ("%d", &PurchaseAmount);
Since PurchaseAmount is of type float you need to use f as a format specifier. d is for integer types.
Should be like this:
CODE
scanf ("%f", &PurchaseAmount);
Tks!! Got that fixed, now I am trying to make it to where the total is calculated...maybe you can see where I am messing up.....
CODE
#include <stdlib.h> #include <stdio.h>
int main() {
/*Some food items and all non-food items are subject to a statewide sales tax and local*/ /*district taxes. Due to differences in district taxes, each store uses a different tax rate*/
int iResponse = 0; float PurchaseAmount; float TotalAmt; char str_DelMar[] = "Del Mar", str_Encinitas[] = "Encinitas", str_LaJolla[] = "La Jolla"; /*The above is the three districts*/ double tax_DelMar = 7.25, tax_Encinitas = 7.5, tax_LaJolla = 7.75; /*The above is the distinct district taxes for the three districts*/ char s; char mychar;
/*This is to ensure the user of this program knows what the purpose of this program is*/ printf("\nThis is a program to calculate the taxes on articles\n"); printf("\npurchased at a price the user chooses, depending on the district.\n");
/*The user needs to choose which option they want*/ printf("\nFor the district Del Mar, please press 1.\n"); printf("\nFor the district Encinitas, please press 2.\n"); printf("\nFor the district La Jolla, please press 3.\n"); /*Added the option to exit for users*/ printf("\nTo exit, please press 0.\n"); scanf ("%d", &iResponse); /*Once the user chooses the district, the user must input the dollar amount of the purchase*/ printf("\nPlease enter the purchase amount.\n"); scanf ("%f", &PurchaseAmount);
if (iResponse == 1){ printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f, for a total of $%.2f\n",PurchaseAmount,str_DelMar, PurchaseAmount * tax_DelMar / 100, TotalAmt, str_DelMar, PurchaseAmount + tax_DelMar); } else {
if (iResponse == 2) printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f for a total of $%.2f\n ",PurchaseAmount,str_Encinitas, PurchaseAmount * tax_Encinitas / 100, TotalAmt,str_Encinitas, PurchaseAmount + tax_Encinitas); else
if (iResponse == 3) printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f for a total of $%.2f\n ",PurchaseAmount,str_LaJolla, PurchaseAmount * tax_LaJolla / 100, TotalAmt,str_LaJolla, PurchaseAmount + tax_LaJolla); } getch(); return 0; }
This post has been edited by promiscuoustx: 9 Sep, 2006 - 01:16 PM
You have more variables than you have format specifiers and they don't match with the correct specifier.
I think this is what you were trying for:
CODE
if (iResponse == 1){ printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f, for a total of $%.2f\n",PurchaseAmount, str_DelMar, (PurchaseAmount * tax_DelMar / 100), PurchaseAmount + (PurchaseAmount * tax_DelMar / 100)); } else {
if (iResponse == 2) printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f for a total of $%.2f\n ",PurchaseAmount, str_Encinitas, (PurchaseAmount * tax_Encinitas / 100), PurchaseAmount + (PurchaseAmount * tax_Encinitas / 100)); else
if (iResponse == 3) printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f for a total of $%.2f\n ",PurchaseAmount, str_LaJolla, (PurchaseAmount * tax_LaJolla / 100), PurchaseAmount + (PurchaseAmount * tax_LaJolla / 100)); }
You have more variables than you have format specifiers and they don't match with the correct specifier.
I think this is what you were trying for:
CODE
if (iResponse == 1){ printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f, for a total of $%.2f\n",PurchaseAmount, str_DelMar, (PurchaseAmount * tax_DelMar / 100), PurchaseAmount + (PurchaseAmount * tax_DelMar / 100)); } else {
if (iResponse == 2) printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f for a total of $%.2f\n ",PurchaseAmount, str_Encinitas, (PurchaseAmount * tax_Encinitas / 100), PurchaseAmount + (PurchaseAmount * tax_Encinitas / 100)); else
if (iResponse == 3) printf("\nThe tax for the purchase at the price of $%.2f in district %s is $ %.2f for a total of $%.2f\n ",PurchaseAmount, str_LaJolla, (PurchaseAmount * tax_LaJolla / 100), PurchaseAmount + (PurchaseAmount * tax_LaJolla / 100)); }
That was exactly the problem!!! Another quick question tho....do I have to do a loop if I want to be able to calculate all of the districts sales together?