Reverse Numbers help

I am trying to use a function to reverse numbers (ex: 123 to 321, -0065 to -56), I am having problems trying to convert numbers such as 7008 to 8007, anyone have any pointers?

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
#include <iostream>
#include <cmath>

using namespace std;

int reverseNumber (int userNumber);

int main()
{
    int userNum;
    int reversedNumber;
    
    cout << "Enter a number, and I will reverse the number" << endl;
    cin >> userNum;
    
    cout << "The reversed number is: ";
    
    reversedNumber = reverseNumber(userNum);
    
    cout << reversedNumber;
    
}

int reverseNumber (int userNumber)
{
    int tenToPower;
    int reversedNumber;
    
    double power = 0;
    
    if (userNumber == 0)
    {
        return userNumber;
    }//end of if statement
    else
    {
        while (userNumber / static_cast<int> (pow (10, power)) >= 10 || userNumber / static_cast<int> (pow (10, power)) <= -10)
        {
            power++;
        }// end of first while
        
        while (userNumber >= 1 || userNumber <= -1)
        {
            tenToPower = static_cast<int> (pow (10, power));
            
            if (userNumber % 10 != 0)
            {
                reversedNumber = (userNumber % 10) * tenToPower + reversedNumber;
                userNumber = userNumber / 10;
                power = power - 1;
            }
        }//end of second while
        
    }//end of else statement
    
    return reversedNumber;
}// end of function reverseNumber 
Last edited on
I'm sorry about not getting the code to look correct... I don't know how to do it on here
close the code bracket (00)
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
#include <iostream>
#include <cmath>

using namespace std;

int reverseNumber (int userNumber);

int main()
{
    int userNum;
    
    cout << "Enter a number, and I will reverse the number" << endl;
    cin >> userNum;
    
    cout << "The reversed number is: "
         << reverseNumber(userNum) << endl;
}

int reverseNumber (int userNumber)
{
    int tenToPower;
    int reversedNumber;
    
    double power = 0;
    
    if (userNumber == 0)
    {
        return userNumber;
    }//end of if statement
    else
    {
        while (userNumber / static_cast<int> (pow (10, power)) >= 10 || userNumber / static_cast<int> (pow (10, power)) <= -10)
        {
            power++;
        }// end of first while
        
        while (userNumber >= 10 || userNumber <= -10)
        {
            tenToPower = static_cast<int> (pow (10, power));
            
            if (userNumber % 10 != 0)
            {
                reversedNumber = (userNumber % 10) * tenToPower + reversedNumber;
                power = power - 1;
            }
        }//end of second while
        
    }//end of else statement
    
    return reversedNumber;
}// end of function reverseNumber 
what do you mean by reverse? Is it like:
user enters 2356 and you output 6532?
Ok, I'm sorry about that. This is my code though, and when it asks for the input, and I enter a number, say 123, it does absolutely nothing. Also if I were to get rid of the endline; after the call, then I get an error saying that I should have ; after I try calling the function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int_to_string(int number) {
	string result;
	stringstream str;
	str << number;
	result = str.str();
	return result;
}

reverse_string(string line) {
	string::reverse_iterator it;
	string result;
	for (it = line.rbegin(); it < line.rend(); it++) {
		result += *it;
	}
	return result;
}
Last edited on
OK NEVERMIND GUYS!!! lol My brain got divine intervention and I figured it out. Thanks for the help and patience, though. I'm sorry for any confusion.
Topic archived. No new replies allowed.