Reversing digits

I wrote a program and spent a lot of time to figure out what is wrong with it and have had no luck. It is probably something simple and in my efforts im sure I have messed it up even more.

The assignment is to write a function to reverse the digits of a positive integer number. So if I entered 1234, the reverse would be 4321. The input should be the keyboard and the output should be written to a file. The main function must ask for the integer number, pass the number to the function, and print the returned value (the reverse) and original number to the output file.

** I have all of this written. The problem is it will read in a number but will not write to a file. The ms-dos window comes up, i enter the number to be reversed, and nothing happens. Something is wrong and I am just not seeing it. The code is below:

Main File CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "stdafx.h"
#include "2_2header.h"
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream wfile;
    wfile.open("2_2reverse.txt");
    int num, reversenum = 0;
    cout << "Please enter a positive number: ";
    cin >> num;
    reverse(num);
    wfile << "\nReverse:  " << reverse(reversenum) << "\n\n";
    wfile << "Original: " << num << "\n";
            
system("pause");
return 0;
}


Header File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int reverse(int num);
//
int reverse(int num)
{
    int digit= 0;
    int reversenum = 0; 
    
    if(num == 0)
    {
        return 0;
    }
    else
    {
        while(num > 0)
        {
            digit = num % 10;
            reversenum = digit /10;
            reversenum = reversenum * 10 + digit;
        }
        return reverse(reversenum);
    }
}


Last edited on
Please don't double post.
deleted, and I wont anymore.
I can`t see anything wrong with your reverse function but there again I`m half blind!
You could use :
1
2
getline(std::cin, word); // get input like 12345
and    reverse(word.begin(), word.end());
//to produce 54321
To convert the string to a number you could use a function like atoi().
Now write the result to file which I can`t help with.
Last edited on
closed account (D80DSL3A)
The reverse function looks like it may not be working OK and you are passing the wrong number to it on line 15 of Main File CPP.
reversenum = 0.
Try using reversenum to catch the return value from the call to reverse(num) on line 14.
Then you can cout the value to see if it's right.
Also, you forgot to close the file after writing to it. Not sure if this would cause the file output failure though.
Is the file empty? non-existent?

Maybe this code will help:
1
2
3
4
5
reversenum = reverse(num);// find reversenum
wfile << "\nReverse:  " << reversenum << "\n\n";// write reversenum to file
wfile << "Original: " << num << "\n";
wfile.close();
cout << "Reversed: " << reversenum << endl;// so you can see the result in the console 

EDIT: writing reversenum to the console will allow you to see if the reverse() works right, separating that from the file output issue.
Last edited on
Do you know how to use your debugger? If so, use it and run through your program. The reverse function has a few things wrong with it, and you don't seem to be using it correctly in main, but stepping through it should help you see the immediate problems. :)

If you don't know how to use your debugger, start with looking at the loop in your reverse function.

Edit: Sorry if I'm being overly vague, I just don't want to provide a complete answer because that would just be detrimental to your learning.
Last edited on
Topic archived. No new replies allowed.