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;
}

:confused:

Recommended Answers

All 18 Replies

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.

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???

Don't know how you changed your program. What's it look like from the while down?

I take it back.... you used scanf() -- therein lies your problem. It leaves your input buffer dirty. Do you know the fgets() function?

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;
}

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

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();

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;
}

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?

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?

No, I was just trying to show you that my original program works. I understand the formatting you suggested and changed it in this revision. I am just wondering how to make a loop to calculate the total sale for each location.

I am just wondering how to make a loop to calculate the total sale for each location.

First, make a loop...
Post that attempt and receive any necessary corrections.

First, make a loop...
Post that attempt and receive any necessary corrections.

This is what I have...probably wrong.....

#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;
      float DelMarSold = 0;
      float EncinitasSold = 0;
      float 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));
}
while ((mychar = getchar()) != '\n');  
       getchar();
       return 0;
}

What does while (iResponse == 0) { do?

What does while (iResponse == 0) { do?

I thought this was supposed to start my loop and end it, if the user pressed 0. That was the intent....If the user presses 0, then the loop is supposed to end.....

I thought this was supposed to start my loop and end it, if the user pressed 0. That was the intent....If the user presses 0, then the loop is supposed to end.....

What loop? [edit]rephrase -- Why loop? Why where you put it?[/edit]

Write down the steps your program takes -- from top to bottom -- including inputs and variable values. Skip nothing.

What do you come up with? Post it here...

What loop? [edit]rephrase -- Why loop? Why where you put it?[/edit]

Write down the steps your program takes -- from top to bottom -- including inputs and variable values. Skip nothing.

What do you come up with? Post it here...

Ok here is what 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=9;
      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"); 

 // All the code needs to be repeated to keep giving
 // the user options.  To get into the loop, we have
    // to initialize iRepsonse to something other than 0.
 while (iResponse != 0)
 {
 
  /*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*/
  // There are three stores to be concerned about, so for each store we need to place the
  // data input code and response to users.
  if (iResponse == 1)
  {
   printf("\nPlease enter the purchase amount.\n");
   scanf  ("%f", &PurchaseAmount);
   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));
   runningTotalTax = runningTotalTax + PurchaseAmount
            
  }
  else 
  if (iResponse == 2)
  {
   printf("\nPlease enter the purchase amount.\n");
   scanf  ("%f", &PurchaseAmount);
   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("\nPlease enter the purchase amount.\n");
   scanf  ("%f", &PurchaseAmount);
   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;
}

Does this look right so far???

Ok here is what 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=9;
      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"); 
 
 // All the code needs to be repeated to keep giving
 // the user options.  To get into the loop, we have
    // to initialize iRepsonse to something other than 0.
 while (iResponse != 0)
 {
 
  /*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*/
  // There are three stores to be concerned about, so for each store we need to place the
  // data input code and response to users.
  if (iResponse == 1)
  {
   printf("\nPlease enter the purchase amount.\n");
   scanf  ("%f", &PurchaseAmount);
   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));
   runningTotalTax = runningTotalTax + PurchaseAmount
 
  }
  else 
  if (iResponse == 2)
  {
   printf("\nPlease enter the purchase amount.\n");
   scanf  ("%f", &PurchaseAmount);
   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("\nPlease enter the purchase amount.\n");
   scanf  ("%f", &PurchaseAmount);
   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;
}

Does this look right so far???

Actually I have some changes.....they are below....

#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=9;
      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;
      float runningTotalTax1;
      float runningTotalTax2;
      float runningTotalTax3;
      
            

/*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"); 

 // All the code needs to be repeated to keep giving
 // the user options.  To get into the loop, we have
    // to initialize iRepsonse to something other than 0.
 while (iResponse != 0)
 {
 
  /*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*/
  // There are three stores to be concerned about, so for each store we need to place the
  // data input code and response to users.
  if (iResponse == 1)
  {
   printf("\nPlease enter the purchase amount.\n");
   scanf  ("%f", &PurchaseAmount);
   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));
   runningTotalTax1 = runningTotalTax1 + PurchaseAmount;
            
  }
  else 
  if (iResponse == 2)
  {
   printf("\nPlease enter the purchase amount.\n");
   scanf  ("%f", &PurchaseAmount);
   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));
  runningTotalTax2 = runningTotalTax2 + PurchaseAmount;
        }
  
  else 
  if (iResponse == 3)
  {
   printf("\nPlease enter the purchase amount.\n");
   scanf  ("%f", &PurchaseAmount);
   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));
  runningTotalTax3 = runningTotalTax3 + PurchaseAmount;
        }
  getch();
 }
 return 0;
}

I think I may have this right but how do I print the running totals???

Write down the steps your program takes -- from top to bottom -- including inputs and variable values. Skip nothing.

I repeat -- write the steps -- this does not mean post code. Line by line convert your code to text, executing the statements as you go on paper.
When you have an input statement, what value on paper goes into the the variable. When you get to the while, what happens? When you get to the ifs, what happens?

This is desk checking your code -- a required step in most programs. I remember testing my code this way with little pieces of paper all around me while I sat on the floor. My boss thought I was nuts, but the program worked when I ran it...

And what is that dang getch() doing in there? I thought we got rid of that 5 hours ago! This is not helping me help you... Neither is your formatting. Indent 4 spaces after every { and unindent 4 spaces before every }. Each and every one!

commented: so helpful and patient, hats off to you Sir - ~s.o.s~ +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.