C++ Void and cout

Hello everyone,

Could you please help me to understand why the program doesn't see the input data?
I need it to display the hours and minutes and it shows 0:0...
I am wondering what I am doing wrong.

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//This program calculates charges for a parking garage based on the type of
//vehicle and the time spent in the garage.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

//constants for rates
#define firstCarRate 0.00 //first rate for cars.
#define secondCarRate 1.25 //second rate for cars.
#define firstTruckRate 3.75 //first rate for trucks.
#define secondTruckRate 4.50 //second rate for trucks.
#define firstBusRate 2.00 //first rate for buses.
#define secondBusRate 2.50 //second rate for buses.

//getInfo Function Prototype.
void getInfo(char vehicle, int hourIn, int minIn, int hourOut, int minOut);

//time function prototype.
void time(int hourIn, int minIn, int hourOut, int minOut, int houtParkTime, int minParkTime, int roundedTotal, int rounds);

//rate function prototype.
void rate(char vehicle, int units, float firstRate, float secondRate);

//charge function prototype.
void charge(int roundedTotal, int units, float firstRate, float secondRate, float totalCharge);

//print bill function prototype.
void printBill(char vehicle, int hourIn, int minIn, int hourOut, int minOut, int hourParkTime, int minParkTime, int roundedTotal, float totalCharge);

//global variables
char vehicle;
int units;
int hourIn;
int minIn;
int hourOut;
int minOut;
int hourParkTime;
int minParkTime;
int roundedTotal;
int rounds;
float firstRate;
float secondRate;
float totalCharge;

int main(void)
{
	getInfo(vehicle, hourIn, minIn, hourOut, minOut);
	time(hourIn, minIn, hourOut, minOut, hourParkTime, minParkTime, roundedTotal, rounds);
	rate(vehicle, units, firstRate, secondRate);
	charge(roundedTotal, units, firstRate, secondRate, totalCharge);
	printBill(vehicle, hourIn, minIn, hourOut, minOut, hourParkTime, minParkTime, roundedTotal, totalCharge);
	
	return 0;
}//end of main.

 //function definition for get info.
