How to add a running total?

I just need a running total to sum up the number of seats taken at their cost. Can anyone provide tips of some sort? I'd appreciate it. I've included my following code, everything works fine (least I think), but I'm just missing that running total.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;

const int n_of_rows = 15;
const int n_of_columns = 4;
char choose (char seat[][n_of_columns]);
void available (char seat[][n_of_columns]);
void show_seats(char seat[][n_of_columns]);
	 
int main(void)
{
	char seat[n_of_rows][n_of_columns] = { {'A', 'B', 'C', 'D'},
					       {'A', 'B', 'C', 'D'},
	                                       {'A', 'B', 'C', 'D'},
	                                       {'A', 'B', 'C', 'D'},
	                                       {'A', 'B', 'C', 'D'},
	                                       {'A', 'B', 'C', 'D'},
	                                       {'A', 'B', 'C', 'D'}, 
					       {'A', 'B', 'C', 'D'},
					       {'A', 'B', 'C', 'D'},
					       {'A', 'B', 'C', 'D'},
					       {'A', 'B', 'C', 'D'},
					       {'A', 'B', 'C', 'D'},
					       {'A', 'B', 'C', 'D'},
					       {'A', 'B', 'C', 'D'},
					       {'A', 'B', 'C', 'D'},};
char answer;
do
{
	cout << endl << "Would you like to make a reservation? (Y/N) ";
	cin >> answer;
	if (answer == 'Y' || answer == 'y')
	{
		cout << endl << "Welcome to Hancock Airline Reservation. Please note that"
			 << endl << "any seats shown with a * is taken" << endl << endl ;

		ifstream inFile;
		int price1, price2, price3;

		inFile.open("SeatPrices.txt");

		inFile >> price1;
		inFile >> price2;
		inFile >> price3;

		inFile.close();

		cout << endl << "All First Class Seats cost: $" << price1 << " "
			 << endl << "First Five Coach Rows Cost: $" << price2 << " "
			 << endl << "Last Five Coach Rows Cost: $"  << price3 << " " << endl << endl;

	    show_seats (seat);
	    available (seat);
	    choose (seat);
	    seat;
	    show_seats (seat);
	}
	else
	    std::exit(EXIT_FAILURE);
	}while(1);
	 return EXIT_FAILURE;
}
	 
void available (char seats[][n_of_columns])
{
	for (int i = 0; i < n_of_rows; i++)
	for (int j = 0; j < n_of_columns; j++)
		seats[i][j];
	return;
}
	 
char choose (char seats[][n_of_columns])
{
	int row, column;
	double firstClass = 200.00,
		   firstFiveCoach = 450.00,
		   lastFiveCoach = 375.00;
	char letter;

	cout << endl << "Enter Row (0-14): ";
	cin >> row;

	cout << endl << endl << "Enter Seat (A-D): ";
	cin >> letter;
	letter = toupper(letter);
	column = letter - 65;

	if (row < 0 && row > 14)
	{
		cout << endl << "Invalid Row. " << endl;
		row--;
	}

	if (row >= 0 && row <= 4)
	{
		cout << endl << "Your total charge is: $" << firstClass << endl << endl;
	}
	
	if (row >= 5 && row <= 9)
	{
		cout << endl << "Your total charge is: $" << firstFiveCoach << endl << endl;
	}

	if (row >= 10 && row <= 14)
	{
		cout << endl << "Your total charge is: $" << lastFiveCoach << endl << endl;
	}
		
	if (seats[row][column] == '*')
		cout << endl << "This seat has been taken. Please try another" << endl << endl;
	    seats[row][column] = '*';
			
	return seats['*'][n_of_columns];
}	                 
void show_seats (char seats[][n_of_columns])
{
	for (int i = 0; i < n_of_rows; i++)
		cout << seats[i][0] << "\t"                   
	         << seats[i][1] << "\t"
	         << seats[i][2] << "\t"
	         << seats[i][3] << "\t" << endl;
	return;
}
You need to keep track of the value somewhere. It's probably best to do this in the main by returning the total cost from your choose function, but to keep changes to a minimum you can do it this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
char choose (char seats[][n_of_columns])
{
	static double runningTotal = 0.00; // static means that this label will memorize the value from the last call of this function.
	int row, column;
	double firstClass = 200.00,
		   firstFiveCoach = 450.00,
		   lastFiveCoach = 375.00;
	char letter;

	cout << endl << "Enter Row (0-14): ";
	cin >> row;

	cout << endl << endl << "Enter Seat (A-D): ";
	cin >> letter;
	letter = toupper(letter);
	column = letter - 65;

	if (row < 0 && row > 14)
	{
		cout << endl << "Invalid Row. " << endl;
		row--;
	}

	if (row >= 0 && row <= 4)
	{
		cout << endl << "Your total charge is: $" << firstClass << endl << endl;
		runningTotal += firstClass;
	}
	
	if (row >= 5 && row <= 9)
	{
		cout << endl << "Your total charge is: $" << firstFiveCoach << endl << endl;
		runningTotal += firstFiveCoach;
	}

	if (row >= 10 && row <= 14)
	{
		cout << endl << "Your total charge is: $" << lastFiveCoach << endl << endl;
		runningTotal += lastFiveCoach;
	}
		
	if (seats[row][column] == '*')
		cout << endl << "This seat has been taken. Please try another" << endl << endl;
	    seats[row][column] = '*';
		
	cout << endl << "Running total: $" << runningTotal << endl << endl;
		
	return seats['*'][n_of_columns];
}	
Last edited on


