Inheritance, printing conversion from decimal to other bases

I am writing a program in which I have class 'number' with derived class to convert to binary, octal and hex. I need number to hold the single integer input and have one pure virtual function member print_it(). My code is a mess, I have a million errors. Can anyone look at this an suggest how I might begin to clean it up? Thanks so much in advance! (If you could respond asap so I can keep working tht would be great! Often, I've changed the code a lot by the time I get a response :0)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include<iostream>
using namespace std;

const int STACK_SIZE = 100;

class stack {
	private:
		int count;			// number of items in the stack
		int data[STACK_SIZE];
	public:
		stack();
		~stack();
		void push(const int item);	// push an item on the stack
		int pop(void);			// pop item off the stack
	};
stack::stack()	// constructor
{
	count = 0;	// zero the stack
}
stack::~stack() {}	// default destructor
void stack::push(const int item)
{
	if (count < STACK_SIZE)
	{
		data[count] = item;
		++count;
	}
	else cout << "Overflow!\n";
}
int stack::pop(void)
{
	if (count >0)
	{
		--count;
		return (data[count]);
	}
	else
	{
		cout << "Underflow!\n";
		return 0;
	}
}
int menu();
//void toBinary();
//void toOctal();
//void toHex();

class number 
{
	int num;
public:
	virtual void print_it()=0;
};

class toBinary:public number
{
public:
	int num;
	int total = 0;
	stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!!
	int ctr=0; // declare a local counter!!!!!!!!!!!!!!!
	cout << "Please enter a decimal: ";
	cin >> num;
	cout << "The decimal number " << num << " converts to the binary number: ";
	while(num > 0)
		{
			total = num % 2;
			num /= 2;
			//cout << total << " ";
			reverse.push(total); // save to stack instead of printing!!!!!!!!!
			ctr++; // count the number of digits saved!!!!!!!!!!!!
		}
	while (ctr > 0)
             {
				cout << reverse.pop() << " ";
				ctr--;
	         }
};

class toOctal: public number
{
public:
	void toOctal()
	int num;
	int total = 0;
	stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!!
	int ctr=0; // declare a local counter!!!!!!!!!!!!!!!
	cout << "Please enter a decimal: ";
	cin >> num;
	cout << "The decimal number " << num << " converts to the octal number: ";
	while(num > 0)
		{
			total = num % 8;
			num /= 8;
			//cout << total << " ";
			reverse.push(total); // save to stack instead of printing!!!!!!!!!
			ctr++; // count the number of digits saved!!!!!!!!!!!!
		}
	while (ctr > 0)
             {
				cout << reverse.pop() << " ";
				ctr--;
	         }
};

class toHex:public number
{
		public void toHex()
    {
	int num,counter,x,a,hex[100];
	cout<<"Please enter a decimal: ";
	cin>>num;
	cout<<"\nThe Decimal number "<<num<<" converts to the Hexadecimal number:  ";
	for(counter=0;num!=0;counter++)
		{
			a=num%16;
			hex[counter]=a;
			num=num/16;
			}
	for(x=counter-1;x>=0;x--)
		{
			if(hex[x]==10)
				{
					cout<<"A";
					}
			else if(hex[x]==11)
				{
					cout<<"B";
					}
			else if(hex[x]==12)
				{
					cout<<"C";
					}
			else if(hex[x]==13)
				{
					cout<<"D";
					}
			else if(hex[x]==14)
				{
					cout<<"E";
					}
			else if(hex[x]==15)
				{
					cout<<"F";
					}
			else
				{
					cout<<hex[x];
					}
			}
	}
}

int main()
{
		int choice = menu();
		switch(choice)
		{
		case (0):
			toBinary();
			break;
		case (1):
			toOctal();
			break;
		case(2):
			toHex();
			break;
		}
		return 0;
}
number * Show;
Show = new toBinary(num);
Show->print_it();
delete Show;

Show = new toOctal(num);
Show->print_it();
delete Show;

Show = new toHex(num);
Show->print_it();
delete Show;

int menu()
{
	int choice;
	cout << " *****Menu***** " << endl;
	cout << "Convert the number from decimal into: " << endl;
	cout << "0-Binary" << endl;
	cout << "1-Octal" << endl;
	cout << "2-Hexadecimal" << endl;
	cin >> choice;
	return choice;
}
Looking at the code, you have a _lot_ of syntactical errors. Why don't you start by looking at the first compile error
(which looks like it should be line 59), fix that one, recompile, and repeat until all errors are gone?
Thanks for the reply. ya, I kow I had a LOT of errors. I have improved it quite a bit but I'm still getting errors. I think most of them are because I am not declaring my variables in the right places. I am temped to make everything public, but I know that's just bad code. Can you help? Here's the code and my errors:
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

#include<iostream>
using namespace std;

const int STACK_SIZE = 100;

class stack {
	private:
		int count;			// number of items in the stack
		int data[STACK_SIZE];
	public:
		stack();
		~stack();
		void push(const int item);	// push an item on the stack
		int pop(void);			// pop item off the stack
	};
stack::stack()	// constructor
{
	count = 0;	// zero the stack
}
stack::~stack() {}	// default destructor
void stack::push(const int item)
{
	if (count < STACK_SIZE)
	{
		data[count] = item;
		++count;
	}
	else cout << "Overflow!\n";
}
int stack::pop(void)
{
	if (count >0)
	{
		--count;
		return (data[count]);
	}
	else
	{
		cout << "Underflow!\n";
		return 0;
	}
}
int menu();


class number 
{
	public:
		number() {x=0;}
		number (int num){x=num;}
		virtual ~number() {}
		virtual void print_it()=0;
		int num;
	private:
	int x;
};

class toBinary:public number
{
public:
	
	int num, total, ctr;
	void print_it()
	{
	
	cout << "Please enter a decimal: ";  
	cin >> num;
	cout << "The decimal number " << num << " converts to the binary number: ";
	while(x > 0)
		{
			total = num % 2;
			num /= 2;
			cout << total << " ";
			reverse.push(total); // save to stack instead of printing!!!!!!!!!
			ctr++; // count the number of digits saved!!!!!!!!!!!!
		}
	while (ctr > 0)
             {
				cout << reverse.pop() << " ";
				ctr--;
	         }
	}
};

class toOctal: public number
{
public:
	void print_it()
	{
	cout << "Please enter a decimal: ";
	cin >> num;
	cout << "The decimal number " << num << " converts to the octal number: ";
	while(num > 0)
		{
			total = num % 8;
			num /= 8;
			cout << total << " ";
			reverse.push(total); // save to stack instead of printing!!!!!!!!!
			ctr++; // count the number of digits saved!!!!!!!!!!!!
		}
	while (ctr > 0)
             {
				cout << reverse.pop() << " ";
				ctr--;
	         }
	}
};

class toHex:public number
{
	void print_it()
	{
   		
	int num,counter,x,a,hex[100];
	cout<<"Please enter a decimal: ";
	cin>>num;
	cout<<"\nThe Decimal number "<<num<<" converts to the Hexadecimal number:  ";
	for(counter=0;num!=0;counter++)
		{
			a=num%16;
			hex[counter]=a;
			num=num/16;
		}
	for(x=counter-1;x>=0;x--)
	{
		if (hex[x] > 9)
		cout << char('A'+hex[x]-10);
	else
		cout << hex[x];
	}
	cout << endl;
	}
};

int main()
{
		int choice = menu();
		switch(choice)
		{
		case (0):
			toBinary();
			break;
		case (1):
			toOctal();
			break;
		case(2):
			toHex();
			break;
		}
		return 0;
}
number * Show;
Show = new toBinary(num);
Show->print_it();
delete Show;

Show = new toOctal(num);
Show->print_it();
delete Show;

Show = new toHex(num);
Show->print_it();
delete Show;

int menu()
{
	int choice;
	cout << " *****Menu***** " << endl;
	cout << "Convert the number from decimal into: " << endl;
	cout << "0-Binary" << endl;
	cout << "1-Octal" << endl;
	cout << "2-Hexadecimal" << endl;
	cin >> choice;
	return choice;
}

Errors:
1>------ Build started: Project: Assignment_10, Configuration: Debug Win32 ------
1>Compiling...
1>Assignment_10.cpp
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(72) : error C2248: 'number::x' : cannot access private member declared in class 'number'
1> c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(58) : see declaration of 'number::x'
1> c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(50) : see declaration of 'number'
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(77) : error C2065: 'reverse' : undeclared identifier
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(77) : error C2228: left of '.push' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(82) : error C2065: 'reverse' : undeclared identifier
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(82) : error C2228: left of '.pop' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(98) : error C2065: 'total' : undeclared identifier
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(100) : error C2065: 'total' : undeclared identifier
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(101) : error C2065: 'reverse' : undeclared identifier
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(101) : error C2228: left of '.push' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(101) : error C2065: 'total' : undeclared identifier
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(102) : error C2065: 'ctr' : undeclared identifier
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(104) : error C2065: 'ctr' : undeclared identifier
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(104) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://c:\Users\Laura\Desktop\Assignment_10\Assignment_10\Debug\BuildLog.htm"
1>Assignment_10 - 13 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
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
int dec_to_oct(int i)
{
	int result = 0;
	int n = 0;
	int first_number = 0;

	if (i < 8)
		return i;

	while (i / 8){
		first_number = i % 8;
		i /= 8;
		n++;
		if (n > 1){
			int temp = 0;
			temp = n - 1;
			while (temp--)
				first_number *= 10;
			first_number += result;
		}
		else
			result = first_number;
		result = first_number;
	}
	while (n--)
		i *= 10;

	i += first_number;

	return i;
}


Better still. Only 3 errors all the same: *EDIT* it compiles and runs if I remove (num) from here- but then I'm not getting the right output and I'm stuck in a loop...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
number * Show;
Show = new toBinary(num);
Show->print_it();
delete Show;

Show = new toOctal(num);
Show->print_it();
delete Show;

Show = new toHex(num);
Show->print_it();
delete Show;
return 0;
}

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

