having an issue getting a value from a void function into the parameter of another function

I'm having issues with compiling a program for an assignment. You can see what I'm trying to do from the code but it's essentially typing in a number between 1-5 and having it print an approval/interest rate.

The assignment calls that we use a void function for getting the input of the score and another void function for printing the decision. I also have to use a reference paramter in the getScore function. If it wasn't for that it'd be ridiculously easy.

I've watched a ton of videos, googled everything I could think of and obviously checked my book. It's good in that I'm learning a lot but in the end my program won't work.

My issue is with the getScore function. From what I gather, void functions don't return values and I've tried to declare the "input" variable in main, at the beginning of the program and in the getScore function. I don't think reference parameters actually store a value but I've been having a lot of trouble with truly understanding parameters and reference parameters in general. But in the end I know my big issue is getting that value that the user inputs into main so that I can get it into the parameter of the printDecision function.

If you guys have any advice on how I should tackle the problem or give me something that makes me slap my head in relevation I would be extremely grateful.

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

void getScore(int &input);
void printDecision(int score);

//////////////////////////////////////////////////////////////

int main()
{

getScore();

printDecision(input);

return 0;
}

///////////////////////////////////////////////////////////////

void getScore(int &input)
{

cout << "Enter the client's mortage score: " << endl;
cin >> input;


}

///////////////////////////////////////////////////////////////

void printDecision(int score)
{
if (score == 5)
{cout <<"You are approved with a 5% interest rate"<<endl;}
else if (score == 4)
{cout <<"You are approved with a 6% interest rate"<<endl;}
else if(score == 3)
{cout <<"You are approved with a 7% interest rate"<<endl;}
else if(score == 2)
{cout <<"You are not approved"<<endl;}
else if (score == 1)
{cout <<"You are not approved"<<endl;}
else if(6 < score)
{cout <<" Please enter a number between 1 and 5" << endl;}
else if(score < 1)
{cout <<" Please enter a number between 1 and 5" << endl;}

}
if you call your print decision function inside of your get score function it should work, as long as you pass the "input" variable into it. Have you tried this? if you did it this way, you shouldn't be required to call it in your main function.
alternatively, you could create a second variable and set it e equal to the address of the input variable, then pass that into your decision function.
1
2
3
4
5
6
7
8
9

///////////////////////////////////////////////////////////////
 void getScore(int &input) { cout << "Enter the client's mortage score: " << endl; 
cin >> input; } 
printDecision(variable);
//////////////////////////////////////////////////////////////
 void printDecision(int score) 
{ if (score == 5) {cout <<"You are approved with a 5% interest rate"<<endl;} else if (score == 4) {cout <<"You are approved with a 6% interest rate"<<endl;} else if(score == 3) {cout <<"You are approved with a 7% interest rate"<<endl;} else if(score == 2) {cout <<"You are not approved"<<endl;} else if (score == 1) {cout <<"You are not approved"<<endl;} else if(6 < score) {cout <<" Please enter a number between 1 and 5" << endl;} else if(score < 1) {cout <<" Please enter a number between 1 and 5" << endl;} }
Last edited on
Thanks, but I forgot to mention that the value of getScore has to be brought back into main. I'm working on your second suggestion right now to see if I can get that to work.
I've been having a lot of trouble with truly understanding parameters and reference parameters in general.


I'll go over these basics briefly. For now, I will not discuss the difference between parameters and arguments, because at this point it might unnecessarly confuse you.

When you declare a variable, such as int score, space gets reserved somewhere in memory to hold the value of score.
You can think of memory as being a long list of sequentially placed slots to fill with values. Each "slot" has a unique address - It's kind of like house numbers. This means, that when you declare int score, score has a unique address in memory from which its value is modified or read.

When you pass a variable to a function, typically, you pass "by-value", which means a copy of the variable you've passed (parameter) is created, and the copy exists for the lifetime of the function. The original variable and it's copy both have different, unique addresses in memory, as you might expect.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

void function(int param) {
	param = 20;
}

int main() {
	int a = 10;
	function(a);
	std::cout << a << std::endl;
	std::cin.get();
	return 0;
}


In the code above, you have a function which takes one integer parameter, and assigns the value 20 to it. In your main() function, you have int a initialized with a value of 10. You call the previously mentioned function, and provide a as a parameter. Then you print a.
What gets printed? Ten or twenty?
"10" is printed, because we passed a by-value. A copy of a was made, and the copy was assigned a value of 20, while the original a remained unaltered.

