Running Total in Do While Loop

This is a homework assignment so I understand that answers cannot be given.
I have been having trouble trying to figure out how to create a running total in my do while loop so that after displaying each customer's total, it can display the grand total. I have started a new variable called RunningTotal but I have not been able to incorporate it properly. Every time I try to add it in, it just skips to the GrandTotal instead of adding another customer or just takes one customer's input. I hope this makes sense. Any help is appreciated! Thank you!

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include  <cmath>
#include <iomanip>
using namespace std;

const double FacilityCharge = 3.50; //Facility charge added to bill.;                                                        

int main ()
{
  int  GamesPlayed;       //Number of games played.                                                                          
  float GrandTotal;       //Total customer owes including facility charge.                                                   
  float PricePerGame;     //Base rate per game played.                                                                       
  float Tax;              //Amount of tax owed.                                                                              
  float TotalGamePrice;   //Total based rate owed.                                                                           
  char Choice1;           //Variable storing user's first choice.                                                            
  char Choice2;           //Variable storing user's second choice.                                                           
  char Choice3;           //Variable for storing user's third choice.                                                        
  float ShoeRental;       //Charge if customer opts for shoe rental.                                                         
  char AddPlayer;         //Add player if more customers                                                                     
  float RunningGameTotal;//Running totals from all customers.;                                                               

    cout << "Weekday rate or weekend rate? Enter 'd' for weekday or 'e' for weekend:" << endl;
    cin  >> Choice1;

 do
   {
    cout << "Bowling game price category? Enter 'a' for Adults, 'j' for Juniors, 's' for Seniors or 'c' for Bowling Club mem\
bers:"
           << endl;
    cin  >> Choice2;

    if (Choice1=='d'&&Choice2=='a')
      {PricePerGame = 3.25;
      }
      else
       if (Choice1=='d'&&Choice2=='j')
       {PricePerGame = 2.00;
       }
       else
        if (Choice1=='d'&&Choice2=='s')
        {PricePerGame = 2.00;
        }
 else
         if (Choice1=='d'&&Choice2=='c')
          {PricePerGame = 1.75;
          }
          else
           if (Choice1=='e'&&Choice2=='a')
            {PricePerGame = 3.25;
            }
            else
             if(Choice1=='e'&&Choice2=='j')
              {PricePerGame = 3.25;
              }
              else
               if(Choice1=='e'&&Choice2=='s')
                {PricePerGame = 3.25;
                }
                else
                 if(Choice1=='e'&&Choice2=='c')
                  {PricePerGame = 1.75;
                  }

  cout << "Please enter the amount of games to be played:" << endl;
  cin  >> GamesPlayed;
  cout << "Will shoes be rented? Enter 'y' for Yes or 'n' for No:" << endl;
  cin  >> Choice3;
 if (Choice3=='y')
    {ShoeRental = 3.50;
    }
  else
    if (Choice3=='n')
      {ShoeRental = 0.00;
      }

  TotalGamePrice = (PricePerGame * GamesPlayed);
  Tax = (TotalGamePrice + ShoeRental) * (0.081);
  GrandTotal = (TotalGamePrice) +(ShoeRental) + (Tax)+(FacilityCharge);

  cout << setfill ('-') << setw(49) <<"-" << endl;
  cout << left << setfill(' ') << setw(40) << "Bowling Game:"
       << right  << "$"
       << setw(8) << right << fixed << showpoint << setprecision(2) << TotalGamePrice << endl;
  cout << left << setfill (' ') << setw(40)
       << left << setfill (' ') << setw(8) << " " << "Price Per Game:"
       << setw(17) << " "
       << right << "$" << setw(8) << right << fixed << showpoint << setprecision(2) <<  PricePerGame << endl;
  cout << left << setfill (' ') << setw(8) << " " <<  "Number of games to play:"
       << setfill (' ') <<setw(16) << " " << right << GamesPlayed << endl;
  cout << left << setfill (' ') << setw(40) << "Shoe Rental:"
       << right << "$"
       << setw(8) << right << fixed << showpoint << setprecision(2) << ShoeRental << endl;
  cout << setfill ('-') << setw(49) <<"-" <<endl;
  cout << endl;

  cout << "Add another player? - 'y' or 'x'(exit):"
       << endl;

  cin  >> AddPlayer;
  if (AddPlayer == 'n')
    {GrandTotal;
    }

   } while (AddPlayer == 'y');

  TotalGamePrice = (PricePerGame * GamesPlayed);
  Tax = (TotalGamePrice + ShoeRental) * (0.081);
  GrandTotal = (TotalGamePrice) +(ShoeRental) + (Tax)+(FacilityCharge);

  cout << setfill ('=') << setw(49) <<"=" <<endl;
  cout << left << setfill(' ') << setw(40) << "Game Total:"
       << right << "$" << setw(8) << right
       << fixed << showpoint << setprecision(2) << TotalGamePrice << endl;
  cout << left << setfill (' ') << setw(40) << "Shoe Rental:"
       << right << "$" << setw(8) << right
       << fixed << showpoint << setprecision(2) << ShoeRental << endl;
  cout << left << setfill (' ') << setw(40)
       << fixed << showpoint << setprecision(2) << "Fees and Tax" << endl;
  cout << left << setfill (' ') << setw(7) << " " <<  "State Sales Tax(8.1%):"
       << right << setw(11) <<  " "  << "$" << setw(4)
       << right << " " << Tax << endl;
  cout << left << setfill (' ') << setw(7) << " " << "Facility Charge:"
       << right << setw(17) << " " << "$" << setw(4)
       << right << " " << FacilityCharge << endl;
  cout << setfill(' ') << setw(49) << " " << endl;
  cout << left << setfill (' ') << setw(40) << "Grand Total:"
       << right << "$" << setw(8) << right << GrandTotal << endl;
  cout << setfill ('=') << setw(49) <<"=" <<endl;
  cout << endl;
  cout << endl;
  cout << "Thank you for visiting Viva Vegas Bowling Center!" << endl;
  cout << endl;
  cout << endl;
  cout << setfill ('=') << setw(49) <<"=" <<endl;
  cout << setfill (' ') << setw(2)  << " " << left << "Generated by April Jamison"
       << setfill (' ') << setw(5)  << " " << right << "Assignment #6" << endl;
  cout << setfill ('=') << setw(49) <<"=" <<endl;


  return 0;
Variable is RunningGameTotal* not RunningTotal, sorry!
So is RunningGameTotal the grand total of all of the games from all of the customers? Ie. The sum of all of the GrandTotals?

I'm a little confused on your second problem... Does the loop work when you enter 'y' for a new player? Or is it exiting out of the loop and ending the program?

Also, what is the purpose of lines 100-102?
Yes, RunningGameTotal is the grand total of all the games from the customers.

The loop still works when I enter 'y' for a new player. The only problem I am having is when I am done adding customers and choose 'x', it only displays the last customer that was put in.

And... for lines 100-102, that was to exit if there are no more customers. I made a typo and it should have been an 'x' rather than 'n'.
OK. Lines 100-102 will not cause it to exit. That is what the while condition will do. Effectively it will loop until you enter anything other than 'y', so 'x' isn't a requirement.

As for the RunningGameTotal, can't you just do something like
RunningGameTotal += GrandTotal; after you've computed the GrandTotal on line 78? You will also need to initialize RunningGameTotal to 0 on line 20 or you could end up getting junk.

The reason it is showing the last customer information after you exit the loop is that you are printing the last values that were just read or computed. If you want totals for shoe rentals, facility charges, taxes, etc... You will need to do something extra like have a counter for the number of shoe rentals, or the number of customers paying a facility charges... Then print those values at the end.

Your if/else statements on lines 32-62 can also be simplified. You have 2 cases for Choice1, so something more like this might be easier to read and manage:

1
2
3
4
5
if(Choice1 == 'd') {
  //4 if/else statements for Choice2 go here for weekday rates
} else if(Choice1 == 'e') {
  //4 if/else statements for Choice2 go here for weekend rates
}


Last edited on
Okay, thank you!
You are right, this is very helpful!
I will try this out right away.
Topic archived. No new replies allowed.