Free2Code
 
Time: 2008-11-21, 09:47pm
Another Newbie Needs Help
Subject: Another Newbie Needs Help  ·  Posted: 2006-12-06, 08:43am
Rank: ? (8)
Member #: 29333
I think I might have too much, or maybe I'm missing something. I am trying to make a program that calculates the total of a purchase. The problem is that the three stores will have different tax amounts and I have to validate the total that is entered.

So here is what I need my code to do. Allow the user to pick their store. User enters in total of purchase. Program uses correct tax amount to figure total with tax.

I've found bits of what I need and then played with them myself but still not working...in fact...I can't even get it to compile anymore!

Anyway, Here is what I have. Any help would be greatly appreciated. Thank you.

Code:
  1. #include<stdio.h> 
  2.    
  3.     int main() 
  4.     { //begin function
  5.     
  6.       int iResponse; 
  7.        float fPurchaseAmount1 = 0.00;
  8.        float fPurchaseAmount2 = 0.00;
  9.        float PurchaseAmount3 = 0.00; 
  10.        char str_DelMar[] = "Del Mar", str_Encinitas[] = "Encinitas", str_LaJolla[] = "La Jolla"; 
  11.        /*The above is the three districts*/ 
  12.       double tax_DelMar = 7.25, tax_Encinitas = 7.5, tax_LaJolla = 7.75; 
  13.        /*The above is the distinct district taxes for the three districts*/ 
  14.        char s; 
  15.        char mychar; 
  16.       float runningTotalTax1 = 0.00; 
  17.       float runningTotalTax2 = 0.00; 
  18.        float runningTotalTax3 = 0.00; 
  19.        float runningTotal1 = 0.00; 
  20.         float runningTotal2 = 0.00; 
  21.         float runningTotal3 = 0.00; 
  22.        char *gets(char *str); 
  23.        const int N = 20; 
  24.        char inputarray[N]; 
  25.        //This is to ensure the user of this program knows what the purpose of this program is*/ 
  26.  printf("\nThis is a program to calculate the taxes on articles\n"); 
  27.   printf("purchased at a price the user chooses, depending on the district.\n"); 
  28.      for(; 
  29.      { 
  30.          printf("\nFor the district Del Mar, please press 1.\n"); 
  31.          printf("For the district Encinitas, please press 2.\n"); 
  32.          printf("For the district La Jolla, please press 3.\n"); 
  33.          printf("\nTo exit, please press 4.\n"); 
  34.          fgets(inputarray, N, stdin);        
  35.         iResponse = atoi(inputarray); 
  36.          
  37.          if (iResponse == 1) 
  38.           { 
  39.              printf("\nPlease enter the purchase amount.\n"); 
  40.                 fgets(inputarray, N, stdin); 
  41.                 if((PurchaseAmount1 = atof(inputarray)) == 0 || PurchaseAmount1 < 0) 
  42.                 { 
  43.                     printf("\nWrong Input\n"); 
  44.                         continue; 
  45.                 } 
  46.              runningTotalTax1 = PurchaseAmount1 * tax_DelMar/100; 
  47.              printf("\nThe tax for the purchase at the percentage rate of 7.25, and at the price of $%.2f \nin district %s is $%.2f, for a total of $%.2f\n",PurchaseAmount1,str_DelMar, (PurchaseAmount1 * tax_DelMar / 100), PurchaseAmount1 + (PurchaseAmount1 * tax_DelMar / 100)); 
  48.              runningTotal1 += runningTotalTax1 + PurchaseAmount1; 
  49.              printf("\nThe total purchases amount in district %s is $%.2f\n", str_DelMar, runningTotal1); 
  50.          continue; 
  51.        } 
  52.  //        else 
  53.          if (iResponse == 2) 
  54.          { 
  55.              printf("\nPlease enter the purchase amount.\n"); 
  56.              scanf  ("%f", &PurchaseAmount2); 
  57.              runningTotalTax2 = PurchaseAmount2 * tax_Encinitas/100; 
  58.               
  59.             printf("\nThe tax for the purchase at the percentage rate of 7.5, and at the price of $%.2f \nin district %s is $%.2f for a total of $%.2f\n ",PurchaseAmount2,str_Encinitas, (PurchaseAmount2 * tax_Encinitas / 100), PurchaseAmount2 + (PurchaseAmount2 * tax_Encinitas / 100)); 
  60.              runningTotal2 += runningTotalTax2 + PurchaseAmount2; 
  61.              printf("\nThe total purchases amount in district %s is $%.2f\n", str_Encinitas, runningTotal2); 
  62.          } 
  63.           
  64.  //        else 
  65.          if (iResponse == 3) 
  66.          { 
  67.              printf("\nPlease enter the purchase amount.\n"); 
  68.              scanf  ("%f", &PurchaseAmount3); 
  69.            runningTotalTax3 = PurchaseAmount3 * tax_LaJolla/100; 
  70.              
  71.              printf("\nThe tax for the purchase at the percentage rate of 7.75, and at the price of $%.2f \nin district %s is $%.2f for a total of $%.2f\n ",PurchaseAmount3,str_LaJolla, (PurchaseAmount3 * tax_LaJolla / 100), PurchaseAmount3 +  (PurchaseAmount3 * tax_LaJolla / 100));  
  72.              runningTotal3 += runningTotalTax3 + PurchaseAmount3; 
  73.              printf("\nThe total purchases amount in district %s is $%.2f\n", str_LaJolla, runningTotal3); 
  74.         } 
  75.          if (iResponse == 4) 
  76.              break; 
  77.          else 
  78. printf("\nWrong Input\n"); 
  79.     
  80.       } 
  81.          getchar(); 
  82.          return 0; 
  83.  }




Thanks,
Jess

» Post edited 2006-12-12, 04:16am by misterhaan.

 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-06, 08:45am
Rank: ? (8)
Member #: 29333
another question...would I be better of using switch?

 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-07, 08:42am
Rank: ? (8)
Member #: 29333
is there anybody out there??

 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-08, 03:12am
Rank: ? (4821)
Member #: 3416
switch is often a good idea when you're using if/elseif statements to check if the same variable is equal to different things. i haven't really looked through the code you've posted since i don't know what exactly you're having trouble with (like what exactly isn't working and what's it doing instead?)

my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-08, 05:06am
Rank: ? (8)
Member #: 29333
 Unico writes...
So here is what I need my code to do. Allow the user to pick their store. User enters in total of purchase. Program uses correct tax amount to figure total with tax.

I've found bits of what I need and then played with them myself but still not working...in fact...I can't even get it to compile anymore!



 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-08, 09:46am
Rank: ? (767)
Member #: 11085
Do not assume we have failed to read your posts, rather, assume what you have written fails to live up to standards.

What misterhaan and I mean are that we need something more specific than what a program is supposed to do if we are to be expected to help. Yes, we could correct every error in your code and return it to you, but you wouldn't learn much, if anything in that manner. Therefore, we would like to know what exact problems are you having. If you have no idea how to do anything in the prompt, then perhaps you need to spend more time reading through tutorials.

- relpats_eht
 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-11, 04:35am
Rank: ? (4821)
Member #: 3416
if you want help with compilation errors, you need to tell us what the compilation errors are. i don't actually use c or c++ so i can't save your code into a file and try to compile it to figure out what the errors are. i also don't want to read through the entire thing when you could tell me what the error is, what line it's referencing, and which line that is in the code you posted and save me a lot of time. if you want people to help you for nothing in return, you might try to make it as easy as possible for them to help you.

my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-11, 05:24am
Rank: ? (134)
Member #: 28863
For someone that wants help you really are cheeky, and for good measure lazy.

 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-11, 06:04am
Rank: ? (8)
Member #: 29333
I wasn't trying to be cheeky or rude and by no means am I lazy! When I go to compile it says it's compiling but it never says complete. I can't compile, I can't build, and I can't run. I've written other programs and they still compile, build and run so it's not Miracle C, it has to be the code.

To clarify: I do not get any error message or prompt...it just doesn't compile.

 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-11, 08:46am
Rank: ? (767)
Member #: 11085
What compiler are you using?
In advance, I will recommend you stop using as a response to your post in which you answer my question.

I have never heard of a compiler that never finishes compiling and gives no error, so I would suggest you simply try another.

- relpats_eht
 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-11, 12:48pm
Rank: ? (8)
Member #: 29333
I actually just started from scratch and tried some simpler tricks. Here is my new code

Code:
  1. #include <stdio.h>
  2. main()
  3. {//begin program
  4.     float delmartaxper, encintaxper, lajoltaxper;
  5.     float delmartaxamt, encintaxamt, lajoltaxamt;
  6.     float purchaseamount;
  7.     float purchasetotal;
  8.     char menuselection;
  9.         delmartaxper = 7.25;
  10.         encintaxper = 7.5;
  11.         lajoltaxper = 7.75;
  12.             printf("\nKudler Fine Foods' Calaculator\n");
  13.             printf("\n\tPlease Enter Purchase Amount:\t$");
  14.             scanf("%f", &purchaseamount);
  15.         
  16.         <b>if (purchaseamount < .01)
  17.             {//begin errorif
  18.             printf("\nInvalid Amount. Enter cost greater than 0.");
  19.             }//end errorif</b>
  20.             
  21.         if (purchaseamount > .00)
  22.     {//begin function
  23.             <b>printf("Please Select City:\n");
  24.             printf("\n\t1. Del Mar");
  25.             printf("\n\t2. Encinitas");
  26.             printf("\n\t3. La Jolla");
  27.             printf("\n\t4. Exit Program");
  28.             printf("\nPlease Enter Selection:");
  29.             scanf("%i", &menuselection);</b>
  30.         
  31.         if (menuselection == 1)
  32.             {//begin select1
  33.             delmartaxamt=delmartaxper*purchaseamount / 100;
  34.             purchasetotal=delmartaxamt+purchaseamount;
  35.             printf("\nDel Mar - 7.25%% Tax Rate\n");
  36.             printf("\n$%.2f Purchased in Del Mar Totals $%.2f With $%.2f Taxes\n", purchaseamount, purchasetotal, delmartaxamt);
  37.             }//end select1
  38.         
  39.         if (menuselection == 2)
  40.             {//begin select2
  41.             encintaxamt = encintaxper * purchaseamount / 100;
  42.             purchasetotal = encintaxamt + purchaseamount;
  43.             printf("\nEncinitas - 7.50%% Tax Rate\n");
  44.             printf("\n$%.2f Purchased in Encinitas Totals $%.2f With $%.2f Taxes\n", purchaseamount, purchasetotal, encintaxamt);
  45.             }//end select2
  46.         if (menuselection == 3)
  47.             {//begin select3
  48.             lajoltaxamt = lajoltaxper * purchaseamount / 100;
  49.             purchasetotal = lajoltaxamt + purchaseamount;
  50.             printf("\nLa Jolla - 7.75%% Tax Rate\n");
  51.             printf("\n$%.2f Purchased in La Jolla Totals $%.2f With $%.2f in Taxes\n", purchaseamount, purchasetotal, lajoltaxamt);
  52.             }//end select3
  53.         
  54.         if (menuselection == 4)
  55.             {//begin select4
  56.             printf("\nHave a Good Day!");
  57.             }//end select4
  58.         <b>if (menuselection < 1 || menuselection > 4)
  59.             {//begin invselect
  60.             printf("\nInvalid Selection - Please Choose 1 - 4\n");</b>
  61.             break;
  62.             }//end invselect
  63.             
  64.     }//end function
  65.             printf("\n\nPress any key to close the window");
  66.               getch();
  67.               return 0;
  68. }//end program


The problem I am running into now is this: when a user enters in a menu option greater than four the program says,, "Invalid Selection - Please Choose 1-4" That part is great. However, right below it says, "Invalid amount. Enter Cost Greater than 0. Please select City: and then has my list again. All I need to remove is the "Invalid amount" statement but that goes above with my purchase amount.
I tried to bold everything that is affected. If I can get rid of that statement there but still have it for purchase amount I will be good. if I reselect 1-4 the program works with the amount I entered before the error so I don't know why it is showing that. I hope I explained this all better than last time.


» Post edited 2006-12-12, 04:15am by misterhaan.

 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-11, 01:42pm
Rank: ? (767)
Member #: 11085
I'm not sure how that even compiles. Unless my eyes are deceiving me as a result of unindented code, I can't find a loop statement meaning that there is an extra bracket and the behavior described is impossible. Although, my eyes could be fooling me as I have said, maybe I just am not seeing it. Also, I think my above post still holds true. If you are using a compiler which you can consistently invoke an infinite loop in then you should probably switch to another compiler. I suggest MinGW or Microsofts compiler if you are on Windows, which I am quite sure you are, with personal preference learning far in the MinGW direction.

On a side note, written here because it is of little enough pertinence to bother anyone about if they aren't already reading what I type, in the event an administrator is reading this, could you please make me a moderator of this forum? It would be convenient to be able to put code tags around posts rather than to simply ask people to do so in the future.

- relpats_eht
 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-12, 03:28am
Rank: ? (8)
Member #: 29333
I am required to use Miracle C. I don't get a choice. This is compiling and working. I'm not getting an error. It's just when I run it that it is doing something I don't want it to do. I'll try to use these buttons above and see if that makes it easier...here goes.

Here is my code. The bolded areas are what I believe are affected or causing the problem.
Code:
  1. #include <stdio.h>
  2. main()
  3. {//begin program
  4.     float delmartaxper, encintaxper, lajoltaxper;
  5.     float delmartaxamt, encintaxamt, lajoltaxamt;
  6.     float purchaseamount;
  7.     float purchasetotal;
  8.     char menuselection;
  9.         delmartaxper = 7.25;
  10.         encintaxper = 7.5;
  11.         lajoltaxper = 7.75;
  12.             printf("\nKudler Fine Foods' Calaculator\n");
  13.             printf("\n\tPlease Enter Purchase Amount:\t$");
  14.             scanf("%f", &purchaseamount);
  15.         
  16.         <strong><em>if (purchaseamount < .01)
  17.             {//begin errorif
  18.             printf("\nInvalid Amount. Enter cost greater than 0.");
  19.             }//end errorif </strong></em>
  20.             
  21.         if (purchaseamount > .00)
  22.     {//begin function
  23.             printf("Please Select City:\n");
  24.             printf("\n\t1. Del Mar");
  25.             printf("\n\t2. Encinitas");
  26.             printf("\n\t3. La Jolla");
  27.             printf("\n\t4. Exit Program");
  28.             printf("\nPlease Enter Selection:");
  29.             scanf("%i", &menuselection);
  30.         
  31.         if (menuselection == 1)
  32.             {//begin select1
  33.             delmartaxamt=delmartaxper*purchaseamount / 100;
  34.             purchasetotal=delmartaxamt+purchaseamount;
  35.             printf("\nDel Mar - 7.25%% Tax Rate\n");
  36.             printf("\n$%.2f Purchased in Del Mar Totals $%.2f With $%.2f Taxes\n", purchaseamount, purchasetotal, delmartaxamt);
  37.             }//end select1
  38.         
  39.         if (menuselection == 2)
  40.             {//begin select2
  41.             encintaxamt = encintaxper * purchaseamount / 100;
  42.             purchasetotal = encintaxamt + purchaseamount;
  43.             printf("\nEncinitas - 7.50%% Tax Rate\n");
  44.             printf("\n$%.2f Purchased in Encinitas Totals $%.2f With $%.2f Taxes\n", purchaseamount, purchasetotal, encintaxamt);
  45.             }//end select2
  46.         if (menuselection == 3)
  47.             {//begin select3
  48.             lajoltaxamt = lajoltaxper * purchaseamount / 100;
  49.             purchasetotal = lajoltaxamt + purchaseamount;
  50.             printf("\nLa Jolla - 7.75%% Tax Rate\n");
  51.             printf("\n$%.2f Purchased in La Jolla Totals $%.2f With $%.2f in Taxes\n", purchaseamount, purchasetotal, lajoltaxamt);
  52.             }//end select3
  53.         
  54.         if (menuselection == 4)
  55.             {//begin select4
  56.             printf("\nHave a Good Day!");
  57.             }//end select4
  58.         <strong>if (menuselection < 1 || menuselection > 4)
  59.             {//begin invselect
  60.             printf("\nInvalid Selection - Please Choose 1 - 4\n");
  61.             break; 
  62.             }//end invselect</strong>
  63.             
  64.     }//end function
  65.             printf("\n\nPress any key to close the window");
  66.                       getch();
  67.                       return 0;
  68. }//end program


This is what it should do. lets say I enter in a total of 200.00. I then have to choose an option 1-4. Well I hit, 6, that is not valid and it returns "Invalid Selection - Please Choose 1-4 and displays the menu again.

What is happening is, I enter 200.00. Then I enter 6, which is invalid and the program says, "Invalid Selection - Please Choose 1-4" this part is good. Below that, however, it is also saying "Invalid Amount. Enter Cost greater than 0" and then displaying the menu. The invalid amount statement should only appear if the user enters in 0 for the total in the beginning. It should not appear when they enter an invalid option.

Is that a better explanation?



 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-12, 03:37am
Rank: ? (8)
Member #: 29333
Code:
  1. #include <stdio.h> 
  2. main() 
  3. {//begin program 
  4.     float delmartaxper, encintaxper, lajoltaxper; 
  5.     float delmartaxamt, encintaxamt, lajoltaxamt; 
  6.     float purchaseamount; 
  7.     float purchasetotal; 
  8.     char menuselection; 
  9.         delmartaxper = 7.25; 
  10.         encintaxper = 7.5; 
  11.         lajoltaxper = 7.75; 
  12.             printf("\nKudler Fine Foods' Calaculator\n"); 
  13.             printf("\n\tPlease Enter Purchase Amount:\t$"); 
  14.             scanf("%f", &purchaseamount); 
  15.          
  16.         if (purchaseamount < .01) 
  17.             {//begin errorif 
  18.             printf("\nInvalid Amount. Enter cost greater than 0.\n"); 
  19.             }//end errorif 
  20.              
  21.         if (purchaseamount > .00) 
  22.     {//begin function 
  23.             printf("Please Select City:\n"); 
  24.             printf("\n\t1. Del Mar"); 
  25.             printf("\n\t2. Encinitas"); 
  26.             printf("\n\t3. La Jolla"); 
  27.             printf("\n\t4. Exit Program"); 
  28.             printf("\nPlease Enter Selection:"); 
  29.             scanf("%i", &menuselection); 
  30.          
  31.         if (menuselection == 1) 
  32.             {//begin select1 
  33.             delmartaxamt=delmartaxper*purchaseamount / 100; 
  34.             purchasetotal=delmartaxamt+purchaseamount; 
  35.             printf("\nDel Mar - 7.25%% Tax Rate\n"); 
  36.             printf("\n$%.2f Purchased in Del Mar Totals $%.2f With $%.2f Taxes\n",  purchaseamount, purchasetotal, delmartaxamt); 
  37.             }//end select1 
  38.          
  39.         if (menuselection == 2) 
  40.             {//begin select2 
  41.             encintaxamt = encintaxper * purchaseamount / 100; 
  42.             purchasetotal = encintaxamt + purchaseamount; 
  43.             printf("\nEncinitas - 7.50%% Tax Rate\n"); 
  44.             printf("\n$%.2f Purchased in Encinitas Totals $%.2f With $%.2f Taxes\n", purchaseamount, purchasetotal, encintaxamt); 
  45.             }//end select2 
  46.         if (menuselection == 3) 
  47.             {//begin select3 
  48.             lajoltaxamt = lajoltaxper * purchaseamount / 100; 
  49.             purchasetotal = lajoltaxamt + purchaseamount; 
  50.             printf("\nLa Jolla - 7.75%% Tax Rate\n"); 
  51.             printf("\n$%.2f Purchased in La Jolla Totals $%.2f With $%.2f in Taxes\n", purchaseamount, purchasetotal, lajoltaxamt); 
  52.             }//end select3 
  53.          
  54.         if (menuselection == 4) 
  55.             {//begin select4 
  56.             printf("\nHave a Good Day!"); 
  57.             }//end select4 
  58.         if (menuselection < 1 || menuselection > 4) 
  59.             {//begin invselect 
  60.             printf("\nInvalid Selection - Please Choose 1 - 4\n"); 
  61.             break;  
  62.             }//end invselect
  63.              
  64.     }//end function 
  65.             printf("\n\nPress any key to close the window"); 
  66.                       getch(); 
  67.                       return 0; 
  68. }//end program


O.k. so now I know that bold doesn't work in code. The lines for the code that is appearing that shouldn't be is lines 20-23. The lines for the statement that should show is 64-68. The lines for the menu is 25-33.

Hope this helps



 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-12, 04:26am
Rank: ? (4821)
Member #: 3416
in the event an administrator is reading this, could you please make me a moderator of this forum?

i just figured you were a moderator in this forum since you answer so many of the questions! i'm not an administrator but next time i'm on irc i'll tell jester he should add you as a moderator here.

my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
 
  Reply to this ·  Post link ·  Top
Subject: Re: Another Newbie Needs Help  ·  Posted: 2006-12-12, 10:00am
Rank: ? (767)
Member #: 11085
Thank you, misterhaan, formatting code in some random text editor was getting quite annoying. Although, I must say that answering questions here is my best procrastination resource when homework or some project I am working on becomes tiresome.

Also, Unico, I have never heard of the Miracle C compiler, but you should argue for a better one if it is, in fact, producing the output you specified with the code you have supplied. You output simply isn't possible. You have no loop in your code and all code posted is in one function, therefore, there is nothing which interrupts the normal process of execution and moves it to another point. Basically, I am saying that by the time, "Invalid Selection - Please Choose 1 - 4," is printed out, the CPU has already passed up the statement, "Invalid Amount. Enter Cost greater than 0," with no way of returning. Please check if you have accidentally left out some segment of code and, if so, post it here. If you haven't, then there is nothing I can do to assist you beyond telling urging you to use a compiler that actually works as a compiler should.

Post Script: For future reference, Unico, use of the "edit" function is proffered to two posts in a row, especially when the latter does no more than correct the former.

» Post edited 2006-12-12, 10:02am by relpats_eht.

- relpats_eht
 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

icons