Thread: Reversing Digits Project (Help Please)

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    15

    Reversing Digits Project (Help Please)

    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

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    31
    so you're saying, given a number (say 1234) you want it to be returned last digit first and first last (4321)?

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    yeah, its supposed to reverse the digits of a number
    ex. 3454 becomes 4543

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I would convert the int value into a string, then just print the string out in reverse.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    we haven't done strings yet in class.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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;

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    31
    Oops, I meant to add-

    Why not itoa, reverse then atoi? Or I think you could use a bitset.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817

    Wink

    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;
    [edit]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]
    Last edited by hk_mp5kpdw; 07-12-2005 at 12:35 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    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

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reversing digits in C
    By Digital Thought in forum C Programming
    Replies: 22
    Last Post: 10-15-2010, 06:34 AM
  2. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  4. reversing digits
    By lizardking3 in forum C++ Programming
    Replies: 15
    Last Post: 03-28-2003, 12:30 PM