In C++, the main way a program interacts with external data is through input (acquiring data from the keyboard) and output (displaying data on the screen). cin and cout are input/output stream objects provided by the C++ Standard Library, making data reading and writing simple and intuitive.
I. What are Input/Output Streams?¶
In simple terms, an Input Stream (Input Stream) is responsible for “flowing” external data (such as keyboard input) into the program, while an Output Stream (Output Stream) is responsible for “flowing out” data from the program to the outside (such as displaying on the screen). C++’s <iostream> library (Input/Output Stream Library) provides the cin (input stream object) and cout (output stream object) to implement this functionality.
II. Header File and Namespace¶
Before using cin and cout, you must include the header file <iostream>, otherwise the compiler will not recognize these objects. To simplify the code (avoiding repeated writing of std::cin), it is common to use using namespace std; to declare the std namespace.
#include <iostream> // Required header file, provides definitions for cin and cout
using namespace std; // Declare use of the standard namespace to avoid prefixing with std::
III. cin: Reading Data from the Keyboard¶
cin is an input stream object that reads data from standard input (usually the keyboard) through the extraction operator >>. The basic syntax is: cin >> variable;.
Example 1: Reading an Integer
#include <iostream>
using namespace std;
int main() {
int age; // Define an integer variable 'age'
cout << "Please enter your age: "; // Output a prompt
cin >> age; // Read data from the keyboard and store it in 'age'
cout << "The age you entered is: " << age << endl; // Output the result
return 0;
}
Code Explanation:
- int age;: Defines an integer variable age to store the input age.
- cin >> age;: After the user enters data on the keyboard, cin “extracts” the data into age.
- >> is the extraction operator, indicating “obtain data from the input stream and assign it to the variable”.
IV. cout: Outputting Data to the Screen¶
cout is an output stream object that displays data on the standard output (usually the screen) through the insertion operator <<. cout can output multiple data continuously with the syntax: cout << data1 << data2 << ... << dataN;.
Example 2: Outputting Multiple Data
#include <iostream>
using namespace std;
int main() {
string name; // Define a string variable 'name'
int score; // Define an integer variable 'score'
cout << "Please enter your name and score: ";
cin >> name >> score; // Read two data continuously (name and score)
cout << "Name: " << name << "\nScore: " << score << endl; // Output the result
return 0;
}
Code Explanation:
- cout << "Name: " << name << "\nScore: " << score << endl;: Continuously outputs strings, variables, and the newline character endl (which inserts a newline and flushes the output buffer to ensure immediate display).
- "\n" is a newline character, also used for line breaks, but endl is more commonly used (to avoid buffer issues).
V. Input/Output for Common Data Types¶
cin and cout support multiple data types, including integers (int), floating-point numbers (float/double), and strings (string). Simply define variables of the corresponding type.
Example 3: Reading a Floating-Point Number
#include <iostream>
using namespace std;
int main() {
double height; // Define a double-precision floating-point variable 'height'
cout << "Please enter your height (in meters): ";
cin >> height; // Read the height
cout << "Your height is: " << height << " meters" << endl;
return 0;
}
Example 4: Reading a String (Note on Space Handling)
cin >> string_variable stops reading when encountering a space. To read a string with spaces (e.g., “Zhang San”), use getline(cin, string_variable) (requires including <string> header):
#include <iostream>
#include <string> // Required for string operations
using namespace std;
int main() {
string fullName;
cout << "Please enter your full name (including spaces): ";
getline(cin, fullName); // Read an entire line of strings (including spaces)
cout << "The full name you entered is: " << fullName << endl;
return 0;
}
VI. Notes¶
- Variable Definition: Variables must be defined before inputting data; otherwise, data cannot be stored.
- Data Type Matching: The input data type must match the variable type (e.g., if
ageisint, the user must enter an integer, otherwise errors may occur). - Don’t Forget the Header File:
#include <iostream>is mandatory; otherwise,cinandcoutwill not be recognized. - Continuous Input: Multiple
cin >> variablescan be chained, with input data separated by spaces.
Summary¶
cin and cout are the most basic input/output tools in C++, implemented through the >> and << operators for data reading and writing. Mastering them requires:
- Using cin >> variable to read keyboard input.
- Using cout << data to output content to the screen.
- Paying attention to header file and namespace declarations.
- Defining corresponding variables based on data types.
By practicing simple input/output programs (e.g., reading two numbers and summing them, calculating average scores), you will become proficient with these basic tools!