|
|
Input |
|
scanf isn't usually used in C++. Instead use
cin and the >> operator.
cin is a global variable defined in iostream
that is the input object tied to standard input (normally the
keyboard).
The general format is
cin >> <var1> >>
<var2> >> <var3>;
The >> operator (when used with cin) knows
how much and what to read based on the type of variable being read
into.
In the following example the program reads two numbers from the keyboard (hit return after each) and prints them to the screen.
#include <iostream>
using namespace std;
int main (int argc, char** argv) {
cout << "Enter an integer then a double" << endl;
int i;
double j;
cin >> i >> j;
cout << "integer : " << i << endl;
cout << "double : " << j << endl;
return 0;
}
Output:
Enter an integer then a double 5 6.6 integer : 5 double : 6.6