User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Sep 2006
Posts: 9
Reputation: promiscuoustx is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
promiscuoustx promiscuoustx is offline Offline
Newbie Poster

Help Using Loops to calculate totals in C Program

  #1  
Sep 9th, 2006
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;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Using Loops to calculate totals in C Program

  #2  
Sep 9th, 2006
Wow! A first time poster that used code tags!!!! Thank you !

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
Reply With Quote  
Join Date: Sep 2006
Posts: 9
Reputation: promiscuoustx is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
promiscuoustx promiscuoustx is offline Offline
Newbie Poster

Re: Using Loops to calculate totals in C Program

  #3  
Sep 9th, 2006
Thanks for the kudos!! I fixed what you said, but now my window does not stay open and I am still at a loss trying to figure out how to calculate and display the total sale amount for each location. Any more tips???? Please???
Reply With Quote  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Using Loops to calculate totals in C Program

  #4  
Sep 9th, 2006
Don't know how you changed your program. What's it look like from the while down?
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Using Loops to calculate totals in C Program

  #5  
Sep 9th, 2006
I take it back.... you used scanf() -- therein lies your problem. It leaves your input buffer dirty. Do you know the fgets() function?
Last edited by WaltP : Sep 9th, 2006 at 6:01 pm.
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Sep 2006
Posts: 9
Reputation: promiscuoustx is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
promiscuoustx promiscuoustx is offline Offline
Newbie Poster

Re: Using Loops to calculate totals in C Program

  #6  
Sep 9th, 2006
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.
Reply With Quote  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Using Loops to calculate totals in C Program

  #7  
Sep 9th, 2006
As I mentioned in another thread, "formatting is of primary importance. Every time you use { indent 4 spaces. Just before you use every }, unindent."

Originally Posted by promiscuoustx View Post
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
Reply With Quote  
Join Date: Sep 2006
Posts: 9
Reputation: promiscuoustx is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
promiscuoustx promiscuoustx is offline Offline
Newbie Poster

Re: Using Loops to calculate totals in C Program

  #8  
Sep 9th, 2006
Originally Posted by WaltP View Post
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 getchar() 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.
Reply With Quote  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Using Loops to calculate totals in C Program

  #9  
Sep 9th, 2006
Originally Posted by promiscuoustx View Post
OK, I changed like you said, but now my formulas are not even calculating.....

Is there something about formatting you don't understand?

And I see a getch() in there....

Oh, you posted the code you originally had....

Did we just waste the past hour or two?
Last edited by WaltP : Sep 9th, 2006 at 7:27 pm.
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Apr 2004
Posts: 3,450
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Using Loops to calculate totals in C Program

  #10  
Sep 9th, 2006
If you've got to use 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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 12:14 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC