Hi, I am writing this code for a class I am taking. It works fine as long as i do not use more 9 digits in length, but my teacher wants the user to be able to be able to input a number of any length. The warning when i build it is " : warning C4715: 'reverse' : not all control paths return a value". Any help would be appreciated. :)

#include <iostream>
using namespace std;

int reverse(int num);


int main()
{
	cout << "Enter the number you wish to reverse: ";
	int number;
	cin >> number;
	cout << reverse(number) << endl;
}

int reverse(int num)
{
	do
	{
		if (num >= 10) 
		{
			cout << (num % 10);
		}
		else
		{
			return num;
		}
		num = num / 10;
	}
	while (num > 0);
}
if (num >= 10) 
		{
			cout << (num % 10);

		}

There's no return statement, that's why you get warning. Soon or later your num value will be smaller than 10 and your program will

return num

, but the compiler doesn't know about it, so it complies.

The if-else is redundant;
A number which is smaller than 10, when undergoes modulus operator with divisor as 10 will yeild that number itself.
7%10=7
1%10=1 and so on...

Your reverse function is not designed well:
You are trying to print the reversed value of the number in the function itself, hence the function should not return anything.
If at all you want to return the reversed of the number(without printing it), you will have to think of doing something more!
One easiest way would be like this:

Algorithm R (to reverse a integer N and store it in s):
R1(initialisation): Let s=0, i=(number of digit in original number N) -1.
R2(check): Is N>0? If No, GOTO R6. If yes, proceed to R3
R3(extract last): Extract the last digit of N and place it in d.
R4(Add): Add d*10^i to s
R5(Loop): N=N/10; i=i-1
R6(end): STOP, s is the reversed integer.

in your reverse function, what happens if num is less than 0?
What would the function return?

Get the input as a string and print the string backwards. Makes sure the string contains numeric data if you want.

Edited 7 Years Ago by firstPerson: n/a

Ok, I reworked the function, but it will still not give the correct output if the number is larger than 9 digits. For example if i enter 1234567895 it returns 1692687025...any ideas? Thanks.

#include <iostream>
using namespace std;

int reverse(int num);


int main()
{
	cout << "Enter the number you wish to reverse: ";
	int number;
	cin >> number;
	cout << number << " reversed is " << reverse(number) << endl;		
}

int reverse(int num)
{
	int store = 0;
	int returnValue = 0;
	while ((num > 0) || (num < 0))
	{
		
		store = num % 10;
		returnValue *= 10;
		returnValue += store;
		num /= 10;

	}
	
	return returnValue;


}

but my teacher wants the user to be able to be able to input a number of [B]any [/B]length

That means that you shouldn't use int, double , long long or whatever.
It means you should get your input as a string in order to have any length.
Primitive data types have limits on the max and min range of number
they can represent, but my teacher wants the user to be able to
be able to input a number of any length"]More Info on that.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

but my teacher wants the user to be able to be able to input a number of any length

Yeah, its a very ambigious requirement. Ask them to clarify.

This question has already been answered. Start a new discussion instead.