Solved

Output a Number, in Decimal, Hex and Octal

Posted on 1998-07-22
2
1,096 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
[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
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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 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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 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