Solved

Output a Number, in Decimal, Hex and Octal

Posted on 1998-07-22
2
1,084 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
ID: 1168404
Hope this helps

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

Expert Comment

by:alexo
ID: 1168405
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

26 Experts available now in Live!

Get 1:1 Help Now