Hexadecimal String Recursion function HELP!

Hello guys! This is my first time posting in this site! I've only been lurking around, but anyways! I am having a lot of trouble getting this dumb function to work! I am really running out of options so please help me and tell me what is wrong with my function! :( I keep getting stack overflow error messages...Anyways, here is my program, again I am only having trouble with my final function which is the HexString function. Basically we have to return base numbers as Hexadecimal strings.


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
106
107
108
109
110
111
112
113
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;


//Function prototypes
string BinaryString( int);

string HexString(int);




//Main function
int main(){
const int SIZE = 64; //Amount of numbers in the array
int arr[SIZE];

cout<<"DEC			HEX				Bin"<<endl;
cout<<"---			---				---"<<endl;



for(int x = 0; x < SIZE; x++)//For loop to print all 63 numbers
{
	
	cout<<left<<setw(12) << x <<endl; 
	  cout<<right<<setw(58) <<  BinaryString(x)<<endl; //x becomes binary here
	  cout<<right<<setw(26) << HexString(x)<<endl;//x becomes hex here

}	







	
	
	
	
	
	system("pause");
	return 0;
}

string BinaryString( int x )//Converts decimals to binary!
{
	if (x==0)
	{
		return "0";
	}
	else if (x == 1)
	{
		return "1";
	}
	else
	{
		return BinaryString(x/2) + BinaryString(x%2);
	}
}
string HexString(int x)//Converts decimals to hex!
{
	string result ;
	string remainder;
	int num = x%16;	
	result = HexString(x/16) + HexString(num);
	
		




if (x == 0)
	{
		
		return "0";
	}
	


else if(result == remainder)
{
	

		switch (num)
		{
		case 10: remainder = "A";  break;
		case 11: remainder = "B";  break;
		case 12: remainder = "C";  break;
		case 13: remainder = "D";  break;
		case 14: remainder = "E";  break;
		case 15: remainder = "F";  break;
		default: remainder;  break;
								
	
		}	
}
else
{

		return result;
}
	
	
	


}

//END PROGRAM! 
Last edited on
When one writes a recursive function, you should make sure they have appropriate base cases. Your HexString function calls itself (twice) unconditionally, so of course you're going to run out of stack. Line 76 cannot be reached.
Understood, should the base cases be the same from my BinaryString function? So I have to call the function only at the end just like in my previous function? Also what exactly should be put in my default inside my switch statement?
Thanks for responding!
Oh man I feel like Ia m close to the answer now!!!!! But I still need to fix the switch statement, now I don't get the stack overflow, instead I get output but I get different symbols for HexString and no numbers just the letters from the switch statement and occassionally symbols. What exactly must I fix in this switch statement? I am sort of running out of ideas...

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
string HexString(int x)//Converts decimals to hex!
{
	
	string remainder;
	int num = x%16;	

	
		




if (x == 0)
	{
		
		return "0";
	}
	


else if( x == 1)
{
	return "1";
}
else
{
	
		switch (num)
		{
		case 10:return remainder = "A"; break;
		case 11: return remainder = "B"; break;
		case 12: return remainder = "C"; break;
		case 13: return remainder = "D"; break;
		case 14:return remainder = "E"; break;
		case 15: return remainder = "F"; break;
		default: return remainder = (x/16) + (x%16);break;
								
	
		}	
}





}

//END PROGRAM! 
Last edited on
Topic archived. No new replies allowed.