Functions, reversing integers

I need to write a program to display a 4 integer number in reverse order, using a function.

The program displays an error when running the while loop. Need helping figuring out what is wrong with the program. I am terrible with functions.
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
  #include <iostream>
using namespace std;

void reverse(int num);

int main()
{
	int num;
	int reverse_num;

	cout << "Enter a number to reverse: " << endl; 
	cin >> num;
	cout << rev_num << endl;
	return; 

}

void reverse(int number);
	int rev_num = 0;

	while (number > 0)
	{
	rev_num += num - ((num / 10) * 10);

	num /= 10;
	
	if (num > 0);
		
	rev_num *= 10;

		}
	}
	return rev_num;
	
Last edited on
Please watch thenewboston on youtube. He (bucky) Has fantastic tutorials on all the basics of c++ - https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83

There are multiple very basic errors. You need to learn the basics.

For example - void reverse(int number); This should be an int function, and there should not be a semicolon. And after the function there needs to be brackets.

[code cout << rev_num << endl;][/code] Here you print nothing. you never tell the program what rev_num is. You have to call for the reverse function and then print it out in main.

1
2
reverse_sum = reverse(num);
cout << reverse_sum << endl;
Last edited on
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
#include <iostream>
using namespace std;

int reverse(int num);

int main()
{
	int num, rev_num, reverse_sum;
	cout << "Enter a number to reverse: " << endl; 
	cin >> num;
	reverse_sum = reverse(rev_num);
	cout << reverse_sum << endl;
	return 0;

}

int reverse(int rev_num)
{
	int num;

	while (num > 0)
	{
	rev_num += num - ((num / 10) * 10);

	num /= 10;
	
	if (num > 0);
		
	rev_num *= 10;

	return rev_num;
		}
	}
	

So I changed the function, and added
1
2
reverse_sum = reverse(num);
cout << reverse_sum << endl;
but it doesn't run. Are some variables in the wrong places?
Last edited on
Could you please provide the error codes?
cin >> num; ? You dont have anything called num. You mean number not num.

reverse_sum = reverse(rev_num); You want to send it number, not rev_num. Like this -

reverse_sum = reverse(number);
Warning 1 warning C4101: 'reverse_num' : unreferenced local variable
Warning 2 warning C4390: ';' : empty controlled statement found; is this the intent?
Warning 3 warning C4715: 'reverse' : not all control paths return a value
Error 4 error C4700: uninitialized local variable 'number' used

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

int reverse(int reverse_num);

int main()
{
	int num, reverse_num, reverse_sum;
	cout << "Enter a number to reverse: " << endl; 
	cin >> num;
	reverse_sum = reverse(num);
	cout << reverse_sum << endl;
	return 0;

}

int reverse(int reverse_num)
{
	int num;

	while (num > 0)
	{
	reverse_num += num - ((num / 10) * 10);

	num /= 10;
	
	if (num > 0);
		
	reverse_num *= 10;

	return reverse_num;
		}
	}
	
Please make the changes to the code I provided you. Also

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int reverse(int reverse_num)
{
	int num;

	while (num > 0)
	{
	reverse_num += num - ((num / 10) * 10);

	num /= 10;
	
	if (num > 0);
		
	reverse_num *= 10;

	return reverse_num;
		}
	}


There is some weird stuff going on in there.

1
2
3
4
int num;

	while (num > 0)
	{


You literally just declared num. Num is not greater than 0 because you never do anything with num.

int reverse(int reverse_num) This is the function you take in from main. You want to use this one.

since I dont know how this reverse thing works, Im not 100% but. I think in the reverse function, you want to switch all the "num" with reverse:num. and switch all the "reverse_num" with "num".
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
#include <iostream>
using namespace std;

int reverse(int num);

int main()
{
	int num, reverse_sum;
	cout << "Enter a number to reverse: " << endl; 
	cin >> num;
	reverse_sum = reverse(number);
	cout << reverse_sum << endl;
	return 0;

}

int reverse(int num)
{
	int num;

	while (num> 0)
	{
	num += num- ((num / 10) * 10);

	num /= 10;
	
	if (num > 0)
		
	num *= 10;

	return num;
		}
	}
	


So how do I fix the while loop?

These are the only two errors that show up now:

Warning 1 warning C4715: 'reverse' : not all control paths return a value
Error 2 error C4700: uninitialized local variable 'number' used
1
2
cin >> num;
	reverse_sum = reverse(number);


Look here. The user enters his thing in num. Why do you send away number? number is nothing, number literally does not exist. change it to
1
2
3
4
5
6
7
8
9
10
11
12
13
 reverse_sum = reverse(num);[code]

the return num; should be outside of the while-loop.

[code]
if (num > 0)
		
	num *= 10;

	
		}
return num;
	}


And, As I also said earlier... so I will just quote myself -

since I dont know how this reverse thing works, Im not 100% but. I think in the reverse function, you want to switch all the "num" with reverse:num. and switch all the "reverse_num" with "num".



Please watch Buckys tutorials on youtube - https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83

They are a great help.
Last edited on
Topic archived. No new replies allowed.