Everyone has been so helpful... I hope someone can help me figure out what happened here...
I am trying to reverse the digits that the user inputs.. and it looked like i had it but for some reason it truncates the last digit and adds a zero???
here is whole program... in case you need it, please remember I am new at this and my code is probably very rudamentary... i only know what I have been taught...and even that is a stretch...
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
usingnamespace std;
int reverseIt(int);
int getData();
void rinse();
fstream outputfile;
void main()
{
int num=0, rev=0;
num = getData();
rev = reverseIt(num/10);
outputfile << rev << " is the reverse of " << num << endl;
outputfile.close();
rinse();
system("pause");
return;
}
int reverseIt(int NBR)
{
if(NBR!=0)
{
outputfile <<(NBR%10);
reverseIt(NBR/10);
}
else
{
return 0;
}
}
int getData()
{
outputfile.open("ReverseIt_result.txt", ios::app);
if (!outputfile)
{
cout<<"Error! Output File did not open!\n";
system("pause");
exit(0);
}
int nbr;
cout << "Enter a number" << endl;
cin >> nbr;
return nbr;
}
void rinse() // & repeat
{
char choice;
cout << "\nWould you like to calculate another? (Y or N)" << endl << endl;
cin >> choice;
if(choice=='y' || choice=='Y')
{
main();
}
else
{
cout << "Thanks and have a great day!" << endl << endl;
outputfile.close(); //don't go back to main to close the outfile... so i am closing it here.
system("pause");
exit(0);
}
}
your reverseit function always returns a 0 at the end.
change your declaration:
int reverseIt(int); to void reverseIt(int);
do some minor edition in your main function, see remarks.
void main()
{
int num=0; // rev=0; no longer needed
num = getData();
reverseIt(num); //you had rev = reverseIt(num/10);
outputfile << " is the reverse of " << num << endl; // you had outputfile << rev << " is the reverse of " << num << endl;
outputfile.close();
rinse();
system("pause");
return;
}
//the reverse function should be this:
void reverseIt(int NBR)
{
if(NBR/10!=0 ) // you had if(NBR!=0)
{
outputfile <<(NBR%10);
reverseIt(NBR/10);
}
else
{outputfile <<(NBR%10);} // you had return 0 that keeps adding 0 at the end
void main()
{
int num=0; // rev=0; no longer needed
num = getData();
reverseIt(num); //you had rev = reverseIt(num/10);
outputfile << " is the reverse of " << num << endl; // you had outputfile << rev << " is the reverse of " << num << endl;
outputfile.close();
rinse();
system("pause");
return;
}
void reverseIt(int NBR)
{
if(NBR/10!=0 ) // you had if(NBR!=0)
{
outputfile <<(NBR%10);
reverseIt(NBR/10);
}
else
{outputfile <<(NBR%10);} // you had return 0 that keeps adding 0 at the end
}
int getData()
{
outputfile.open("ReverseIt_result.txt", ios::app);
if (!outputfile)
{
cout<<"Error! Output File did not open!\n";
system("pause");
exit(0);
}
int nbr;
cout << "Enter a number" << endl;
cin >> nbr;
return nbr;
}
void rinse() // & repeat
{
char choice;
cout << "\nWould you like to calculate another? (Y or N)" << endl << endl;
cin >> choice;
if(choice=='y' || choice=='Y')
{
main();
}
else
{
cout << "Thanks and have a great day!" << endl << endl;
outputfile.close(); //don't go back to main to close the outfile... so i am closing it here.
system("pause");
exit(0);
}
@ jrfrago - ok.. its working... if you saw the eariler post w/new code...i only cout some of the data..rest went into the outfile... if not.. its works! thank you!!!! here is my new code - i removed the recursion of main() which solved another issue I had w/opening and closing the outputfile too much... =) thanks vlad for pointing that out...
It is a very bad code. First of all why is outputfile a global variable? Why do functions use this global variable? Function run calls itself recursively that can result in the program abort because the stack is a limited resource.
@vlad - i was taught to put the outputfile & input file below the function prototypes - which makes it global - and makes the file accessable by all functions in the program. we are only doing simple one or two part programs so this should be acceptable. I am at a loss to how the function run calls itself recursively - unless because rinse() calls it. This was how i got past having to open the file each and every time the user decided to do another...
I suppose i could have called each of the functions again in rinse... but then I couldn't use that code as a generic function in other programs...whcih is why it used to call main() - i had to modify it for this one cause of the multliple outputs...