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;
}
Comments
Still no code tags!! 11Posts

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.

This question has already been answered. Start a new discussion instead.