?
Solved

C++ Convert decimal to octal in printf

Posted on 2003-02-20
7
Medium Priority
?
619 Views
Last Modified: 2012-05-05
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
Comment
Question by:adinkins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Expert Comment

by:gj62
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

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

Expert Comment

by:gj62
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Salte
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

by:
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

by:adinkins
ID: 8001539
Great,

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

ad
0
 

Author Comment

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

ad
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question