• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Modify the calculator.

Hello all. I bet you are tired of this calculator. Anyway, to my question. This part is part 1 of three. This week I have to modify the calculator to get the user to input purchase amount, then check the validity, calculate and display the total sale amount for each location. Please do not think I am after the quick answer here. I respect everyone in here for there time and knowledge. My problems are many but any guidance to help me in the right direction would be appreciated. I have asked a few classmates and my instructor for guidance but have not recieved any word from them. I kind of feel like the class pest.

Here are my questions:
Do I need to rewrite the program in a different way to get these modifications?
Do I need to use a loop function or array's or is it just preference?

I do not want to ask anymore, I think I am asking to much already. Thank you for any possible help.

#include <stdio.h>

float calculateTax(float SalesPrice, float tax);

main()
{
      int iResponse = 0;
      
      printf("\n1\tDel Mar = .0725\n");
      printf("2\tEncinitas = .075\n");
      printf("3\tLa Jolla = .0775\n");
      printf("\nPlease select your proper tax (1-3): ");
      scanf("%d", &iResponse);
      
      switch (iResponse) {
      
          case 1:
              printf("\nYour total is %.2f\n",calculateTax(125.00,.0725)+125.00);
              break;
          case 2:
              printf("Your total is %.2f\n",calculateTax(125.00,.075)+125.00);
              break;
          case 3:
              printf("Your total is %.2f\n",calculateTax(125.00,.0775)+125.00);
              break;
              }
              
            getch();
       } // end switch    
      
       float calculateTax(float SPrice, float tax) {
       return(SPrice * tax);  
       }
0
Shadow-Ninja
Asked:
Shadow-Ninja
5 Solutions
 
ozoCommented:
> Do I need to rewrite the program in a different way to get these modifications?
Something needs to be different to get these modifications.

> Do I need to use a loop function or array's or is it just preference?
You don't need to, but it would be my preference.
0
 
Shadow-NinjaAuthor Commented:
The loop function is what I am leaning to. It seems to give me more options to work with. The case statements I have in the original program, can I still use those, or will it over complicate the objective for me. I am a beginner, and I mean beginner. I have some knowledge from the books I have read and others just  being kind enough to answer my million questions. I comprehend better by asking questions over retaining the reading. If I accidentaly asked  for any answer other than a directional one please dis regard question. I want to learn, I do not want it handed to me. Thank you for your response ozo. I appreciate it.
0
 
Shadow-NinjaAuthor Commented:
The for loop function, sorry...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
efnCommented:
To expand upon ozo's brief comment:

Your program is to calculate and display the total sales amount for each location.  There are two ways you can do this:

1:

calculate Del Mar
display Del Mar
calculate Encinitas
display Encinitas
calculate La Jolla
display La Jolla

2:

for each city x of Del Mar, Encinitas, La Jolla:
    calculate x
    display x

Even though this is pseudocode, you can see that option 2, which uses a loop, is shorter and less redundant.  If you are doing something only two times, it doesn't matter much whether you use a loop.  If you are doing something a hundred or a thousand or a million times, you don't want to write code for every case, so you should use a loop.  In your case, you have three cases, and you can certainly make it work without a loop, but if you do it with a loop, you will be practicing a technique that is scalable and works just as well for a larger number of cases.
0
 
Jinesh KamdarCommented:
Ur prog. looks good & need not be rewritten completely just to achieve the modifications that u want - to get the user to input purchase amount and then check the validity. You already have the logic in place to calculate and display the sale amount for each location.

U just need to plug in the code to accept the purchase amt. from the user after accepting the choice for location, validate it (my guess is for amt > 0) and pass that amt. for calculating the sale amount.
0
 
Shadow-NinjaAuthor Commented:
I think I have the location part taken care of. When you select your choice it outputs your slection with total. I just need to know if I am close or do I need glasses. The user input is seeming more tricky to me than I for saw. Is it possible to elaborate on this further without compromising any of your ethics in academic honesty. If you can go no further I understand. I do not want to act dishonest in turning my project in. I would rather get an incomplete than to cheat. I will check back later to see. Thank you to all who have helped. If no more answers are available due to academic honesty and rstrictions I will award them in them morning.  Again, thanks for all of the advice, You Guys "ROCK"

#include <stdio.h>
float calculateTax(float SalesPrice, float tax);
main()
{
 int iResponse = 0;
 
 printf("\nPlease Purchase Amount: 125.00\n");
 printf("\n****************************************\n");
 printf("\n1\tDel Mar = .0725\n");
 printf("2\tEncinitas = .075\n");
 printf("3\tLa Jolla = .0775\n");
 printf("\n****************************************\n");
 printf("\nPlease select your proper tax (1-3): ");
 scanf("%d", &iResponse);
 printf("\nThank You\n");
 
 if (iResponse == 1)
     printf("\nYou have chosen Del Mar tax\n");
     
 if (iResponse == 2)
     printf("\nYou have chosen Encinitas tax\n");
     
 if (iResponse == 3)
     printf("\nYou have chosen La Jolla tax\n");
       
 
 switch (iResponse) {
 
     case 1:
         printf("\nYour total is %.2f\n", calculateTax(125.00, .0725)+125.00);
         break;
     case 2:
         printf("Your total is %.2f\n", calculateTax(125.00, .075)+125.00);
         break;
     case 3:
         printf("Your total is %.2f\n", calculateTax(125.00, .0775)+125);
         break;
         }
         
       getch();
  } // end switch        

