Storing outputs


Im doing a program for a parking garage.

Im trying to save the cost of each car that leaves the garage then totak it at the end.

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
 

#include <cstdlib>
#include <iostream>
#include <math.h>


using namespace std;

int main(int argc, char *argv[])
{
    int cars = 0;
    int userChoice;
    const int flatRate = 6; 
    const double hourlyFee = 2.50;
    double customerFee = 0;
    int carHours; 
     
 ...


    cout <<  " Do you want to exit? if so enter -1 if enter any key" << endl;
    cin >> userChoice;
   
   if (userChoice != -1) 
     {
       cars += 1;// record ammount of cars input into program
       cout << cars << endl;
     }
     
}
 while (userChoice != -1);  
       
       cout << " The total number of Cars for today was: " << cars << endl; 
       cout << "Have a nice Day" << endl; 
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

 

how can i store the charge of each car?
Last edited on
on line 18 say..

 
double totalCost(0.0);


Then when you increment the number of cars (line58?):
 
totalCost+=customerFee;


edit: i would also initialise your 'customerFee' to zero at the very top of your do while loop (before your first if statement for example), rather than outside.
Last edited on
mutexe can i ask one more question the program is not receiving the last car ( if i get the price of 4 cars it only registers up until 3 and disregards 4) is it a matter of moving car+=1 ?
pretty much yes. As you can see on line 56 if the user enters -1 the last car is not added.
you may as well do the increment as soon as you're in the do while loop i think.

edit: actually, delete lines 56, 57 and 60. That should do it.

Last edited on
okay thank you very much
Can i make one more suggestion?
Change:
 
int userChoice;

to
 
char userChoice;


then do something like this:

1
2
3
4
5
6
7
8
9

// increment number of cars
cars++;// record amount of cars input into program
cout << "Enter q to quit, or any other key to enter a new car" << endl;
		cin >> userChoice;
		
		

	} while (userChoice != 'q');

rather than use an int to test.
Last edited on
thank you for the suggestion is does make the code more simple or user friendly.
Topic archived. No new replies allowed.