Chapter 1: Input, Output, and Comments in C++ Primer

1.2 Introduction to Input and Output

The previous section explained “output” (<span><span>std::cout</span></span>), this section supplements “input” (<span><span>std::cin</span></span>), and introduces “how to achieve interaction between the program and the user through input and output”—the core is to master the basic usage of<span><span>std::cin</span></span> and <span><span>std::cout</span></span>, as well as the simplification of the <span><span>std</span></span> namespace.

1.2.1 Standard Input: <span><span>std::cin</span></span>

<span><span>std::cin</span></span> is the “standard input stream” (<span><span>cin</span></span> is an abbreviation for “console input”), which serves to “read user input from the console”, used in conjunction with the “input operator”<span><span>>></span></span>.

Example: Read two integers from user input and calculate their sum

#include<iostream>using namespace std;  // Simplify the use of the std namespace (important!)int main(){    int a, b;  // Define two integer variables to store user input    cout << "Please enter two integers (separated by space):";  // Prompt user for input    cin >> a >> b;  // Read input: store the first number in a, the second number in b    cout << "Their sum is:" << a + b << endl;  // Output result    return 0;}

Code Analysis

  1. <span><span>using namespace std;</span></span>: Simplifies namespace usage

  • <span><span>std</span></span> is the C++ Standard Library namespace—its purpose is to “isolate identifiers with the same name (such as functions, variables, classes)” to avoid naming conflicts (for example, your own <span><span>cout</span></span> function will not conflict with the standard library’s <span><span>cout</span></span>).
  • If you do not write <span><span>using namespace std;</span></span>, you must prefix every standard library component with <span><span>std::</span></span> (such as <span><span>std::cin</span></span>, <span><span>std::cout</span></span>); after writing it, you can use <span><span>cin</span></span> and <span><span>cout</span></span> directly without repeating <span><span>std::</span></span>.
  • Note:<span><span>using namespace std;</span></span> is usually written “after header file inclusion and before the <span><span>main()</span></span> function”; its scope is “the current source file”—for simple programs, this is a convenient way to write; but in large projects, it is recommended to use global <span><span>using namespace std;</span></span> sparingly (to avoid potential naming conflicts), and instead explicitly write <span><span>std::</span></span> where needed, or use “local namespace declarations” (such as writing <span><span>using std::cout;</span></span> within the function body).
  • <span><span>cin >> a >> b;</span></span>: Read input

    • <span><span>cin</span></span> will automatically “skip whitespace characters” (spaces, tabs<span><span> </span></span>, newline characters<span><span>
      </span></span>
      , etc.)—when the user inputs, whether separated by spaces or newlines, <span><span>cin</span></span> can read correctly.
    • The variable type must match the input content: if the variable is <span><span>int</span></span> (integer), and the user inputs a letter (such as <span><span>a</span></span>), <span><span>cin</span></span> will enter an “error state”, and subsequent reads will fail (the following chapters will discuss how to handle input errors).
    • <span><span>>></span></span> is the “input operator” (also called the “extraction operator”), which serves to “extract content from the left-side stream and store it in the right-side variable”.
    • Reading rules:

    Run Effect

    Enter two integers (separated by space): 3 5Their sum is: 8

    1.2.2 Continuous Use of Input and Output

    <span><span><<</span></span> and <span><span>>></span></span> both support “chained calls”—multiple operators can be used consecutively to simplify code. For example:

    • Continuous output:<span><span>cout << "a = " << a << ", b = " << b << endl;</span></span> (first output the string <span><span>"a = "</span></span>, then output the value of <span><span>a</span></span>, then output <span><span>", b = "</span></span>, then output the value of <span><span>b</span></span>, and finally a newline).
    • Continuous input:<span><span>cin >> a >> b >> c;</span></span> (read three values in sequence, storing them in <span><span>a</span></span>, <span><span>b</span></span>, and <span><span>c</span></span>).

    1.3 Comments

    Comments are “explanations of code written for humans”; the compiler ignores comment content—reasonable comments can make code more readable and maintainable. C++ supports two styles of comments:

    1.3.1 Single-line Comments:<span><span>//</span></span>

    • Syntax:<span><span>// comment content</span></span> (everything from <span><span>//</span></span> to the “end of the line” is a comment).
    • Applicable scenarios: brief comments (such as explanations of single lines of code, variable meanings).
    • Example:
    int a = 10;  // Define variable a, initial value is 10 (single-line comment)// cin >> a;  // This is a line of code that has been commented out (will not execute)

    1.3.2 Multi-line Comments:<span><span>/* ... */</span></span>

    • Syntax:<span><span>/* comment content */</span></span> (everything from <span><span>/*</span></span><code><span><span> to </span></span><code><span><span>*/</span></span> is a comment, can span multiple lines).
    • Applicable scenarios: longer comments (such as function functionality descriptions, logical explanations of code blocks).
    • Example:
    /* * Function functionality: read two integers, calculate and output their sum * Input: user inputs two integers from the console * Output: print the sum of the two numbers on the console */int main() {    /* Variable definition area       a: stores the first integer       b: stores the second integer    */    int a, b;    cin >> a >> b;    cout << a + b << endl;    return 0;}

    Comments Considerations

    1. Multi-line comments cannot be nested: that is, <span><span>/* ... /* ... */ ... */</span></span> is incorrect—the compiler will match the first <span><span>/*</span></span> and the first <span><span>*/</span></span>, and the remaining <span><span>... */</span></span> will be treated as code, leading to compilation errors.
    2. Comments should be “concise and useful”: avoid redundant comments (such as <span><span>// Define variable a</span></span> which is unnecessary), and do not write “outdated comments” (after code modifications, comments must be updated synchronously, otherwise they will mislead readers).
    3. Prefer single-line comments: in scenarios where single-line comments can be used, prefer using <span><span>//</span></span> (more concise than <span><span>/* ... */</span></span> and avoids nesting issues).

    Leave a Comment