Storing and converting bases using recursion and exception handling.

Hey guys I have an assignment due in 3 days(Wednesday). The program is supposed to take a number from any base and be able to display it in any base as well.The methods of conversion between bases MUST BE RECURSIVE. I know there are some functions out there that let you do this easily, anyhow my professor is strictly asking for recursive methods. I have managed to make a class for each base (decimal, binary, octal, hexadecimal) and I'm storing the number in a string in order to validate it (which means that somehow I have to turn that string into an integer and I don't know how. I was trying to use the strtoll function to convert a string into a long long int but I don't know how to do this since the num doesn't exist at compile time so I guess I have to do some magic here that I still don't quite understand ). Now I need to print out all bases with that number using a recursive method. I want to start on the decimal number class, converting from decimal to: octal, hexadecimal and binary. Since binary is the easiest I want to precisely start here.

So after getting through the constructor I have a string but I need that value in an integer.

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
#include "decType.h"



decType::decType(string base)
{
	num = "";
	outNum = 0;
	numCh[decMax_chars] = { 0 };

	size_t one_t = 1;
	size_t zero_t = 0;

	try
	{
		size_t last_pos = base.size() - one_t;
		size_t dec_max_chars = 18;

		if (last_pos > dec_max_chars)
		{
			int error1 = 1;
			throw error1;
		}
		else
		{
			int error2 = 2;
			char dec = 'D';

			if (base.find_first_not_of("1234567890") == last_pos && last_pos != zero_t)
			{
				if (base[last_pos] == dec)
				{
					num = base.substr(zero_t, last_pos);
				}
				else
				{
					throw error2;
				}
			}
			else
			{
			
				throw error2;
			}
		}
	}
	catch (int error)
	{
		switch (error)
		{
		case 1:
			cout << endl << "Max number of characters of decType declaration exceeds 18 digits." << endl;
			cout << "Terminate program. ERROR 1" << endl;
			terminate();
			break;
		case 2:
			cout << "error initializing decType, check declaration line" << endl;
			cout << "string input to decType is not legal syntax for a decimal base number" << endl;
			cout << "decimal input format(numberD)" << endl;
			cout << "Terminate program. ERROR 2" << endl;
			terminate();
			break;
		}
	}
}

string decType::toHex()
{
	num = "";
	return num;
}
string decType::toDec()
{
	return num;
}
string decType::toOct()
{
	num = "";
	return num;
}
string decType::toBin()
{
	num = "";
//convert the string to long long int here so I can then convert to binary and output the value...
	return num;
}


I have been breaking my head for 5 days now, It was time to ask for help...
Last edited on
I'd fancy working on the code, but since you are in a hurry... What about the first of these methods:
http://stackoverflow.com/questions/10375922/base-number-converter-from-10-base-with-recursive-function-c
?
I'm asking for a way to convert the string into a long long int.

Enoizat, I appreciate your answer but I am asking for guidance, not estimates on how I emotionally feel towards the project.

Back to the subject at hand. I know how to convert a defined string to a long long int but I don't know how to do it here because the string is not defined at compile time (since it has to go through the constructor first). Can someone shine a light here? here's some more code. UNType is the base class for decType, hexType, binType, octType. I am working on decType. Any suggestions are very much appreciated.

//UNType.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
#include "string"
#include "iostream"
#include "stdlib.h" //I think i'll need this for using stroll function

using namespace std;

class UNType
{
public:
	string virtual toHex() = 0;
	string virtual toDec() = 0;
	string virtual toOct() = 0;
	string virtual toBin() = 0;
protected:
	string num;
};


//decType.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include "UNType.h"

const int decMax_chars = 18;

class decType :
	public UNType
{
public:
	decType(string);

	string toHex();
	string toDec();
	string toOct();
	string toBin();

	long long int outNum;
	char numCh[decMax_chars];
};


//decType.cpp is on the post

//source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "UNType.h"
#include "hexType.h"
#include "decType.h"
#include "octType.h"
#include "binType.h"

int main()
{
	decType d("1234567891011D");
//     decType dError1("81234567891011222222D"); // causes error
//	decType dError2("123456789101112131H"); // causes error

	cout << d.toDec() << endl; //displays decimal number 

	system("pause");

	return 0;
}

> convert a defined string to a long long int

http://en.cppreference.com/w/cpp/string/basic_string/stol

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

int main()
{
    const std::string hex_number = "f5d42" ;
    std::cout << std::stoll( hex_number, nullptr, 16 ) << " decimal\n" ;

    const std::string base_23_number = "3dh9k" ;
    std::cout << std::stoll( base_23_number, nullptr, 23 ) << " decimal\n" ;
}
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
#include <iostream>
#include <cctype>
#include <string>
using namespace std;


//==========================================
// Integer to char conversion and vice versa
//==========================================

char symbol( int i ) 
{
   if ( i < 10 ) return (char)( '0' + i      );
   else          return (char)( 'A' + i - 10 );

}

int lobmys( char c )
{
   if ( isdigit( c ) ) return               c   - '0';
   else                return 10 + toupper( c ) - 'A';
}


//===================
// Recursion routines
//===================

string intToBase( int n, int base ) {  return n  > 0  ? intToBase       ( n / base                   , base ) + symbol( n % base      ) : ""; }

int BaseToInt( string s, int base ) {  return s != "" ? base * BaseToInt( s.substr( 0, s.size() - 1 ), base ) + lobmys( s[s.size()-1] ) : 0 ; }


//=========
// Checking
//=========

int main()
{
   int n, base;
   string s;

   cout << "Convert a decimal integer to an arbitrary base\n";
   cout << "Enter a positive integer: ";   cin >> n   ;
   cout << "Enter a base (2-16): "     ;   cin >> base;
   cout << n << " in base " << base << " is " << intToBase( n, base ) << endl;

   cout << "\n\n";

   cout << "Convert a string in an arbitrary base to a decimal integer\n";
   cout << "Enter a string: "          ;   cin >> s;
   cout << "Enter a base (2-16): "     ;   cin >> base;
   cout << s << " in base " << base << " is " << BaseToInt( s, base ) << " in base 10" << endl;;
}


Convert a decimal integer to an arbitrary base
Enter a positive integer: 255
Enter a base (2-16): 16
255 in base 16 is FF


Convert a string in an arbitrary base to a decimal integer
Enter a string: FF
Enter a base (2-16): 16
FF in base 16 is 255 in base 10
Big thank you to lastchance!!! I made the whole thing :D.
Topic archived. No new replies allowed.