void getInfo(char vehicle, int hourIn, int minIn, int hourOut, int minOut)
{
	cout << "\nType of vehicle? ";
	cout << "\n(enter C for car,T for truck or B for bus). - ";
	cin >> vehicle;

	//check for valid vehicle type

	switch (vehicle)

	{
	case 'C': cout << "You entered C for 'Car'.\n";
	case 'c': cout << "You entered c for 'Car'.\n";
		break;
	case 'T': cout << "You entered T for 'Truck'.\n";
	case 't': cout << "You entered t for 'Truck'.\n";
		break;
	case 'B': cout << "You entered B for 'Bus'.\n";
	case 'b': cout << "You entered b for 'Bus'.\n";
		break;
	default: cout << "You did not enter a valid vehicle type\n";
		cout << "Please Enter C,c,T,t,B, or b\n";

		return;
	}

	//get the hour that the vehicle entered the garage.

	cout << "\nHour vehicle entered garage? - ";
	cin >> hourIn;

	//validate input for hourIn
	if (hourIn < 0 || hourIn > 23)
	{
		cout << "invalid input. enter an integer between 0 and 23.";
	}

	//get the minute that the vehicle entered the garage.
	cout << "\nMinute vehicle entered garage? - ";
	cin >> minIn;

	//validate input for minIn.
	if (minIn < 0 || minIn > 60)
	{
		cout << "invalid input. enter a number between 0 and 60.";
	}

	//get the hour vehicle exits garage.
	cout << "\nHour vehicle exits garage? - ";
	cin >> hourOut;

	//validate input for hourOut.
	if (hourOut < 0 || hourOut > 23)
	{
		cout << "invalid input. enter an integer between 0 and 23.";
	}

	//get the minute vehicle exits garage.
	cout << "\nMinute vehicle exits garage? - ";
	cin >> minOut;

	//validate input for minOut.
	if (minOut < 0 || minOut > 60)
	{
		cout << "invalid input. enter a number between 0 and 60.";
	}

	return;
}

		//function definition for time.
	void time(int hourIn, int minIn, int hourOut, int minOut, int hourParkTime, int minParkime, int roundedTotal, int rounded)
		{
			if (minOut < minIn)
			{
				minOut = minOut + 60;
				hourOut = hourOut - 1;
			}
			else
			{
				hourParkTime = hourOut - hourIn;
				minParkTime = minOut - minIn;
			}
			if (minParkTime >= 1)
			{
				rounds = hourParkTime + 1;
			}
			else
			{
				rounds = hourParkTime;
			}
			roundedTotal = rounds;
			return; 
			
			system("pause");
		}

		//Function definition for rate.
	void rate(char vehicle, int units, float firstRate, float secondRate)
		{
			switch (vehicle)

			{
			case 'c':
			case 'C': units = 3;
				firstRate = firstCarRate;
				break;

			case 't':
			case 'T': units = 1;
				firstRate = firstTruckRate;
				secondRate = secondTruckRate;
				break;

			case 'b':
			case 'B': units = 2;
				firstRate = firstBusRate;
				secondRate = secondBusRate;
				
				return;
			}
		}
	
		//Function definition for charge.
	void charge(int roundedTotal, int units, float firtsRate, float secondRate, float totalCharges)
	{
		if (roundedTotal <= units)
		{
			totalCharge = (roundedTotal * firstRate);
		}
		else
		{
			totalCharge = (roundedTotal - units) * (secondRate)+(units * firstRate);

			return;
		}
	}

		//Function definition for print Bill.
		void printBill(char vehicle, int hourIn, int minIn, int hourOut, int minOut, int hourParkTime, int minParkTime, int roundedTotal, float totalCharge)
		{

			cout << "\nVehicle Type:\t\t" << vehicle << endl;
			cout << "Time In:\t\t" << hourIn << ":" << minIn << endl;
			cout << "Time Out:\t\t" << hourOut << ":" << minOut << endl;
			cout << "Total Park Time:\t" << roundedTotal << endl;
			cout << "Total Charge:\t\t" << totalCharge << endl;

			system("pause");

			return;
		}
	
		



When you call your functions, you are passing them parameters. The function receives a COPY of the parameter passed. So when you call the function getInfo, the program works on a COPY of hourIn. When the function ends, that copy is thrown away.

1) STOP using global variables.

2) STOP USING GLOBAL VARIABLES.

3) Read up on pass-by-value and pass-by-reference

http://www.learncpp.com/cpp-tutorial/72-passing-arguments-by-value/
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/


4) I REALLY MEAN IT ABOUT THE GLOBAL VARIABLES.
to change a variable in a function via parameter, you need references.

void foo(int &canbechanged)
{
canbechanged = 3; //this will change the variable that the function is called with.
}

without the &, it won't change.
you need to add &s to the getinfo routine for sure, and any others that modify the inputs and expect that to be kept.
Oh! You have another issue. You are using the same variable names for your variables (the globals mostly) and the parameter names. It is extremely hard to work like that, and you will make bugs in your code when you forget which version of the variable you currently have in hand. I strongly advise rename things so you don't duplicate names, esp for now as you learn. Maybe later you will have a justifiable reason (I don't see it, but there are some places one can argue the point) but for now, keep everything to a unique name and your life will be easier.

Thank you! I have worked on the problem a bit more...I thought I got it and I still get bunch of errors...I am on my wits ends. Can you maybe hint what is wrong this time?

//This program calculates charges for a parking garage based on the type of
//vehicle and the time spent in the garage.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

//constants for rates
#define firstCarRate 0.00 //first rate for cars.
#define secondCarRate 1.25 //second rate for cars.
#define firstTruckRate 3.75 //first rate for trucks.
#define secondTruckRate 4.50 //second rate for trucks.
#define firstBusRate 2.00 //first rate for buses.
#define secondBusRate 2.50 //second rate for buses.

