How to use the C++ stream extraction operator with sample code

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Write to standard output and  read from standard input using cout and cin (stream extraction operator) respectively in C++ programming

To show: How to write to standard output and read from standard input using cout and cin (stream extraction operator) respectively in C++ programming

 

// the stream extraction operator, input from keyboard with cin

#include <iostream>

using namespace std;

 

int main(void)

{

int x, y;

 

cout<<"Enter two integers: ";

cin>>x>>y;

cout<<"Sum of "<<x<<" and "<<y<<" is "<<(x + y)<<endl;

 

return 0;

}

 

Output example:

 

Enter two integers: 12 40

Sum of 12 and 40 is 52

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index