Solved

# C++ Convert decimal to octal in printf

Posted on 2003-02-20
Medium Priority
664 Views
Here is my code.  Does anyone know a simple way to convert the sum in printf statement to an octal value?

Any help would be appreciated.

#include<stdio.h>

int main()
{
int octal, decimal = 0;
int number1, number2, sum;
int highBit = 512 /*8^3*/, factor = 1000 /*10^3*/;

printf( "Enter two numbers ( no more than 4 digits ) in base 8: " );
scanf( "%d%d", &number1, &number2, &octal );

while ( highBit >= 1 )
{
decimal += octal / factor * highBit;
highBit /= 8;
octal %= factor;
factor /= 10;
}

sum = number1 + number2;

printf( "Result: %d + %d = %d\n", number1, number2, sum );

return 0;
}

0
• 3
• 2
• 2

LVL 6

Expert Comment

ID: 7990794
Prints octal values for x, y and z...

printf(" octal x = %4o y = %4o z = %4o\n",x,y,z);
0

LVL 6

Expert Comment

ID: 7990803
whoops, you'd only need to do the last number, not all 3...
0

LVL 6

Expert Comment

ID: 7990860
Of course, you are adding the 2 numbers as if they were base10 numbers, so the sum will be incorrect in octal format, unless you are doing something not shown in your code...

Routines to convert and work/add  octals can be found here:

http://www.programmersheaven.com/zone3/cat415/13801.htm

0

LVL 12

Expert Comment

ID: 7993566
why do you scanf the numbers using %d if you want to read them as octal numbers?

use %o if the numbers are octal.

printf( "Enter two numbers ( no more than 4 digits ) in base 8: " );
scanf( "%o%o", &number1, &number2 );

Then you have the numbers already in octal form and all you have to do is add them together:

int result = number1 + number2;

Question 2: What is that &octal doing in your scanf call. You have only two formats so the third argument to scanf is ignored.

Also be aware that scanf will leave the \n the user typed at the end of the numbers still in the buffer, you should do a fgets() or something  after the scanf to get rid of that '\n' and other garbage after the numbers.

Personally I detest scanf and consider the function bad. In your particular usage of it isn't that bad though so I won't nag about that here except saying that I think fgets() followed by sscanf() is better. Even better would be to use cin >> number1 >> number2; and not use scanf() type functions at all.

#include <iostream>

...

cout << "Enter two numbers blah blah blah: ";
cin >> oct >> number1 >> number2;
cin.ignore(1024,'\n');

Alf
0

LVL 12

Accepted Solution

Salte earned 100 total points
ID: 7993573
Btw, if you want to print a value as octal by printf the format is also %o:

printf( "The result is 0%o\n", number1 + number2);

would display the result in octal.

Alf
0

Author Comment

ID: 8001539
Great,

Thanks for all your help.  I used the %o in scanf and printf and all is well.

0

Author Comment

ID: 8001550
Thanks for all your help.  I used %o in scanf and printf and all work out well.

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month5 days, 20 hours left to enroll