943,882 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 477
  • C++ RSS
Jun 18th, 2009
0

Need help with grand total of ticket sales

Expand Post »
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.



C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
begnnr_help is offline Offline
4 posts
since Jun 2009
Jun 18th, 2009
-1

Re: Need help with grand total of ticket sales

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.

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 8
Solved Threads: 1
Junior Poster
goody11 is offline Offline
117 posts
since Jun 2009
Jun 18th, 2009
0

Re: Need help with grand total of ticket sales

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.
Reputation Points: 8
Solved Threads: 1
Junior Poster
goody11 is offline Offline
117 posts
since Jun 2009
Jun 18th, 2009
0

Re: Need help with grand total of ticket sales

Thanks for the help. That worked like a charm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
begnnr_help is offline Offline
4 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: is there a better way
Next Thread in C++ Forum Timeline: Organising code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC