Pass by reference, wont accept my 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
//Requires: n >= 0
//Modifies: nothing
//Effects: returns the int formed by reversing the digits of n
//  a parameter of 123 will return 321
//  a parameter of 1234 will return 4321
//  a parameter of 1230 will return 321  (or 0321)
//   NOTE:  0321 will print as 321 
int reverseNumber(int n)
{
	{
		int a = 1;
		int b = 1;
		int y = 1;
		int x = 0;
		while(b != 0)
		{
			a = n % 10;
			x = x * 10 + a;
			b = n / 10;
			n = b;
		}
		return x;
	}


}


and

1
2
3
4
5
6
7
8
9
10
//Requires: n >= 0;
//Modifies:  n
//Effects: alters n to be the integer formed by reversing its digits
//         if n = 123, then it will be altered to be 321
//         if n = 1230, then it will be altered to be 0321, i.e., 321
void reverseNum(int &n)
{
	reverseNumber(n);
}


Now this is my issue:

1
2
3
4
5
6
int main ()
{

	int n = reverseNum(12);
	cout << n;
}


The 12 has a red underline and won't compile. Compiler says 1>i:\project3\project3\test1.cpp(16): error C2664: 'reverseNum' : cannot convert parameter 1 from 'int' to 'int &'

And the red underline says "initial value of reference to non-const must be lvalue"

Can someone walk me through solving my issue?

Thanks
Last edited on
reverseNum's parameter isn't const-correct.
You cannot bind a temporary to a non-const reference.
I kind of get what you mean. How would we fix that though?

My reverseNumber passed the test, but reverseNum did not.


*******************************************
***** *****
***** Your output is: *****
***** *****
*******************************************
1
12
12345
10
105
1000



*******************************************
***** *****
***** Correct Output *****
***** *****
*******************************************
1
21
54321
1
501
1

So for some reason my reverseNum is not getting the reversed value.
Last edited on
basically what he said would look like this in your main:
1
2
3
4
5
6
7
int main()
{
     int n = 12;
     int x = reverseNum(n);
     cout << "x = " << endl;
     cout << "n = " << endl;
}


try that and see what it tells you.
What would I have to change in the function to get those results? They are testing my functions, I never submit an int main() to my autograder.

My issue revolves around how
1
2
3
4
5
6
7
int main ()
{

	int a = reverseNum(12);
	cout << a;
	return 0;
}


The 12 is underlined in red and says ""initial value of reference to non-const must be lvalue""
Last edited on
I'm pretty sure it's because you're passing a constant as a reference. Obviously you can't change the value of the number 12 (at least easily anyhow xD). Try this first:
1
2
3
4
5
6
int main () {
        int n = 12;
	int a = reverseNum(n);
	cout << a;
	return 0;
}
Last edited on
That won't work either because the reverseNum function is void.

1
2
3
4
5
6
7
8
9
//Requires: n >= 0;
//Modifies:  n
//Effects: alters n to be the integer formed by reversing its digits
//         if n = 123, then it will be altered to be 321
//         if n = 1230, then it will be altered to be 0321, i.e., 321
void reverseNum(int &n)
{
	reverseNumber(n);
}


And we can't change that.

When I did
1
2
3
4
5
6
7
int main ()
{
    int n = 12;
	reverseNum(n);
	cout << n;
	return 0;
}


12 came out, not 21.
Last edited on
Then how about

1
2
3
4
5
6
7
int main ()
{
    int n = 12;
    reverseNum(n);
    cout << n;
    return 0;
}

Note that n will still remain unchanged because all you're doing is passing it into reverseNumber by value. You will have to modify reverseNum (or reverseNumber) for n to change.
Last edited on
What would I have to modify here:

1
2
3
4
5
6
7
8
9
//Requires: n >= 0;
//Modifies:  n
//Effects: alters n to be the integer formed by reversing its digits
//         if n = 123, then it will be altered to be 321
//         if n = 1230, then it will be altered to be 0321, i.e., 321
void reverseNum(int &n)
{
	reverseNumber(n);
}


For n to change? I am even more confused now. The reverseNumber function works 100%, it has passed in my autograder. But reverseNum just gives back the number without it being reversed.

Simply this:

1
2
3
4
5
6
7
void reverseNum(int &n)
{
    //assigning the value of n to that returned from reverseNumber
    //n will be passed in by value (so it won't change by parameter passing)
    //however, we modify it with the assignment.
    n = reverseNumber(n);
}
Last edited on
Yes that will work, or you could just pass n as a reference to the reverseNumber function as well.
(but then you would have to modify reverseNumber such that n stores the reversed number at the end)
Yeah, that worked well, thank you shacktar. Its annoying how simple solutions can be in c++ some times.
Topic archived. No new replies allowed.