Just some style points:

1. Avoid infinite loops.
2. Make use of a bool variable to help control the looping.
3. use the toupper or tolower function to avoid the double test.
4. If you get some input, check it's validity.
5. I avoid using do loops - they can be rewritten as while or for loops.
6. In the show_seats function, use a nested for loop.
7. Don't use using namespace std;, put std:: before each std thing
8. When opening files, check that it worked.

I am confused about this:
return seats['*'][n_of_columns];


with this:
1
2
3
4
5
6
7
8
9
void show_seats (char seats[][n_of_columns])
{
	for (int i = 0; i < n_of_rows; i++)
		cout << seats[i][0] << "\t"                   
	         << seats[i][1] << "\t"
	         << seats[i][2] << "\t"
	         << seats[i][3] << "\t" << endl;
	return;
}


the return; isn't needed because it is a void function.

With this:
char choose (char seat[][n_of_columns]);

It isn't needed because you send it the entire array anyway.

To calc the sum, cycle through the array with nested for loops, and keep adding to the sum.

With variable names, I prefer something like NumCol rather than n_of_columns

With this:
{'A', 'B', 'C', 'D'},};

There is an extra comma.

1
2
3
4
5
6
7
void available (char seats[][n_of_columns])
{
	for (int i = 0; i < n_of_rows; i++)
	for (int j = 0; j < n_of_columns; j++)
		seats[i][j]; //what does this do?
	return;
}


I suggest you fix these things, if still trouble post compiler output.

HTH Good Luck
Thank you for the replies and suggestions. First, Stewbond, I've made those changes to add the running total, I noticed one slight error with it. If I enter a seat that's already taken, it takes the cost and adds it to the total when it shouldn't.

TheIdeasMan: I fixed most of those with no problems. The code you were confused about, return seats['*'][n_of_columns];, that's there for when the display comes back, the * will be there for that seat taken. I noticed if I leave that out it doesn't keep the *.
@TheIdeasMan, I don't know that most of those style preferences are so commonly adopted that we can tell someone 'you should do it this way'. 1 and 2 I disagree with you, 5 I agree but consider it a personal choice, and 7 I tend to disagree with more and more: the standards committee has accepted the fact that 'using namespace std;' is common practice, so much so that they named they chose not to name unorderered_set/map hash_set/map in order to avoid name collision with existing applications. If they have accepted it, I feel I have to as well (and honestly, it is so much easier - who is going to write their own library with an 'ifstream' class anyway?).
@rollie

1. I was meaning that newbies shouldn't make a habit of using infinite loops when there is no real need.
2. Why not have a boolean Quit variable? Then it can be tested in the while condition - no need for the infinite loop.
5. I dislike do loops because i find them error prone, and IMO it is clearer to use a for or while loop. There are situations when a do loop might be necessary, but these are rare. Bjarne Stroustrup and Kernighan & Richie don't like them either.
7. Well, experienced coders have been pre-pending std:: for years - so they can avoid bringing in the entire std namespace, and it makes it easier on the compiler but not having to decide to discard all the things that aren't being used. (I am not sure exactly how the compiler does that) Maybe things are changing with recent work by the standards committee.

@BinaryGeek

return seats['*'][n_of_columns];

The part that confused was the ['*']

In my mind that would evaluate to the integer value of the * char, which would very easily put your array out of bounds.

that's there for when the display comes back, the * will be there for that seat taken. I noticed if I leave that out it doesn't keep the *.


That is not the way to set the value of an element in an array - you have to use legal subscripts and an assignment operator to do that.
Topic archived. No new replies allowed.