It is one of the most fundamental and commonly used input-output tools in the C++ standard library.
π― 1. What is Standard Input and Output in C++?
C++ provides a stream-based input-output system, which mainly includes:
|
Functionality |
Description |
Related Objects |
|---|---|---|
|
Standard Output (Screen Printing) |
Outputs information to the console (screen) |
|
|
Standard Input (Keyboard Input) |
Reads user input from the keyboard |
|
|
Standard Error Output |
Used to output error messages (usually also to the screen) |
|
These functionalities are included in the <span><iostream></span> header file, which is part of the C++ standard library.
β 2. Using iostream for Standard Input and Output
1. Include the Header File
#include<iostream>
This is necessary as it provides objects and functionalities such as <span>cin</span>, <span>cout</span>, <span>cerr</span>, <span>endl</span>, etc.
2. Using the Standard Namespace (Recommended Practice)
These objects in the C++ standard library (like <span>cout</span>, <span>cin</span>) are defined in the namespace <span>std</span>, so we can either:
-
Write
<span>std::cout</span>,<span>std::cin</span>(recommended for clarity) -
Or use
<span>using namespace std;</span>(simplified syntax, suitable for small programs, but use with caution in large projects)
π§© 3. Standard Output:<span>std::cout</span> (Printing Content to the Screen)
Basic Usage:
std::cout << "Content to output";
-
<span><<</span>is the stream insertion operator, which βinsertsβ the content on the right into the output stream (screen). -
Multiple
<span><<</span>can be used consecutively to output multiple contents.
β Example 1: Output Text
#include<iostream>
int main(){
std::cout << "Hello, C++!" << std::endl;
return 0;
}
π Output Result:
Hello, C++!
<span>std::endl</span>indicates a newline and flushes the output buffer, equivalent to `
` + flush.
β Example 2: Output Multiple Contents (Variables, Strings, Numbers, etc.)
#include<iostream>
int main(){
std::cout << "My age is: " << 20 << " years old" << std::endl;
std::cout << "The value of PI is approximately: " << 3.14159 << std::endl;
return 0;
}
π Output:
My age is: 20 years old
The value of PI is approximately: 3.14159
<span>cout</span>can automatically recognize various data types (such as<span>int</span>,<span>double</span>,<span>string</span>, etc.) and output them correctly.
π§© 4. Standard Input:<span>std::cin</span> (Reading Input from the Keyboard)
Basic Usage:
std::cin >> variable;
-
<span>>></span>is the stream extraction operator, which extracts data from the input stream (usually the keyboard) and stores it in the specified variable. -
Typically used for inputting basic types: such as
<span>int</span>,<span>double</span>,<span>char</span>,<span>std::string</span>, etc.
β Example 3: Input an Integer from the Keyboard and Output
#include<iostream>
int main(){
int age;
std::cout << "Please enter your age: ";
std::cin >> age; // Read an integer from the keyboard and store it in age
std::cout << "Your entered age is: " << age << std::endl;
return 0;
}
π Running Process:
Please enter your age: 25β
Your entered age is: 25
<span>β</span>indicates that you pressed the Enter key to submit the input.
β Example 4: Input Multiple Values
#include<iostream>
int main(){
int a, b;
std::cout << "Please enter two integers, separated by a space: ";
std::cin >> a >> b; // Read two integers sequentially
std::cout << "The two numbers you entered are: " << a << " and " << b << std::endl;
return 0;
}
π Input:
10 20β
π Output:
The two numbers you entered are: 10 and 20
<span>cin</span>extracts data from the input stream in order, separated by spaces, tabs, or newlines.
π§© 5. Standard Error Output:<span>std::cerr</span> and <span>std::clog</span>
|
Stream Object |
Description |
Buffered? |
|---|---|---|
|
|
Used for outputting error messages, unbuffered, displayed immediately |
β Unbuffered |
|
|
Used for outputting log information, buffered |
β Buffered |
Generally used for debugging or logging errors, similar to <span>cout</span>, but with a more specific purpose.
β Example 5: Using cerr to Output Error Information
#include<iostream>
int main(){
std::cerr << "This is an error message!" << std::endl;
return 0;
}
Usually used to show errors to users/developers when the program fails, distinguishing it from normal output.
π§ 6. Common Input-Output Manipulators (Formatted Output)
|
Manipulator |
Description |
Example |
|---|---|---|
|
|
Newline and flush the buffer |
|
|
`’ |
||
|
‘` |
Newline (without flushing the buffer) |
`cout << “Hi” |
|
|
Set output width (requires |
|
|
|
Set floating-point precision |
|
|
|
Fixed decimal representation |
Used with |
These formatting tools are usually found in the
<span><iomanip></span>header file, such as controlling decimal places, alignment, etc.
β 7. Complete Example: Input Name and Age and Output
#include<iostream>
#include<string>// Include this header file to use std::string
int main(){
std::string name;
int age;
std::cout << "Please enter your name: ";
std::cin >> name;
std::cout << "Please enter your age: ";
std::cin >> age;
std::cout << "Hello, " << name << "! You are " << age << " years old." << std::endl;
return 0;
}
π Running Example:
Please enter your name: Xiao Mingβ
Please enter your age: 18β
Hello, Xiao Ming! You are 18 years old.
π οΈ 8. Summary: Key Points of C++ Standard Input and Output (iostream Library)
|
Functionality |
Corresponding Object |
Description |
Header File |
|---|---|---|---|
|
Standard Output (Print to Screen) |
|
Used to output information to the console |
|
|
Standard Input (Read from Keyboard) |
|
Used to read input from the keyboard |
|
|
Standard Error Output |
|
Outputs error messages (unbuffered) |
|
|
Standard Log Output |
|
Outputs logs (buffered) |
|
|
Newline / Formatting |
|
||
|
‘` |
Newline control |
|
|
|
Formatting Tools (e.g., Decimal Places, Width) |
|
Requires |
|
|
String Input |
|
Requires |
|
π§© 9. Recommended Writing Standards
-
It is recommended to use
<span>std::cout</span>and<span>std::cin</span>(instead of<span>using namespace std;</span>), for clearer and safer code. -
For beginners, you can start with
<span>using namespace std;</span>to simplify code, but understand its principles and potential risks. -
When inputting multiple values, pay attention to the spaces / newlines separating them.
-
When formatting output (like controlling decimal places, alignment), use tools from
<span><iomanip></span>.
β 10. Want to Learn More?
- How to use
<span>std::getline()</span>to input a line of text (including spaces)? - How to format output (like keeping 2 decimal places, setting width, etc.)?
- Common issues with
<span>cin</span>: input type mismatch causing the program to hang, how to resolve? - What is buffer flushing,
<span>endl</span>and `’ - How to input and output from files (i.e., file streams
<span>fstream</span>)?