Input and Output Statements
In C++, a stream is referred to as a series of letters, characters or symbols. The stream is handled by objects which are inside the iostream library. That is why, you use #include <iostream> at the start of a C++ program. This #include statement will let you use the input and output stream commands of C++.
Output Statements
Output statements are used to display information on the screen. The cout object (pronounced as see out) is used to display information on the screen, it is followed by the insertion operator <<, then a variable or a literal string, and finally a semicolor ;. Here is what the syntax looks like:
cout<<value;cout<<variable;cout<<"Literal String";You could even combine multiple cout statements into a single line like this:
cout<<"Welcome "<<name<<" to C++ Programming";and the output will be:
// Assuming the value of "name" is SamanthaWelcome Samantha to C++ ProgrammingIf you want to display a new line, you can either use the endl object (pronounced as end line) or the escape sequence \n. Here is an example of using endl:
cout<<"Hello there!"<<endl;cout<<"This is a new line";and the output will be:
Hello there!This is a new lineInput Statements
Input statements are used to get information from the user. The cin object (pronounced as see in) is used to get information from the user, it is followed by the extraction operator >>, then a variable, and finally a semicolon ;. Here is what the syntax looks like:
cin>>variable;Here is an example of using cin with an integer variable called age:
cin>>age;cin alone will not display any message to the user, so you should use cout to display a message before using cin to get information from the user. Here is an example of using cout and cin together:
cout<<"Enter your age: ";cin>>age;and the output will be:
Enter your age: 20