In order to prevent the function from creating a copy of the original parameter, and instead, modify the original parameter, we need to pass by-reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

void function(int& param) {
	param = 20;
}

int main() {
	int a = 10;
	function(a);
	std::cout << a << std::endl;
	std::cin.get();
	return 0;
}


Note the ampersand(&) on line 3. It's the only change that has been introduced to the previous code. The function no longer creates a copy, and modifies the original variable. As a result, "20" is printed instead of "10".

In this case, int& param is a reference, because it refers to the address of a.
Last edited on
Thank you for laying it out for me. I actually have the understanding of it but just the way you laid out that example for me kind of gave me that snap moment of clarity and I was able to get the program to work.

So even though the assignment is done, I really want to know what exactly happened. On line 13 where getScore is located I put "x" in the parenthesis. Does that kind of mean that the value inputted in the getScore funtion (line 22) was returned through that "portal"?

I nowhere in the program put x = input which logically seems like a bridge I would have needed to build at some point.

This is the edit that I did

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

void getScore(int &input);
void printDecision(int score);


//////////////////////////////////////////////////////////////

int main()
{
int x;
getScore(x);
cout <<  x << endl;
printDecision(x);

return 0;
}

///////////////////////////////////////////////////////////////

void getScore(int &input)
{
cout << "Enter the client's mortage score: " << endl;
cin >> input;
}

///////////////////////////////////////////////////////////////

void printDecision(int score)
{
if (score == 5)
{cout <<"You are approved with a 5% interest rate"<<endl;}
else if (score == 4)
{cout <<"You are approved with a 6% interest rate"<<endl;}
else if(score == 3)
{cout <<"You are approved with a 7% interest rate"<<endl;}
else if(score == 2)
{cout <<"You are not approved"<<endl;}
else if (score == 1)
{cout <<"You are not approved"<<endl;}
else if(6 < score)
{cout <<" Please enter a number between 1 and 5" << endl;}
else if(score < 1)
{cout <<" Please enter a number between 1 and 5" << endl;}

}
On line 13 where getScore is located I put "x" in the parenthesis. Does that kind of mean that the value inputted in the getScore funtion (line 22) was returned through that "portal"?

I nowhere in the program put x = input which logically seems like a bridge I would have needed to build at some point.


Yes. In your getScore() function, you take an integer reference as a parameter. For the lifetime of the getScore() function, that reference will be known as "input". When you modify the reference of a variable, you are actually modifying the variable. The reference, by itself, only acts as a medium to access and operate on a different variable which already exists somewhere else.
Yes. In your getScore() function, you take an integer reference as a parameter. For the lifetime of the getScore() function, that reference will be known as "input". When you modify the reference of a variable, you are actually modifying the variable. The reference, by itself, only acts as a medium to access and operate on a different variable which already exists somewhere else.


So just so I get 100% understanding/clarification. The value of "input" is now stored in the parenthesis of getScore () ? And anywhere I put getScore () in the program will contain "input"?
Just to make explaining easier, I've rewritten your getScore() function below.

1
2
3
4
void getScore(int& input) {
	std::cout << "Enter the client's mortgage score: ";
	std::cin >> input;
}


"input" is the name(identifier) you gave the reference. It's a reference to an integer. The only place that "input" can be accessed is in the getScore() function, because it has been declared in the function's local scope.

I'm not quite sure what you mean... here's an example of what is perfectly valid.

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

void getScore(int& input) {
	std::cout << "Enter the client's mortgage score: ";
	std::cin >> input;
}

int main() {

	int first_score;
	int second_score;

	getScore(first_score);
	getScore(second_score);

	std::cout << "The first score is:\t" << first_score << std::endl;
	std::cout << "The second score is:\t" << second_score << std::endl;

	std::cin.sync();
	std::cin.get();
	return 0;
}


You could say that getScore() is a function which modifies values that exist somewhere else, in this case, in the scope of your main() function. It does this by accepting one reference parameter. When you call getScore() in main, and give it a parameter like so getScore(first_score);, the reference parameter is now a reference to first_score. Thus, any changes that you make to the reference, will affect the referenced variable (in this case first_score).
Then, right after, you call the getScore() function again, only this time, you're passing second_score by-reference.
Last edited on
Topic archived. No new replies allowed.