//getInfo Function Prototype.
void getInfo(char *vehicle, int *hourIn, int *minIn, int *hourOut, int *minOut);

//time function prototype.
void time(int hourIn, int minIn, int hourOut, int minOut, int *houtParkTime, int *minParkTime, int *roundedTotal, int *rounds);

//rate function prototype.
void rate(char vehicle, int *units, float *firstRate, float *secondRate);

//charge function prototype.
void charge(int roundedTotal, int units, float firstRate, float secondRate, float *totalCharge);

//print bill function prototype.
void printBill(char vehicle, int hourIn, int minIn, int hourOut, int minOut, int hourParkTime, int minParkTime, int roundedTotal, float totalCharge);

// variables
char vehicle;
int units;
int hourIn;
int minIn;
int hourOut;
int minOut;
int hourParkTime;
int minParkTime;
int roundedTotal;
int rounds;
float firstRate;
float secondRate;
float totalCharge;

int main(void)
{
getInfo(&vehicle, &hourIn, &minIn, &hourOut, &minOut);
time(hourIn, minIn, hourOut, minOut, &hourParkTime, &minParkTime, &roundedTotal, &rounds);
rate(vehicle, &units, &firstRate, &secondRate);
charge(roundedTotal, units, firstRate, secondRate, &totalCharge);
printBill(vehicle, hourIn, minIn, hourOut, minOut, hourParkTime, minParkTime, roundedTotal, totalCharge);
return 0;
}

//function definition for get info.
void getInfo(char *vehicle, int *hourIn, int minIn, int *hourOut, int *minOut)
{
cout << "\nType of vehicle? ";
cout << "\n(enter C for car,T for truck or B for bus).";
cin >> vehicle;

//check for valid vehicle type
{
switch (*vehicle)
{
{
case 'C': cout << "You entered C.\n";
case 'c': cout << "You entered c.\n";
break;
}
{
case 'T': cout << "You entered T.\n";
case 't': cout << "You entered t.\n";
break;
}
{
case 'B': cout << "You entered B.\n";
case 'b': cout << "You entered b.\n";
break;
}
cout << "You did not enter a valid vehicle type\n";
cout << "Please Enter C,c,T,t,B, or b\n";
return;
}
}

//get the hour that the vehicle entered the garage.
cout << "\nHour vehicle entered garage? ";
cin >> *hourIn;

//validate input for hourIn
if (*hourIn < 0 || *hourIn > 23)
{
cout << "invalid input. enter an integer between 0 and 23.";
}

//get the minute that the vehicle entered the garage.
cout << "\nMinute vehicle entered garage? ";
cin >> minIn;

//validate input for minIn.
if (minIn < 0 || minIn > 60)
{
cout << "invalid input. enter a number between 0 and 60.";
}

//get the hour vehicle exits garage.
cout << "\nHour vehicle exits garage? ";
cin >> *hourOut;

//validate input for hourOut.
if (*hourOut < 0 || *hourOut > 23)
{
cout << "invalid input. enter an integer between 0 and 23.";
}

//get the minute vehicle exits garage.
cout << "\nMinute vehicle exits garage?";
cin >> *minOut;

//validate input for minOut.
if (minOut < 0 || *minOut > 60)
{
cout << "invalid input. enter a number between 0 and 60.";

return;
}
}
//function definition for time.
void time(int hourIn, int minIn, int hourOut, int minOut, int hourParkTime, int *minParkime, int roundedTotal, int *rounded)
{
if (minOut < minIn)
{
minOut = minOut + 60;
hourOut = hourOut - 1;
}
else
{
hourParkTime = hourOut - hourIn;
minParkTime = minOut - minIn;
}
if (minParkTime >= 1)
{
rounds = hourParkTime + 1;
}
else
{
rounds = hourParkTime;
}
roundedTotal = rounds;
return;
}
//Function definition for rate.
void rate(char *vehicle, int units, float firstRate, float secondRate)
{
switch (*vehicle)
{
{
case 'c':
case 'C': units = 3;
firstRate = firstCarRate;
break;
}
{
case 't':
case 'T': units = 1;
firstRate = firstTruckRate;
secondRate = secondTruckRate;
break;
}
{
case 'b':
case 'B': units = 2;
firstRate = firstBusRate;
secondRate = secondBusRate;
}
return;
}
}
//Function definition for charge.
void charge(int roundedTotal, int units, float firtsRate, float secondRate, float totalCharges)
{
if (roundedTotal <= units)
{
totalCharge = (roundedTotal * firstRate);
}
else
{
totalCharge = (roundedTotal - units) * (secondRate)+(units * firstRate);

return;
}
}
//Function definition for print Bill.
void printBill(char vehicle, int hourIn, int minIn, int hourOut, int minOut, int hourParkTime, int minParkTime, int roundedTotal, float totalCharge)
{
cout << "Vehicle Type:/t/t" << vehicle << endl;
cout << "Time In:/t/t" << hourIn << ":" << minIn << endl;
cout << "Time Out:/t/t" << hourOut << ":" << minOut << endl;
cout << "Total Park Time:/t/t" << roundedTotal << endl;
cout << "Total Charge:/t/t" << totalCharge << endl;
return;
}



