Reading input
The iostreams classes provide the ability
to read input. The object used for
standard input is
cin (for “console input”). cin
normally expects input from the console, but this input can be redirected from
other sources. An example of redirection is shown later in this
chapter.
The iostreams operator used with
cin is >>. This operator waits for the same kind of input as
its argument. For example, if you give it an integer argument, it waits for an
integer from the console. Here’s an example:
//: C02:Numconv.cpp
// Converts decimal to octal and hex
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a decimal number: ";
cin >> number;
cout << "value in octal = 0"
<< oct << number << endl;
cout << "value in hex = 0x"
<< hex << number << endl;
} ///:~
This program converts a number typed in
by the user into octal and hexadecimal
representations.