| | |
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:
Solved Threads: 0
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.
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)
#include <iostream> #include <iomanip> using namespace std; int Get_Valid_Code(); char response; int main() { cout << setprecision(2) << setiosflags(ios::fixed) << setiosflags(ios::showpoint); Get_Valid_Code(); } int Get_Valid_Code() { bool invalid_data; int cities, NUM_TICKETS; double grand_total, CHARGE; grand_total = 0.00; double price[6] = {86.79, 149.69, 193.49, 55.29, 81.40, 93.91}; { cout << "What is the number of your destination city? "; cin >>cities; if (cities < 1 || cities > 7) { cerr << endl; cerr << "You entered an invalid city number. The program will now terminate." << endl; invalid_data = true; return 0; } else invalid_data = false; cout << "How many tickets do you wish to purchase? "; cin >> NUM_TICKETS; CHARGE = NUM_TICKETS * price[cities - 1]; cout << "The total cost is $" << CHARGE << endl << endl; grand_total += CHARGE; cout << "Do you want to purchase more tickets? " ; cin >> response; } while (response == 'Y' || response == 'y') Get_Valid_Code(); cout << endl << endl; cout << "The Grand Total is $" << grand_total << endl; return 0; }
•
•
Join Date: Jun 2009
Posts: 104
Reputation:
Solved Threads: 1
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)
#include <iostream> #include <iomanip> using namespace std; int Get_Valid_Code(); char response; double grand_total = 0.00; int main() { cout << setprecision(2) << setiosflags(ios::fixed) << setiosflags(ios::showpoint); Get_Valid_Code(); } int Get_Valid_Code() { bool invalid_data; int cities, NUM_TICKETS; double CHARGE; double price[6] = {86.79, 149.69, 193.49, 55.29, 81.40, 93.91}; { cout << "What is the number of your destination city? "; cin >>cities; if (cities < 1 || cities > 7) { cerr << endl; cerr << "You entered an invalid city number. The program will now terminate." << endl; invalid_data = true; return 0; } else invalid_data = false; cout << "How many tickets do you wish to purchase? "; cin >> NUM_TICKETS; CHARGE = NUM_TICKETS * price[cities - 1]; cout << "The total cost is $" << CHARGE << endl << endl; grand_total = grand_total + CHARGE; cout << "Do you want to purchase more tickets? " ; cin >> response; } while (response == 'Y' || response == 'y') Get_Valid_Code(); cout << endl << endl; cout << "The Grand Total is $" << grand_total << endl; system("pause"); return 0; }
Last edited by Tekmaven; Jun 18th, 2009 at 5:25 pm. Reason: Code Tags
![]() |
Similar Threads
- Data reports Grand total issue (Visual Basic 4 / 5 / 6)
- Updating prices in a 2d array (C++)
- why is the program not working?? (C++)
- BASH script - sales per each associate (3 files) (Shell Scripting)
- Need help generating a Grand Total (VB.NET)
- Confusing directions (VB.NET)
- C help calculate grand total (C)
Other Threads in the C++ Forum
- Previous Thread: is there a better way
- Next Thread: Organising code
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