#include<iostream>
using namespace std;

const int STACK_SIZE = 100;

class stack {
	private:
		int count;			// number of items in the stack
		int data[STACK_SIZE];
	public:
		stack();
		~stack();
		void push(const int item);	// push an item on the stack
		int pop(void);			// pop item off the stack
	};
stack::stack()	// constructor
{
	count = 0;	// zero the stack
}
stack::~stack() {}	// default destructor
void stack::push(const int item)
{
	if (count < STACK_SIZE)
	{
		data[count] = item;
		++count;
	}
	else cout << "Overflow!\n";
}
int stack::pop(void)
{
	if (count >0)
	{
		--count;
		return (data[count]);
	}
	else
	{
		cout << "Underflow!\n";
		return 0;
	}
}
int menu();


class number 
{
	public:
		number() {x=0;}
		number (int num){x=num;}
		virtual ~number() {}
		virtual void print_it()=0;
		int num;
	private:
	int x;
};

class toBinary:public number
{
public:
		void print_it()
				{
				int num;
				int total = 0;
				stack reverse; 
				int ctr=0;
				cout << "Please enter a decimal: ";  
				cin >> num;
				cout << "The decimal number " << num << " converts to the binary number: ";
					while(num > 0)
						 {
							total = num % 2;
							num /= 2;
			                cout << total << " ";
			                reverse.push(total); // save to stack instead of printing!!!!!!!!!
			                ctr++; // count the number of digits saved!!!!!!!!!!!!
		                 }
					while (ctr > 0)
					     {
							cout << reverse.pop() << " ";
							ctr--;
						 }
				}
};

