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:
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.
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.