Solved

Output a Number, in Decimal, Hex and Octal

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming on screen browser tutorial lesson 2 87
Safe conversion? 4 76
VS2015 Redefinition errors 4 91
c++  placing data into a form and an editbox 5 23
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

733 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