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.

#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;
}

Dani AI

Generated

A correct quick fix was applied by and accepted by , but the program can be made clearer and more robust by separating a single prompt/charge computation from the loop that accumulates the grand total. That avoids globals, removes the fragile recursion pattern, and makes input validation and array bounds checks easier to reason about.

One compact pattern: have a function that prompts once and returns the computed subtotal (or a sentinel like 0 to signal "done"), and keep a simple loop in main that adds each returned subtotal to grand_total. Example outline:

double promptSubtotal(const std::array<double,6>& prices) {
    int city, tickets;
    std::cout << "City (1-6, 0 to finish): ";
    if (!(std::cin >> city)) { std::cin.clear(); std::cin.ignore(10000,'\n'); return 0.0; }
    if (city == 0) return 0.0;
    if (city < 1 || city > static_cast<int>(prices.size())) { std::cerr << "Invalid city\n"; return 0.0; }
    std::cout << "Tickets: ";
    if (!(std::cin >> tickets) || tickets < 0) { std::cin.clear(); std::cin.ignore(10000,'\n'); std::cerr << "Invalid tickets\n"; return 0.0; }
    return tickets * prices[city-1];
}

Practical cautions and improvements: avoid system("pause") and recursion for user input; always validate cin (use cin.clear()/cin.ignore() on bad input); derive valid range from the price container (use prices.size() or a named constant) to prevent off-by-one errors; consider storing money as integer cents (or a fixed-point/decimal type) to avoid floating-point rounding in financial totals; and prefer returning values or passing by reference instead of globals to make code testable and maintainable.

These changes keep the grand total calculation explicit (printed once after the loop), reduce side effects, and make future enhancements (named cities, dynamic price lists) straightforward.

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.

#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;
}
commented: Still no code tags!! 11Posts -2

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.

Thanks for the help. That worked like a charm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.