Here's a start... classes are your friend. Tabs in console are done with "\t", not "/t", and you don't need to write "return" at the end of a void function.

There are still have some logical errors:
- Entering 'C' for car prints the message twice because you tried to write it as a fall-through case but for some reason wrote the message twice. YOu want
1
2
3
4
case 'C':
case 'c':
    // stuff
    break;

- Entering in at 10:45, and the for leaving I chose 11th hour, second minute -- this produces an eventual output of "10:62", with a 0 total park time and 0 total charge.
- roundedTotal is off
- probably other issues, like handling bad user input. Currently if one function fails and returns, the next one would still be called (with potentially incomplete data stored in the object)


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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//This program calculates charges for a parking garage based on the type of
//vehicle and the time spent in the garage.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class GarageParking
{
public:

    //function definition for get info.
    void getInfo()
    {
        cout << "\nType of vehicle? ";
        cout << "\n(enter C for car,T for truck or B for bus).";
        cin >> vehicle;

        //check for valid vehicle type        
        switch (vehicle)
        {
            {
            case 'C':
                cout << "You entered C.\n";
            case 'c':
                cout << "You entered c.\n";
                break;
            }
            {
            case 'T':
                cout << "You entered T.\n";
            case 't':
                cout << "You entered t.\n";
                break;
            }
            {
            case 'B':
                cout << "You entered B.\n";
            case 'b':
                cout << "You entered b.\n";
                break;
            }
            cout << "You did not enter a valid vehicle type\n";
            cout << "Please Enter C,c,T,t,B, or b\n";
            return;
        }
        

        //get the hour that the vehicle entered the garage.
        cout << "\nHour vehicle entered garage? ";
        cin >> hourIn;

        //validate input for hourIn
        if (hourIn < 0 || hourIn > 23)
        {
            cout << "invalid input. enter an integer between 0 and 23.";
        }

        //get the minute that the vehicle entered the garage.
        cout << "\nMinute vehicle entered garage? ";
        cin >> minIn;

        //validate input for minIn.
        if (minIn < 0 || minIn > 60)
        {
            cout << "invalid input. enter a number between 0 and 60.";
        }

        //get the hour vehicle exits garage.
        cout << "\nHour vehicle exits garage? ";
        cin >> hourOut;

        //validate input for hourOut.
        if (hourOut < 0 || hourOut > 23)
        {
            cout << "invalid input. enter an integer between 0 and 23.";
        }

        //get the minute vehicle exits garage.
        cout << "\nMinute vehicle exits garage?";
        cin >> minOut;

        //validate input for minOut.
        if (minOut < 0 || minOut > 60)
        {
            cout << "invalid input. enter a number between 0 and 60.";
        }
    }
    //function definition for time.
    void time()
    {
        if (minOut < minIn)
        {
            minOut = minOut + 60;
            hourOut = hourOut - 1;
        }
        else
        {
            hourParkTime = hourOut - hourIn;
            minParkTime = minOut - minIn;
        }
        if (minParkTime >= 1)
        {
            rounds = hourParkTime + 1;
        }
        else
        {
            rounds = hourParkTime;
        }
        roundedTotal = rounds;
    }

    //Function definition for rate.
    void rate()
    {
        switch (vehicle)
        {
            {
            case 'c':
            case 'C':
                units = 3;
                firstRate = firstCarRate;
                break;
            }
            {
            case 't':
            case 'T':
                units = 1;
                firstRate = firstTruckRate;
                secondRate = secondTruckRate;
                break;
            }
            {
            case 'b':
            case 'B':
                units = 2;
                firstRate = firstBusRate;
                secondRate = secondBusRate;
            }
        }
    }
    //Function definition for charge.
    void charge()
    {
        if (roundedTotal <= units)
        {
            totalCharge = (roundedTotal * firstRate);
        }
        else
        {
            totalCharge = (roundedTotal - units) * (secondRate)+(units * firstRate);
        }
    }

    //Function definition for print Bill.
    void printBill()
    {
        cout << "Vehicle Type:\t\t" << vehicle << endl;
        cout << "Time In:\t\t" << hourIn << ":" << minIn << endl;
        cout << "Time Out:\t\t" << hourOut << ":" << minOut << endl;
        cout << "Total Park Time:\t\t" << roundedTotal << endl;
        cout << "Total Charge:\t\t" << totalCharge << endl;
    }

private:
    //constants for rates
    const double firstCarRate = 0.00; //first rate for cars.
    const double secondCarRate = 1.25; //second rate for cars.
    const double firstTruckRate = 3.75; //first rate for trucks.
    const double secondTruckRate = 4.50; //second rate for trucks.
    const double firstBusRate = 2.00; //first rate for buses.
    const double secondBusRate = 2.50; //second rate for buses.

    // variables
    char vehicle;
    int units;
    int hourIn;
    int minIn;
    int hourOut;
    int minOut;
    int hourParkTime;
    int minParkTime;
    int roundedTotal;
    int rounds;
    float firstRate;
    float secondRate;
    float totalCharge;
};

int main()
{
    GarageParking g;
    g.getInfo();
    g.time();
    g.rate();
    g.charge();
    g.printBill();

    return 0;
}
Last edited on
Thank you for the info. The problem is I haven't studied classes yet. I am trying to do it without it. Just using void ...ugh. Any ideas will be helpful, thank you !
well... this project screams "class!" to neatly remember relevant vars for the life of the object, and to not pollute the global namespace with those same variables.

If you absolutely must avoid classes/structs, you could
1. Place all the const and normal variables at the top, globally, similarly to what you had (avoid #define). Just know that it's bad practice, especially with so many, and a potential conflict with built-in function time().
2. Keep function signatures as void with no parameters, as I have.

OR

1. Place just the const variables at the top, globally.
2. Place the normal variables inside main().
3. Change functions to use C++-style references (don't try to write in C pointer style until you absolutely need to support a pointer interface). This means that the caller keeps ownership, in this case main(), which is what we want:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// #1 consts at the top here somewhere

void getInfo(char& vehicle, int& hourIn, int& minIn, int& hourOut, int& minOut)  // #3
{
    cin >> vehicle;
    // etc.  populate other vars.
}

int main()
{
    // Move all variables here as per second option, part #2
    char vehicle;
    int hourIn, minIn, hourOut, minOut;
    
    getInfo(vehicle, hourIn, minIn, hourOut, minOut);

    return 0;
}

An example of correct function pass by reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

//function declaration
void add2(int &);

int main() {
	int i;
	i = 3;

	add2(i); //function call

	cout << i; //the value of i increased by 2
	cin.get();
	return 0;
}

//function definition
void add2(int &myInt) {
	myInt += 2;
}
Topic archived. No new replies allowed.