class toOctal: public number
{
public:
	void print_it()
	{
		int num;
		int total = 0;
		stack reverse; 
		int ctr=0;
			cout << "Please enter a decimal: ";
			cin >> num;
			cout << "The decimal number " << num << " converts to the octal number: ";
			while(num > 0)
				{
					total = num % 8;
					num /= 8;
					cout << total << " ";
					reverse.push(total); // save to stack instead of printing!!!!!!!!!
					ctr++; // count the number of digits saved!!!!!!!!!!!!
				}
			while (ctr > 0)
			    {
					cout << reverse.pop() << " ";
					ctr--;
				}
	}
};

class toHex:public number
{
	void print_it()
	{
   		
	int num,counter,x,a,hex[100];
	cout<<"Please enter a decimal: ";
	cin>>num;
	cout<<"\nThe Decimal number "<<num<<" converts to the Hexadecimal number:  ";
	for(counter=0;num!=0;counter++)
		{
			a=num%16;
			hex[counter]=a;
			num=num/16;
		}
	for(x=counter-1;x>=0;x--)
	{
		if (hex[x] > 9)
		cout << char('A'+hex[x]-10);
	else
		cout << hex[x];
	}
	cout << endl;
	}
};

int main()

{
		int choice = menu();
		switch(choice)
		{
		case (0):
			toBinary();
			break;
		case (1):
			toOctal();
			break;
		case(2):
			toHex();
			break;
		}
		
number * Show;
Show = new toBinary();
Show->print_it();
delete Show;

Show = new toOctal(num);
Show->print_it();
delete Show;

Show = new toHex(num);
Show->print_it();
delete Show;
return 0;
}

int menu()
{
	int choice;
	cout << " *****Menu***** " << endl;
	cout << "Convert the number from decimal into: " << endl;
	cout << "0-Binary" << endl;
	cout << "1-Octal" << endl;
	cout << "2-Hexadecimal" << endl;
	cin >> choice;
	return choice;
}

Errors:
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(165) : error C2065: 'num' : undeclared identifier
1>c:\users\laura\desktop\assignment_10\assignment_10\assignment_10.cpp(169) : error C2065: 'num' : undeclared identifier
1>Build log was saved at "file://c:\Users\Laura\Desktop\Assignment_10\Assignment_10\Debug\BuildLog.htm"
1>Assignment_10 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Where do I declare num to make this work? Thanks much!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int oct_dec(int i)
{
	int result = 0;
	int n = 0;
	int number = 0;

	if (i < 10)
		return i;

	while (i / 10) {
		number = i % 10;
		result += (number * powl(8, n));
		n++;
		i /= 10;
	}
	result += (i * powl(8, n));
	return result;
}
firix-thanks for replying, but can you explain your response? I'm not even trying to convert oct to dec???
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
int oct_dec(int i)
{
	int result = 0;
	int n = 0;
	int number = 0;

	if (i < 10)
		return i;

	while (i / 10) {
		number = i % 10;
		result += (number * powl(8, n));
		n++;
		i /= 10;
	}
	result += (i * powl(8, n));
	return result;
}

