decimal to octal conversion program. output comes in reverse.

Hey. Im writing a program that converts decimal numbers to octal. the problem that its giving output in reverse. Like, decimal number= 8, octal number is actually 10, but its shows 01. please help me with this. below is a rough draft.

int i,j,r;
cout<<"octal\n";
i=1;
while(i<=256)
{ {for(j=i;j>0;)
{r=j%8;
cout << r;
j=j/8;
}}
i++;
cout<<endl;}
One possible solution.
Create a small array to store the digits as you get them, instead of using cout.
After the processing is completed loop backwards through the array and cout each value.
Thanks but Is there another solution, because I'm a beginner, and we haven't learned array yet, in our class.
Did you learn about strings?
Yes, I did, but I'm still trying to figure it 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
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
	int decimal;
	string octal;
	cout << "Input an integer : "; cin >> decimal;

	do
	{
		octal += '0' + (decimal % 8);
		decimal /= 8;
	} while(decimal);

	// http://www.cplusplus.com/reference/algorithm/reverse/
	std::reverse(octal.begin(), octal.end());

	cout << octal << endl;

	cin.ignore();
	cin.get();
	return 0;
}


Input an integer : 8
10
Another answer is to use a recursive function, but I suspect that the use of recursion is unlikely to be expected or is it?
I have absolutely no idea about recursive functions, and how to use them. But if you could tell me about that string thing, and how i can use it in this program, maybe i can try work on it.
@Alam997
Did you understand my program?
@Kotori sorry I was going to reply to that. I understood that you've used reverse function (i just searched it on internet and the link you provided). but i don't exactly understand the concept.
As an alternative to using std::reverse, you could use a loop. If octal is a string, you could print it in reverse
1
2
for (int i = octal.size()-1; i >=0; --i)
    cout << octal[i];

or something like that.

Or a variation on the code by Kotori
1
2
3
4
5
6
7
8
9
10
11
12
    int decimal;
    string octal;
    cout << "Input an integer : "; cin >> decimal;

    do
    {
        char ch = '0' + decimal % 8;
        octal = ch + octal;
        decimal /= 8;
    } while (decimal);

    cout << octal << endl;


Last edited on
The trivial solution is to use std::oct. See http://www.cplusplus.com/reference/ios/oct/

That, however, is not a solution for you. You don't actually want to see any octal values or whatnot. You want to practice logical thinking.

Why do you get a reverse output? Because you extract the least meaningful bits first.

What do you need to get the most meaningful bits first? The number of bits in the input.

How does one get that? With a loop. However, in your case you do know that the input is in range [1..256]. You do know/can calculate how many bits they have at most.

Or lets not say "bits" but "digits". Dec 256 = Oct 400. Three digits.
To get the first digit, first drop the other two with division.
To get the second, drop the last and use the modulo to drop the first digit.
To get the third, use the modulo.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
    for ( int input=1; input <= 256; ++input ) {
        std::cout << std::dec << input << " = ";
        std::cout << input / 64;
        std::cout << ( input / 8 ) % 8;
        std::cout << input % 8;
        std::cout << " (" << std::oct << input << ")\n";
    }
	return 0;
}

That is not a generic solution; it would not work for 765'234'987. For unknown input you would first increase the divisor in a loop, until it is "big enough". Then, decrease the divisor in actual loop that prints one number.


What about those leading zero's in small numbers? I leave that for you to think about.
Topic archived. No new replies allowed.