Need help with FOR repitition with Switch

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2009
Posts: 3
Reputation: gtrippleb is an unknown quantity at this point 
Solved Threads: 0
gtrippleb gtrippleb is offline Offline
Newbie Poster

Need help with FOR repitition with Switch

 
0
  #1
Nov 3rd, 2009
I'm trying to write a c++ program that first asks how may people are in a party, then it takes that number to calculate how many times to ask what the next person wants to order. This is what I have so far. I can get the program to recognize how many people I have entered in for the amount of people in a party. What I'm having a problem with is totaling the amount and then displaying the total amount due. The available menu items are a, b, c, d, and x. Each one has a value amount except for x. So if x is entered, then there is no amount to add to the total.

With what I have right now, the program will ask and hold the amount of people in the party. So the program will ask for an order the correct amount of time, but it isn't totaling my cost. So somewhere I'm missing a holder for the total and I'm just not sure on that part. Any help is greatly appreciated.


  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. int main( )
  8. {
  9. char itemOrdered = ' ';
  10. double customers = 0.0;
  11. double total = 0.0;
  12. int numCustomers = 0;
  13.  
  14. cout << "Enter number of customers in party: ";
  15. cin >> numCustomers;
  16.  
  17. cout << "This program calculates total for " << numCustomers << " customers." << endl;
  18.  
  19. for (int counter = 0; counter < numCustomers; counter = counter + 1)
  20. {
  21. cout << "Our lunch menu today: " << endl;
  22. cout << " Combo A: Fried chicken with slaw $4.25" << endl;
  23. cout << " Combo B: Roast beef and mashed potato $5.75" << endl;
  24. cout << " Combo C: Fish and chips $5.25" << endl;
  25. cout << " Combo D: Soup and salad $3.75" << endl;
  26. cout << "Enter combo code[A/B/C/D] or X for no order: ";
  27. cin >> itemOrdered;
  28. itemOrdered = toupper(itemOrdered);
  29.  
  30. for (char itemOrdered = 0; itemOrdered < numCustomers;)
  31. {
  32. switch(itemOrdered)
  33. {
  34. case 'A': total = total + 4.25;
  35. break;
  36. case 'B': total = total + 5.75;
  37. break;
  38. case 'C': total = total + 5.25;
  39. break;
  40. case 'D': total = total + 3.25;
  41. break;
  42. case 'x': total = total + 0.00;
  43. }
  44. cout << "Enter next item ordered [A/B/C/D/X] : ";
  45. cin >> itemOrdered;
  46. itemOrdered = toupper(itemOrdered);
  47. }
  48. }
  49. cout << "Please pay this amount: " << total << endl;
  50.  
  51. system("pause");
  52. return 0;
  53. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: alg is an unknown quantity at this point 
Solved Threads: 3
alg alg is offline Offline
Newbie Poster
 
0
  #2
Nov 3rd, 2009
You can use while
  1. while (itemOrdered != 'x')
  2. {
  3. switch(itemOrdered)
  4. {
  5. .
  6. .
  7. .
  8. }
  9. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 625
Reputation: jonsca will become famous soon enough jonsca will become famous soon enough 
Solved Threads: 84
Sponsor
jonsca jonsca is online now Online
Practically a Master Poster
 
0
  #3
Nov 3rd, 2009
Scratch... sorry. I think he's right with the while, except use your count variable. You were getting stuck since your for loop wasn't incrementing itemsOrdered but even doing that your count would be off. Go for the while or do{} while().

(when writing this I had missed that itemsOrdered was a char, but you got it to work so good luck and hope we at least nudged you a bit in the right direction )
Last edited by jonsca; Nov 3rd, 2009 at 2:49 pm. Reason: incorrect assumption
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: gtrippleb is an unknown quantity at this point 
Solved Threads: 0
gtrippleb gtrippleb is offline Offline
Newbie Poster
 
0
  #4
Nov 3rd, 2009
It looks like I was resetting my count to zero with the second itemOrdered statement. So after I removed that, and some other code, I was able to get it to work correctly. Thanks for the help.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: gtrippleb is an unknown quantity at this point 
Solved Threads: 0
gtrippleb gtrippleb is offline Offline
Newbie Poster
 
0
  #5
Nov 3rd, 2009
For this program I wasn't able to use a while statement. Here is the code that I was able to get to work. Thanks again for the suggestions.

#include <iostream>
  1. using std::cout;
  2. using std::cin;
  3. using std::endl;
  4.  
  5. int main( )
  6. {
  7. char itemOrdered = ' ';
  8. double customers = 0.0;
  9. double total = 0.0;
  10. int numCustomers = 0;
  11.  
  12. cout << "Enter number of customers in party: ";
  13. cin >> numCustomers;
  14.  
  15. cout << "This program calculates the total due for " << numCustomers << " customers." << endl;
  16.  
  17. for (int counter = 0; counter < numCustomers; counter = counter + 1)
  18. {
  19. cout << "Our lunch menu today: " << endl;
  20. cout << " Combo A: Fried chicken with slaw $4.25" << endl;
  21. cout << " Combo B: Roast beef and mashed potato $5.75" << endl;
  22. cout << " Combo C: Fish and chips $5.25" << endl;
  23. cout << " Combo D: Soup and salad $3.75" << endl;
  24. cout << "Enter combo code[A/B/C/D] or X for no order: ";
  25. cin >> itemOrdered;
  26. itemOrdered = toupper(itemOrdered);
  27. {
  28. switch(itemOrdered)
  29. {
  30. case 'A': total = total + 4.25;
  31. break;
  32. case 'B': total = total + 5.75;
  33. break;
  34. case 'C': total = total + 5.25;
  35. break;
  36. case 'D': total = total + 3.25;
  37. break;
  38. case 'x': total = total + 0.00;
  39. }
  40. itemOrdered = toupper(itemOrdered);
  41.  
  42. }
  43. }
  44. cout << "Please pay this amount: " << total << endl;
  45.  
  46. system("pause");
  47. return 0;
  48. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 244 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC