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

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.