Need help with grand total of ticket sales

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 4
Reputation: begnnr_help is an unknown quantity at this point 
Solved Threads: 0
begnnr_help begnnr_help is offline Offline
Newbie Poster

Need help with grand total of ticket sales

 
0
  #1
Jun 18th, 2009
I have a problem where I am writing a program that has a predefined costs for tickets. The program asks what city you want to go to and then it asks how many tickets you want to buy and it calculates the cost. If you want more it will ask you a city code and then how many tickets you want to buy. Then it gives you a grand total of the cost of all of the tickets in one line.

My code is not doing that. It displays a grand total for each ticket sales entry. I need it to total everything at the end.



  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int Get_Valid_Code();
  8. char response;
  9.  
  10. int main()
  11. {
  12. cout << setprecision(2)
  13. << setiosflags(ios::fixed)
  14. << setiosflags(ios::showpoint);
  15.  
  16. Get_Valid_Code();
  17.  
  18. }
  19. int Get_Valid_Code()
  20. {
  21. bool invalid_data;
  22. int cities,
  23. NUM_TICKETS;
  24. double grand_total,
  25. CHARGE;
  26.  
  27. grand_total = 0.00;
  28.  
  29. double price[6] = {86.79, 149.69, 193.49, 55.29, 81.40, 93.91};
  30.  
  31. {
  32. cout << "What is the number of your destination city? ";
  33. cin >>cities;
  34.  
  35. if (cities < 1 || cities > 7)
  36. {
  37. cerr << endl;
  38. cerr << "You entered an invalid city number. The program will now terminate." << endl;
  39. invalid_data = true;
  40. return 0;
  41. }
  42. else invalid_data = false;
  43.  
  44. cout << "How many tickets do you wish to purchase? ";
  45. cin >> NUM_TICKETS;
  46.  
  47. CHARGE = NUM_TICKETS * price[cities - 1];
  48. cout << "The total cost is $" << CHARGE << endl << endl;
  49. grand_total += CHARGE;
  50.  
  51. cout << "Do you want to purchase more tickets? " ;
  52. cin >> response;
  53. }
  54. while (response == 'Y' || response == 'y')
  55. Get_Valid_Code();
  56.  
  57.  
  58. cout << endl << endl;
  59. cout << "The Grand Total is $" << grand_total << endl;
  60.  
  61. return 0;
  62. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 104
Reputation: goody11 is an unknown quantity at this point 
Solved Threads: 1
goody11 goody11 is offline Offline
Junior Poster

Re: Need help with grand total of ticket sales

 
-1
  #2
Jun 18th, 2009
I found the problem. Your grand total kept resetting to 0.00 because it was in the while loop. Here is a working copy of the code.

  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int Get_Valid_Code();
  8. char response;
  9. double grand_total = 0.00;
  10. int main()
  11. {
  12. cout << setprecision(2)
  13. << setiosflags(ios::fixed)
  14. << setiosflags(ios::showpoint);
  15.  
  16. Get_Valid_Code();
  17.  
  18. }
  19. int Get_Valid_Code()
  20. {
  21. bool invalid_data;
  22. int cities,
  23. NUM_TICKETS;
  24. double CHARGE;
  25.  
  26.  
  27.  
  28. double price[6] = {86.79, 149.69, 193.49, 55.29, 81.40, 93.91};
  29.  
  30. {
  31. cout << "What is the number of your destination city? ";
  32. cin >>cities;
  33.  
  34. if (cities < 1 || cities > 7)
  35. {
  36. cerr << endl;
  37. cerr << "You entered an invalid city number. The program will now terminate." << endl;
  38. invalid_data = true;
  39. return 0;
  40. }
  41. else invalid_data = false;
  42.  
  43. cout << "How many tickets do you wish to purchase? ";
  44. cin >> NUM_TICKETS;
  45.  
  46. CHARGE = NUM_TICKETS * price[cities - 1];
  47. cout << "The total cost is $" << CHARGE << endl << endl;
  48. grand_total = grand_total + CHARGE;
  49.  
  50. cout << "Do you want to purchase more tickets? " ;
  51. cin >> response;
  52. }
  53. while (response == 'Y' || response == 'y')
  54. Get_Valid_Code();
  55.  
  56.  
  57. cout << endl << endl;
  58. cout << "The Grand Total is $" << grand_total << endl;
  59. system("pause");
  60.  
  61. return 0;
  62. }
Last edited by Tekmaven; Jun 18th, 2009 at 5:25 pm. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 104
Reputation: goody11 is an unknown quantity at this point 
Solved Threads: 1
goody11 goody11 is offline Offline
Junior Poster

Re: Need help with grand total of ticket sales

 
0
  #3
Jun 18th, 2009
woops! I accidentilly left in the system("pause"). You can take that out if you want that was for me so I could see what the grand total was.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 4
Reputation: begnnr_help is an unknown quantity at this point 
Solved Threads: 0
begnnr_help begnnr_help is offline Offline
Newbie Poster

Re: Need help with grand total of ticket sales

 
0
  #4
Jun 18th, 2009
Thanks for the help. That worked like a charm.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC