Help appreciated - Pass by reference

Pages: 12
In the Input function I am supposed to update the variables MilesDriven and GalsUsed. It says to do it by using Pass-By-Reference. Then, after completing that the program will go back to the menu display.

Anyone have any ideas on what I could do here?

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
#include <iostream>
using namespace std;

const double TAXPERMILE = .05;
const double TAXPERGAL = .529;

void Input(double& MilesDriven, double& GalsUsed);
int CalcAndDisplay();

int main()
{
	double MilesDriven = 0;
	double GalsUsed = 0;
	double TotalTaxForFuel = 0;
	double TotalTaxForMilesDriven = 0;

	char loop = 'y';
	char menu;

	while (loop) == 'y'
	{
		cout << "Main Menu /n";
		cout << "(E)nter the miles driven and number of gals used. /n";
		cout << "(C)alculate the two tax rates and display them. /n";
		cout << "(Q)uit the program. /n"
		cout << "Enter a (E), (C) or (Q) only. /n";
		cin.get(menu);

		switch (menu)
		{
		case 'e':
		case 'E':
			Input();
			break;
		case 'c':
		case 'C':
			CalcAndDisplay();
			break;
		case 'q':
		case 'Q':
			loop = 'n';
			break;
		}
	}

	system("cls");
	return 0;
}

int CalcAndDisplay()
{
	double TotalTaxDrivingMiles = 0;
	double TotalTaxFuel = 0;

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	cout << "Enter the number of miles driven:" << endl;
	cin >> MilesDriven;
	cin.ignore();
	cout << "Enter the number of gallons used:" << endl;
	cin >> GalsUsed;
	cin.ignore();

	TotalTaxDrivingMiles = TAXPERMILE * MilesDriven;
	TotalTaxFuel = TAXPERGAL * GalsUsed;

	cout << "The total of tax per mile is= " << TotalTaxDrivingMiles << endl;
	cout << "The total of tax per gallon is= " << TotalTaxFuel << endl;
}



void Input(double &MilesDriven, double &GalsUsed)
Bool Invalid = false;
{
	do
	cout << "Enter the correct amount of miles driven: " << endl;
	cin >> MilesDriven;
	Invalid = cin.fail;
	
	if (Invalid == true);
	{
		cout << "" << endl;
		cin.clear()
		MilesDriven = 0;
	}
}
Nobody has an idea here?
closed account (D80DSL3A)
The call to Input() on line 33 requires 2 parameters to be passed.
Call as: Input(MilesDriven, GalsUsed);
Also add code to the Input function for input of GalsUsed. It currently only gets MilesDriven from the user.

What's with the orphaned 'do' on line 78? Did you want to make a do-while loop in the function?
Thanks for the help fun2code!

I will fix those things. Also, you are correct as the 'do' is going to be a do-while loop in the Input function! I am required to have the program loop back to the menu until the user asks to quit.
fun2code,

how do you think I should go about writing the Input function? That's really where I am having trouble.
closed account (D80DSL3A)
Take input for the 2nd value (as mentioned in 1st post) and complete the do-while loop.
Also, cin.clear() won't be enough to reset for a retry at reading values. You must also remove the offending input from the stream. Use also cin.ignore(256, '\n'); after cin.clear();.

There are many syntax errors in your current code for Input(). Try to fix some of them.
eg. Line 76 belongs inside the function.
Line 81 fail is a function, so call as cin.fail()
line 86 missing ; at the end.
do-while missing {} braces and the while at the end.

Please post back something for the Input function that compiles (or at least comes close).

EDIT: I keep seeing more errors! Lose the ; at the end of line 83 or lines 84-88 not associated with the if.
Last edited on
Okay, I will work on it!

Thank you so much for the feedback, I truly appreciate the help.
fun2code,

I think I've made some progress! I am getting one compile error from this program now and it is:

'Error 1 error C4716: 'CalcAndDisplay' : must return a value'

Not sure what I've done wrong here. Shouldn't it be executing the math in the function and outputting the new value?


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
#include <iostream>
using namespace std;

const double TAXPERMILE = .05;
const double TAXPERGAL = .529;

void Input(double& MilesDriven, double& GalsUsed);
int CalcAndDisplay();

int main()
{
	double MilesDriven = 0;
	double GalsUsed = 0;
	double TotalTaxForFuel = 0;
	double TotalTaxForMilesDriven = 0;

	char loop = 'y';
	char menu;

	while(loop == 'y')
	{
		cout << "Main Menu /n";
		cout << "(E)nter the miles driven and number of gals used. /n";
		cout << "(C)alculate the two tax rates and display them. /n";
		cout << "(Q)uit the program. /n";
		cout << "Enter a (E), (C) or (Q) only. /n";
		cin.get(menu);

		switch (menu)
		{
		case 'e':
		case 'E':
			Input(MilesDriven, GalsUsed);
			break;
		case 'c':
		case 'C':
			CalcAndDisplay();
			break;
		case 'q':
		case 'Q':
			loop = 'n';
			break;
		}
	}

	system("cls");
	return 0;
}

int CalcAndDisplay()
{
	double TotalTaxDrivingMiles = 0;
	double TotalTaxFuel = 0;
	double GalsUsed = 0;
	double MilesDriven = 0;

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	cout << "Enter the number of miles driven:" << endl;
	cin >> MilesDriven;
	cin.ignore();
	cout << "Enter the number of gallons used:" << endl;
	cin >> GalsUsed;
	cin.ignore();

	TotalTaxDrivingMiles = TAXPERMILE * MilesDriven;
	TotalTaxFuel = TAXPERGAL * GalsUsed;

	cout << "The total of tax per mile is= " << TotalTaxDrivingMiles << endl;
	cout << "The total of tax per gallon is= " << TotalTaxFuel << endl;
}



void Input(double &MilesDriven, double &GalsUsed)
{
	bool Invalid = false;
	do
	{
		cout << "Enter the amount of miles driven: " << endl;
		cin >> MilesDriven;
		Invalid = cin.fail();
		if (Invalid == true)
		{
			cout << "Please enter the correct amount of miles driven: " << endl;
			cin >> MilesDriven;
			cin.ignore();
			MilesDriven = 0;
			cout << "Please enter the amount of gallons used: " << endl;
			cin >> GalsUsed;
			cin.ignore();
			Invalid = cin.fail();
		}
	} while (Invalid == true);
}
closed account (D80DSL3A)
The error is because you've declared CalcAndDisplay returns an int, but there is no return in the function. Change return type to void.
Nice improvement on the Input function but the code may not work right yet.

I wrote some similar code. See if it fits your situation. You can run it in the shell here.
Click the little wheel like symbol at the upper right corner of the code.
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
#include<iostream>
using namespace std;

int main()
{
    double a,b;
    bool invalid = false;

    do
    {
        invalid = false;
        cout << "Enter a and b: ";
        cin >> a >> b;
        if( cin.fail() )
        {
            invalid = true;
            cin.clear();
            cin.ignore(256, '\n');
        }
    }while( invalid );

    cout << "valid a and b has been entered! a = " << a << " b = " << b;

    cout << endl;
    return 0;
}

Try entering letters for a and b. It should repeat until numbers are entered.

About the CalcAndDisplay function. Why prompt for MilesDriven and GallonsUsed again? Isn't collection of that data Input's job?
Pass the already collected values to CalcAndDisplay and just use them there.
Last edited on
I'm really excited, it's compiling now!

But, you are right. The input function isn't working correctly. I think I understand how to convert my code into the format you've provided above so I'm going to work on that!

Also I think I understand what you mean about CalcAndDisplay. Basically I can eliminate lines 61-66, correct? Because they are already declared in Input? Hopefully I'm understanding that correctly.



EDIT: I just tried deleting those lines in CalcAndDisplay and the function did something funky! It just spit out all 0's and went back to menu!
Last edited on
closed account (D80DSL3A)
For CalcAndDisplay move the variables from being locals to function arguments.
From:
1
2
3
4
5
6
7
8
void CalcAndDisplay()
{
	double TotalTaxDrivingMiles = 0;
	double TotalTaxFuel = 0;
	double GalsUsed = 0;
	double MilesDriven = 0;
        // .......
}

To:
1
2
3
4
5
6
void CalcAndDisplay(double GalsUsed, double MilesDriven)// use values from Input()
{
	double TotalTaxDrivingMiles = 0;
	double TotalTaxFuel = 0;
        // .......
}
Last edited on
fun2code,

I just tried moving the variable from being locals to function arguments and I got a couple compile errors. Not sure why.

I'm going to continue working on it. Could it because of something wrong in my Input function?
closed account (D80DSL3A)
Yes, you need to do a couple of other things for that change. Also modify the prototype before main and change how the function is called in the switch body. I was hoping you would see the need for those other changes.

For learning purposes, look at the errors and see if you can tell that's what they're telling you is wrong.
Last edited on
Okay I am currently working on it! I see for the Input function example that you did the do-while loop in main. Is that any different from mine? Can I just follow the template you've provided here but put it in the Input function?
I think I got it! What do you think?

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
#include <iostream>
using namespace std;

const double TAXPERMILE = .05;
const double TAXPERGAL = .529;

void Input(double& MilesDriven, double& GalsUsed);
void CalcAndDisplay(double& MilesDriven, double& GalsUsed);

int main()
{
	double MilesDriven = 0;
	double GalsUsed = 0;
	double TotalTaxForFuel = 0;
	double TotalTaxForMilesDriven = 0;

	char loop = 'y';
	char menu;

	while (loop == 'y')
	{
		cout << "Main Menu" << endl;
		cout << "(E)nter the miles driven and number of gals used." << endl;
		cout << "(C)alculate the two tax rates and display them." << endl;
		cout << "(Q)uit the program." << endl;
		cout << "Enter a (E), (C) or (Q) only." << endl;
		cin.get(menu);

		switch (menu)
		{
		case 'e':
		case 'E':
			Input(MilesDriven, GalsUsed);
			break;
		case 'c':
		case 'C':
			CalcAndDisplay(MilesDriven, GalsUsed);
			break;
		case 'q':
		case 'Q':
			loop = 'n';
			break;
		}
	}

	system("cls"); 
	return 0;
}

void CalcAndDisplay(double& MilesDriven, double &GalsUsed)
{
	double TotalTaxDrivingMiles = 0;
	double TotalTaxFuel = 0;
	
	cout << "Enter the number of miles driven:" << endl;
	cin >> MilesDriven;
	cin.ignore();
	cout << "Enter the number of gallons used:" << endl;
	cin >> GalsUsed;
	cin.ignore();

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	TotalTaxDrivingMiles = TAXPERMILE * MilesDriven;
	TotalTaxFuel = TAXPERGAL * GalsUsed;

	cout << "The total of tax per mile is= " << TotalTaxDrivingMiles << endl;
	cout << "The total of tax per gallon is= " << TotalTaxFuel << endl;
}



void Input(double &MilesDriven, double &GalsUsed)
{
	bool invalid = false;
	
	do
	{
		invalid = false;
		cout << "Enter the amount of miles driven and gallons used: " << endl;
		cin >> MilesDriven >> GalsUsed;
		if( cin.fail() )
		{
			invalid = true;
			cin.clear();
			cin.ignore();
		}
	} while( invalid );

	cout << "The values for miles driven and gallons of fuel used has been entered! Miles driven = " << MilesDriven << "Gallons of fuel used = " << GalsUsed << endl;

	system("pause");
}
fun2code,

Let me know what you think!

And thank you so much for all the help you've provided for me. Appreciated so, so much.
closed account (D80DSL3A)
That looks better but you're still taking input in the calcAndDisplay function. I'm thinking remove the code lines55-69. The function is being passed those values.

The only thing odd about the program is that the user could select option C the 1st time, which would leave MilesDriven=0 and GalsUsed=0 from their initial values. I'm not sure how to work around this little problem. Perhaps call Input() once before entering the while loop?

You're welcome for the help.
Last edited on
I could remove all the code from 55-69? What about the math to calculate TotalTaxDrivingMiles and TotalTaxFuel?


Also yes I noticed that issue too. It's as if the (E) option is pointless. Hmmmm.
closed account (D80DSL3A)
Oops! I meant 55-60.
The (E) option is needed if user doesn't select (Q). He would want to enter new numbers.
Slightly tricky problem. Calling Input once before the loop seems like the easiest solution to guaranteeing numbers are entered before (C) can be selected.
Ohhhh that's right I didn't think about that for (E). Okay.


If I remove 55-60, won't I get compile errors? I'm going to play with that now.

Also you are saying call Input() in main before the while loop that contains the menu?
Pages: 12