Solved

Output a Number, in Decimal, Hex and Octal

Posted on 1998-07-22
2
1,080 Views
Last Modified: 2008-02-26
Given the following code segment:

#include <iostream.h>

int main()
{
      int num = 0;
      cout << "Enter an integer: " << endl;
      cin >> num;

      cout << "Format: decimal, octal & hexadecimal." << endl;
      //cout.setf(ios::dec, ios::basefield);
      //cout << num;                           ??????
      //cout.setf(ios::oct, ios::basefield);
      //cout << num;
      cout.setf(ios::hex, ios::basefield);
      cout << num;

  //  cout << num;

return(0);
}

I need to output the number entered in decimal, hexidecimal and octal
format, all at once.

Why can't I string together the appropriate format specifiers, as I originally
tried in the above code?
0
Comment
Question by:jnowlin
2 Comments
 
LVL 2

Accepted Solution

by:
kellyjj earned 60 total points
Comment Utility
Hope this helps

cout <<hex << num;
cout <<dec <<num;
cout <<oct<< num;
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
jnowlin, kellyjj's answer is correct but there is one more thing that he forgot to mention.

the << operator returns a reference to the stream, allowing concatenation.  Thus:

    cout << hex << num << ' ' << dec << num << ' ' << oct << num << endl;

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now