int main()
{

	cout << oct << 1884 << endl;  // 1884 ->>> 3534 convert 

	cout << dec << endl; // Print as decimal
	
	cout << oct_dec(3534) << endl;   // Converting from octal to decimal

	return 0;		
}
Got it. If anyone is interested:
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

#include<iostream>
using namespace std;

const int STACK_SIZE = 100;

class stack {
	private:
		int count;			// number of items in the stack
		int data[STACK_SIZE];
	public:
		stack();
		~stack();
		void push(const int item);	// push an item on the stack
		int pop(void);			// pop item off the stack
	};
stack::stack()	// constructor
{
	count = 0;	// zero the stack
}
stack::~stack() {}	// default destructor
void stack::push(const int item)
{
	if (count < STACK_SIZE)
	{
		data[count] = item;
		++count;
	}
	else cout << "Overflow!\n";
}
int stack::pop(void)
{
	if (count >0)
	{
		--count;
		return (data[count]);
	}
	else
	{
		cout << "Underflow!\n";
		return 0;
	}
}
int menu();

class number 
{
	public:
		number() {x=0;}
		number (int num){x=num;}
		virtual ~number() {}
		virtual void print_it()=0;
		int num;
	private:
	int x;
};

class toBinary:public number
{
public:
		void print_it()
				{
				int num;
				int total = 0;
				stack reverse; 
				int ctr=0;
				cout << "Please enter a decimal: ";  
				cin >> num;
				cout << "The decimal number " << num << " converts to the binary number: ";
					while(num > 0)
						 {
							total = num % 2;
							num /= 2;
			                reverse.push(total); // save to stack instead of printing!!!!!!!!!
			                ctr++; // count the number of digits saved!!!!!!!!!!!!
		                 }
					while (ctr > 0)
					     {
							cout << reverse.pop() << " ";
							ctr--;
						 }
				}
};

class toOctal: public number
{
	void print_it()
	{
		int num;
		int total = 0;
		stack reverse; 
		int ctr=0;
			cout << "Please enter a decimal: ";
			cin >> num;
			cout << "The decimal number " << num << " converts to the octal number: ";
			while(num > 0)
				{
					total = num % 8;
					num /= 8;
					reverse.push(total); // save to stack instead of printing!!!!!!!!!
					ctr++; // count the number of digits saved!!!!!!!!!!!!
				}
			while (ctr > 0)
			    {
					cout << reverse.pop() << " ";
					ctr--;
				}
	}
};

class toHex:public number
{
	void print_it()
	{
   	int num,counter,x,a,hex[100];
	cout<<"Please enter a decimal: ";
	cin>>num;
	cout<<"\nThe Decimal number "<<num<<" converts to the Hexadecimal number:  ";
	for(counter=0;num!=0;counter++)
		{
			a=num%16;
			hex[counter]=a;
			num=num/16;
		}
	for(x=counter-1;x>=0;x--)
	{
		if (hex[x] > 9)
		cout << char('A'+hex[x]-10);
	else
		cout << hex[x];
	}
	cout << endl;
	}
};

int main()

{
	number * Show;
		int choice = menu();
		switch(choice)
		{
		case (0): 
			Show = new toBinary();
			Show->print_it();
			delete Show;
			break;
		case (1):
			Show = new toOctal();
			Show->print_it();
			delete Show;
			break;
		case(2):
			Show = new toHex();
			Show->print_it();
			delete Show;		
			break;
		}
return 0;
}

int menu()
{
	int choice;
	cout << " *****Menu***** " << endl;
	cout << "Convert the number from decimal into: " << endl;
	cout << "0-Binary" << endl;
	cout << "1-Octal" << endl;
	cout << "2-Hexadecimal" << endl;
	cin >> choice;
	return choice;
}
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
int *dec_binary(int i)
{

	int n = 0;
	int k = 1;
	int *p = NULL;
	while (i / 2) {
		k++;
		p  = (int *)realloc(p, sizeof(int) * k);
		p[n++] = i % 2;
		i /= 2;
	}

	p[n++] = i;
	p[n] = 9;


	for (int k  = 0; k < n / 2; k++) {
		int temp  = p[k];
		p[k] = p[n - k -1];
		p[n -k -1] = temp;
	}

	return p;
		
}
Topic archived. No new replies allowed.