# Reversing Digits Project (Help Please)

• 07-12-2005
fenixataris182
for my introduction to c++ class i was given an assignment to create a program that reverses the digits of a number. The class is online so it's hard for the instructor to help me. I've been waiting for three days for a reply. I need some help on this. Here is what i have. What am i doing wrong? how should i change it? Nothing too advanced here, since it is an introduction class.
Code:

```#include <iostream> using std::cout; using std::cin; using std::endl; #include <iomanip> int reverseDigits (int d, int n, int m); int main() {   int number;   cout << "Enter a number: ";   cin >> number;   cout << "The number with its digits reversed is: "         << reverseDigits         << endl;   return 0; } // end main // function reverseDigits definition int reverseDigits (int d, int n, int m) {         int reverse;         while ( n > 0 ) {         ( d = n % 10 )                         ( m = m * 10 + d )                         ( n = n / 10 );                         reverse = m;                         return reverse;   } // end while   } // end function reverseDigits```
• 07-12-2005
Ichmael™
so you're saying, given a number (say 1234) you want it to be returned last digit first and first last (4321)?
• 07-12-2005
fenixataris182
yeah, its supposed to reverse the digits of a number
ex. 3454 becomes 4543
• 07-12-2005
mitakeet
I would convert the int value into a string, then just print the string out in reverse.
• 07-12-2005
fenixataris182
we haven't done strings yet in class.
• 07-12-2005
Daved
Code:

`( d = n % 10 )`
What are the parentheses for? These should just be statements that end in a semi-colon.

In your reverseDigits function, you return reverse inside the while loop, so the while loop runs only once. You should probably return the result outside the loop.

Your reverseDigits function takes three integers, what are they for? The variable n is for the number you want to reverse, but d and m are just temporary variables. They should be declared inside of the function instead of as parameters, and they should be initialized along with reverse to 0.

When you call reverseDigits from main, you don't pass any arguments. You have to call it and pass the number you read in from the user. Since you should probably change the reverseDigits function to take only the number to reverse, you could call it like this:
Code:

``` cout << "The number with its digits reversed is: " << reverseDigits(number) << endl;```
• 07-12-2005
Ichmael™

Why not itoa, reverse then atoi? Or I think you could use a bitset.
• 07-12-2005
hk_mp5kpdw
Code:

```int reverseDigits (int d, int n, int m) {     int reverse;     while ( n > 0 ) {         ( d = n % 10 )         ( m = m * 10 + d )         ( n = n / 10 );         reverse = m;         return reverse;   } // end while   } // end function reverseDigits```
Looks like you're close with the basic algorithm but need work on how to actually write (and call) a function. Try this instead:

Code:

```int ReverseDigits(int value) {     int result = 0;     while( value > 0 )     {         result = result*10 + value%10;         value /= 10;     }     return result; }```
To call it you would just have:

Code:

```int number; cout << "Enter a number: "; cin >> number; cout << "The number with its digits reversed is: "     << ReverseDigits(number)     << endl;```
Of course that only works with positive numbers. If you expect the user to input negative values you'll have to find a way to modify things.[/edit]
• 07-12-2005
fenixataris182
I took your advice, but i still get errors. Can I please get some more help. I really appreciate it.
Code:

```#include <iostream> using std::cout; using std::cin; using std::endl; #include <iomanip> int reverseDigits (int value); int reverse; int main() {   int number;   cout << "Enter a number: ";   cin >> number;   cout << "The number with its digits reversed is: "         << reverseDigits ( number )         << endl;   return 0; } // end main // function reverseDigits definition int ReverseDigits(int value) {     int result = 0;     while( value > 0 )         {         result = result*10 + value%10;         value /= 10;     return result;   } // end while   } // end function reverseDigits```
• 07-12-2005
Daved
>> In your reverseDigits function, you return reverse inside the while loop, so the while loop runs only once. You should probably return the result outside the loop.

You didn't fix that.

You also have a typo, it should be reverseDigits, not ReverseDigits.