recursion problems continue...

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???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  int reverseIt(int NBR)
{
	if(NBR!=0)
	{
		outputfile <<(NBR%10);
		reverseIt(NBR/10);
	}
	else
	{	
		
		
		return 0;
	
	}
	
	
}


if you input 12345
it spits out 43210

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace 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);
	}

}




1
2
3
4
5
6
7
void reverse( unsigned int x, std::ostream &os = std::cout )
{
   const unsigned int base = 10;

   os << x % base;
   if ( x /= base ) reverse( x, os );
}
Last edited on
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

}
Last edited on
@jfrago
though I can follow your code - it doesn't work - it actually seems to truncate even more...
@vlad
your code is too advanced for me - I don't understand what you have coded.

i figured out os is the file name..

and that you set base to 10

but your if statment??? what does /= mean?

and why are you pulling in 2 sources into the funtion?
Expression

x /= base;

is equivalent to

x = x / base;

os is not a file name. It is an output stream. For example you could use the function the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
	
	reverse( nbr ); 
	reverse( nbr, outputfile ); 

	return nbr;

}


Of course this and other your functions should be rewritten because it is a bad design of the program.
By the way in C++ function main may not be called recursively.

1
2
3
4
5
	if(choice=='y' || choice=='Y') 

	{
		main(); 
	}
it works perfectly

here is the entire code, you may just copy and paste.
your code is not bad all, it just needs some modifications.

#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;

void reverseIt(int);
int getData();
void rinse();

fstream outputfile;


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);
}

}
Last edited on
it works...
just be mindful that int signed: is only from this range -2147483648 to 2147483647

the program should be as simple as that for a beginner.
he needs help in his code, not create a completely different code.
Last edited on
it works for me...
copy the entire code I posted, and make sure you only enter within the range range -2147483648 to 2147483647.
Last edited on
@ 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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;


void reverseIt(int);
int getData();
void run();
void rinse();


fstream outputfile;


void main()
{
	outputfile.open("ReverseIt_result.txt", ios::app);
	if (!outputfile) 
		{
		cout<<"Error! Output File did not open!\n";
		system("pause");
		exit(0);	
		}

	run();
	outputfile.close();

	system("pause");
	return;
}


void run()
{
	int num=0, rev=0;

	num = getData();
	reverseIt(num);
	outputfile << " is the reverse of " << num << endl;	
	rinse();
	return;
}

void reverseIt(int NBR)
{
	if(NBR/10!=0)
	{
		outputfile <<(NBR%10);
		reverseIt(NBR/10);
	}
	else
	{	
		outputfile << (NBR%10);	
	}
	return;
	
}
	
	


int getData()

{
	
	int nbr;

	outputfile << "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') 

	{
		run(); 
	}

	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);
	}

}



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.
Oh, another function run()....
The last code you posted works.
yes it is a bad code but he's a beginner. He'll figure that out as he progresses...
@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...

but I am new at this... so I am learning.
here is a shorter code for the reverse of an integer:

1
2
3
4
5
6
7
8
int reverseInt(int myInt) {
	int reverse = 0;
	do {
		reverse=reverse*10 + myInt%10;
		myInt=myInt/10;
	} while (myInt/10>0 || myInt%10>0);
	return reverse;
}
Last edited on
1
2
3
4
5
6
7
long BackwardNum(long num)
{
  int t = log10(num);
  if (t == 0)//base case
    return num;
  return (((num%10) * pows_n(10, t)) + BackwardNum(num/10));
}
@Smac89 Is pows_n a fucntion from a standard library? or you have separate code for it?
Last edited on
Seperate code
Topic archived. No new replies allowed.