•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 392,076 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,034 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 2563 | Replies: 18
![]() |
•
•
Join Date: Sep 2006
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
Hi all!!! I have been tearing my hair out trying to figure out how to make the below program calculate and display the total sales tax for each location. If anyone can please look at my code and send me in the right direction, I am hoping I can get this figured out. Thanks in advance!!
#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 = 0;
float TotalAmt = 0;
float TotalSale = 0;
const float Sale = 0;
short DelMarSold = 0;
short EncinitasSold = 0;
short LaJollaSold = 0;
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);
while (iResponse = 0) {
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));
}
getch();
return 0;
}
Wow! A first time poster that used code tags!!!! Thank you !
== maybe?
And the bottom of your code should be indented, too, for proper formatting.
while (iResponse = 0) { sets iReponse to 0, which by definition is FALSE and the loop does not execute. == maybe?
getch() is not standard and should be avoided. Use getchar() instead.And the bottom of your code should be indented, too, for proper formatting.
Age is unimportant -- except in cheese
•
•
Join Date: Sep 2006
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
No I have never heard of that function. OK, I looked up fgets() and it says it is used to read a line of data from an external source, but I am not sure what that means. Here is the code I have so far...
#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 = 0;
float TotalAmt = 0;
float TotalSale = 0;
const float Sale = 0;
short DelMarSold = 0;
short EncinitasSold = 0;
short LaJollaSold = 0;
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);
while (iResponse == 0) {
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));
}
getchar();
return 0;
} Last edited by promiscuoustx : Sep 9th, 2006 at 6:15 pm.
As I mentioned in another thread, "formatting is of primary importance. Every time you use { indent 4 spaces. Just before you use every }, unindent."
Bummer. OK, change your
•
•
•
•
No I have never heard of that function. OK, I looked up fgets() and it says it is used to read a line of data from an external source, but I am not sure what that means. Here is the code I have so far...
Bummer. OK, change your
getchar() to:while ((myChar = getchar()) != '\n'); // clean the input buffer getchar();
Age is unimportant -- except in cheese
•
•
Join Date: Sep 2006
Posts: 9
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
As I mentioned in another thread, "formatting is of primary importance. Every time you use { indent 4 spaces. Just before you use every }, unindent."
Bummer. OK, change yourgetchar()to:
while ((myChar = getchar()) != '\n'); // clean the input buffer getchar();
OK, I changed like you said, but now my formulas are not even calculating.....this was my original code....hopefully this will help....I know that it calculated the input of the user and found the sales tax and made a total.... And Sorry my code is slopppy...I will make sure I do the indents and unindents...
#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), 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));
}
getch();
return 0;
} Last edited by promiscuoustx : Sep 9th, 2006 at 7:20 pm.
If you've got to use
User Input: Strings and Numbers [C]
scanf and you're new, lord help you until you can ditch it.User Input: Strings and Numbers [C]
Last edited by Dave Sinkula : Sep 9th, 2006 at 7:28 pm.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Other Threads in the C Forum
- Previous Thread: glut
- Next Thread: Class Project



Linear Mode