float calculateTax(float SPrice, float tax) {
 return(SPrice * tax);  
}
0
 
Jinesh KamdarCommented:
U are quite close to the final solution. All u need now is to put in that one line of code to accept the purchase amount from the user into a variable, instead of displaying a prefixed amt. to the user. Then pass this variable to the function to calculate the tax, replacing it for the prefixed amt. being passed currently. Finally, add it to the value retunred from the function call to make up the total sale amt.

Hint : You have used a similar input stmt. to accept the choice of location from the user.
0
 
Shadow-NinjaAuthor Commented:
This is what I have now. It compiles but I still think there is one thingmissing and I also think I wrote more code than neccessary to achieve this goal, which I do not mind. I learned and that is the most important thing.

#include <stdio.h>  
float calculateTax(float SalesPrice, float tax);
main()
{
 int iResponse = 0; //Gives value to my integer
 float fPurchase = 125.00;//The amount of the purchase being made
 float fDelMar = .0725;//This locations tax
 float fEncinitas = .075;//This locations tax
 float fLaJolla = .0775;//This locations tax
 
 printf("\nWelcome to Kudler Fine Foods\n"); //Greeting
 printf("\nPlease select your Local Tax\n"); //Prompts for user input
 printf("\n****************************************\n"); //Outlines my menu
 printf("\n1\tDel Mar = .0725\n");/////
 printf("2\tEncinitas = .075\n");//////////// These are tax percentages for each location
 printf("3\tLa Jolla = .0775\n");////
 printf("\n****************************************\n");
 printf("\nPlease select your proper tax (1-3): "); //Ask for input
 scanf("%d", &iResponse);
 printf("\nThank You\n"); //Manners,LOL
 
 if (iResponse == 1) {      
      printf("\nEnter Amount Purchased: ");
      scanf("%f", &fPurchase);                   //Gives tax,purchase amount,and totalfor the this Response
      printf("\nYour Total: $%.2f\n" fDelMar, fPurchase);//Multiplies my purchase with the correct tax for this location
}//end if
 if (iResponse == 2) {            
      printf("\nEnter Amount Purchased: ");
      scanf("%f", &fPurchase);                   //Gives tax,purchase amount, and total for this response
      printf("\nYour Total: $%.2f\n", fEncinitas, fPurchase); //Multiplies my purchase with the correct tax for this location
}//end if

 if (iResponse == 3) {      
      printf("\nEnter Amount Purchased: ");
      scanf("%f", &fPurchase);                    //Gives tax, purchase amount, and total for this response
      printf("\nYour Total: $%.2f\n", fLaJolla, fPurchase); //multiplies my purchase with the correct tax for this location
      
}//end if   //ends the if structures
 
 
 switch (iResponse){ // Begins my switch
 
     case 1:
         printf("\nYour total is %.2f\n", calculateTax(125.00, .0725)+125.00);
         break;
     case 2:
         printf("Your total is %.2f\n", calculateTax(125.00, .075)+125.00);
         break;
     case 3:
         printf("Your total is %.2f\n", calculateTax(125.00, .0775)+125);
         break;
         }
         
       getch();
  } // end switch        

float calculateTax(float SPrice, float tax) {
 return(SPrice * tax);  
}
0
 
Jinesh KamdarCommented:
U are right ... there's more than necessary code in there, but at least u got the user input to work :)

Lets get to the toal calculaton part now. Currently, you are accepting the purchase amt. as user input and displaying the local tax amt. for each of the choices 1, 2, 3. This is actually killing the whole point of the switch-case structure. What u need to do after accepting the location choice is to validate it i.e. if it is a valid selection, then only accept the user input for the purchase amt. once. After this value is captured safely into a float variable, u need to pass this variable (say fPurchase) instead of the 125.00 prefixed values in the calculateTax function calls and add its return value to the same variable for the purchase amt. to give u the final sale amt.

I also see that u hv defined variables for each location's tax wich is a good appraoch as hard-coding such values is not recommended for easy modification and scalabiliy reasons. U can also pass these variables as the 2nd argument in each of the calculateTax function calls instead of the hard-coded values.
0
 
Shadow-NinjaAuthor Commented:
Thanks, I appreciate everyone who helped me and also by helping it be honest. I liked that you helped me but kept it to where I was able to learn it mostly by my deducing the problem. I was going to terminate my membership but since I was treated with respect and was not given the answer I am going to stay. Thank you, and have a great day. I have spread the points out appropiatly....
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now