octal conversion help

Good afternoon all, I need some help with my code here. The goal of my program is to have the user input a number (decimal) and either want a binary or octal conversion. As of now I have the binary conversion done. I have no clue how to convert to octal. I figured using a switch was the best way to go about this too. But I keep having my default message outputed after each loop. How do I have my program loop "properly"? Any help would be great! Also my program does compile, except for when selecting octal it'll only display "Test."

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
#include <iostream>
using namespace std;


int main()
{
	//I entered the array output and necessary
	//components separately away from the other variables.
	//Just to be distinguishable.
	int bArray[16] = { 0 };
	int k = 0;

	int decimal = 0;
	double binary = 0;
	double octal = 0;
	int number;

	cout << "Enter 'B' to convert a decimal number to binary and 'O' for an octal conversion." 
		<< "Hit space to quit program." << endl;

	while ((number = cin.get()) != ' ')
	{
		cout << "\nEnter a decimal number to be converted: " << endl;

		switch (number)
		{
		case 'B':
		case 'b':
			cin >> decimal;
			while ((decimal != 0) & (k <= 15))
			{
				bArray[k] = decimal % 2;
				decimal = decimal / 2;
				k++;
			}
			//printing the array backwards to display correct binary.
			for (k = 15; k >= 0; k--)
			{
				cout << bArray[k];
				if ((k % 4) == 0)
					cout << " ";
			}
			break;

		case 'O':
		case 'o':
			//This next line is just a test. I'll need to put in the octal conversion here.
			cout << "Test" << endl;
			break;

		default:
			//This code here continues to output even if my cases are successfully compiled.
			cout << "Wrong decimal input." << "Enter a new number." << endl;
			break;
		}
	}

	cout << "Thank you." << endl;//end main
}
Well octal numbers are in base 8, and you already have the conversion for binary, which is base 2. You can use the same conversion code, and just switch the base:
1
2
3
4
5
6
while ((decimal != 0) & (k <= 15))
{
	bArray[k] = decimal % base;
	decimal = decimal / base;
	k++;
}

This would be one way to do it.

It's basically subtracting one from the user_input until it reaches zero and increasing the last element of the oct_array. When the last element of the oct_array exceeds 7, the program enters the if statement which increments the [n-1] element of the oct_array and set the current element to 0 until none of the elements exceed 7.

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
#include <iostream>
using namespace std;


int main()
{
	int oct_array[8];
	int user_input;

    for (int i = 0; i < 8; i++){
        oct_array[i] = 0;//populate oct_array with 0's
        }

cout << "Enter a decimal number to convert to octal: ";

cin >> user_input;

    for (int i = user_input; i > 0; i--){
            oct_array[7] = oct_array[7] + 1;
            if (oct_array[7] > 7){
                for (int n = 6; n > -1; n--){
                        oct_array[n+1] = 0;
                        oct_array[n] = oct_array[n] + 1;
                        if (oct_array[n] < 8){
                            n = -1;
                        }
                    }
                }
    }
cout << "\n\nConverted to Octal is: ";
for (int i = 0; i < 8; i++){
        cout << oct_array[i];
        }

return 0;

}
Last edited on
Garion and Yulingo thanks a bunch for the help! Also Garion thanks for the previous help on another thread of mine, I forgot to reply to it.

Anyway this is what I got so far.

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
#include <iostream>
using namespace std;


int main()
{
	//I entered the array output and necessary
	//components separately away from the other variables.
	//Just to be distinguishable.
	int bArray[16] = { 0 };
	int k = 0;

	int oArray[8] = { 0 };
	int i = 0;

	int decimal = 0;
	double binary = 0;
	double octal = 0;
	int number;

	cout << "Enter 'B' to convert a decimal number to binary and 'O' for an octal conversion." 
		<< "Hit space to quit program." << endl;

	while ((number = cin.get()) != ' ')
	{
		cout << "\nEnter a decimal number to be converted: " << endl;

		switch (number)
		{
		case 'B':
		case 'b':
			cin >> decimal;
			while ((decimal != 0) & (k <= 15))
			{
				bArray[k] = decimal % 2;
				decimal = decimal / 2;
				k++;
			}

			cout << "Here is the binary conversion: ";

			for (k = 15; k >= 0; k--)
			{
				cout << bArray[k];
				if ((k % 4) == 0)
					cout << " ";
			}
			break;

		case 'O':
		case 'o':
			cin >> decimal;
			while ((decimal != 0) & (i <= 7))
			{
				oArray[i] = decimal % 8;
				decimal = decimal / 8;
				i++;
			}

			cout << "Here is the octal conversion: ";

			for (i = 7; i >= 0; i--)
			{
				cout << oArray[i];
				if ((i % 4) == 0)
					cout << " ";
			}
			break;

		default:
			//This code here continues to output even if my cases are successfully compiled.
			cout << "Wrong decimal input." << "Enter a new number." << endl;
			break;
		}
	}

	cout << "Thank you." << endl;//end main
}


The conversions work perfectly fine. The only other problem I'm having is on each loop my default message displayed. I thought the default message was only suppose to display if someone were to enter the wrong kind of input needed?
Last edited on
Alright fixed the issue with default message. Although I have ran into another problem. When I convert for the first time it works. But when it loops again it says " Enter a decimal number to be converted: " instead of "Enter 'B' to convert a decimal number to binary and 'O' for an octal conversion." Because it is trying to get a B or O, not the number.
Topic archived. No new replies allowed.