Chapter 7: Input and Output in C++

Chapter 7: Input and Output in C++

Input and output in C++

In C++, input and output are important ways for a program to interact with the user or external devices. This article will deeply explain the concepts of input and output in C++, common input and output functions and related operators, and combine a large number of code examples and practical cases to help you better understand.

enter

In C++, the commonly used input function is cinthat it allows the program to receive input from the user and store it in the corresponding variable.

The following are examples in different scenarios, demonstrating how to use cinget user input:

Example 1: Get integer input

#include <iostream>
using namespace std;

int main() {
    
    
    int number;

    cout << "请输入一个整数:" << endl;
    cin >> number;

    cout << "您输入的整数是:" << number << endl;

    return 0;
}

Example of running results:

请输入一个整数:
42
您输入的整数是:42

Example 2: Get string input

#include <iostream>
using namespace std;

int main() {
    
    
    string name;

    cout << "请输入您的姓名:" << endl;
    cin >> name;

    cout << "您的姓名是:" << name << endl;

    return 0;
}

Example of running results:

请输入您的姓名:
John Doe
您的姓名是:John

Example 3: Get string input with spaces

#include <iostream>
using namespace std;

int main() {
    
    
    string full_name;

    cout << "请输入您的全名:" << endl;
    cin.ignore(); // 忽略之前的换行符
    getline(cin, full_name); // 读取整行输入

    cout << "您的全名是:" << full_name << endl;

    return 0;
}

Example of running results:

请输入您的全名:
John Doe
您的全名是:John Doe

output

The output function in C++ is coutused to display the data in the program on the console for easy viewing by the user.

The following are examples in different scenarios showing how to use coutfor output:

Example 1: Output variable value

#include <iostream>
using namespace std;

int main() {
    
    
    int number = 42;
    string name = "John Doe";

    cout << "整数:" << number << endl;
    cout << "字符串:" << name << endl;

    return 0;
}

Example of running results:

整数:42
字符串:John Doe

Example 2: Formatted output

#include <iomanip>
#include <iostream>
using namespace std;

int main() {
    
    
    double number = 3.1415926;
    int value = 42;

    cout << "默认输出:" << number << endl;
    cout << "定点表示:" << fixed << number << endl;
    cout << "科学计数法:" << scientific << number << endl;
    cout << "带宽度的输出:" << setw(10) << value << endl;
    cout << "十六进制表示:" << hex << value << endl;

    return 0;
}

Example of running results:

默认输出:3.14159
定点表示:3.141593
科学计数法:3.141593e+00
带宽度的输出:        42
十六进制表示:2a

file input and output

In addition to console input and output, C++ also provides input and output operations on files. By using the related file stream classes, we can read data from a file or write data to a file.

The following are examples in different scenarios showing how to do file input and output:

Example 1: File writing

#include <fstream>
#include <iostream>
using namespace std;

int main() {
    
    
    ofstream outputFile("data.txt"); // 创建用于输出的文件流对象

    if (outputFile.is_open()) {
    
    
        outputFile << "Hello, World!" << endl;
        outputFile << "This is a test." << endl;
        outputFile.close();
    } else {
    
    
        cout << "无法打开文件" << endl;
    }

    return 0;
}

After executing the program, a file named .txt will be generated in the same directory data.txt, and the specified text content will be written into it.

Example 2: File reading

#include <fstream>
#include <iostream>
using namespace std;

int main() {
    
    
    ifstream inputFile("data.txt"); // 创建用于输入的文件流对象

    if (inputFile.is_open()) {
    
    
        string line;
        while (getline(inputFile, line)) {
    
    
            cout << line << endl;
        }
        inputFile.close();
    } else {
    
    
        cout << "无法打开文件" << endl;
    }

    return 0;
}

After the program is executed, the previously generated data.txtfile is read and each line in the file is displayed on the console.

exception handling

In actual programming, error handling is required for reading input or writing output. C++ provides an exception handling mechanism to deal with possible exceptions.

Here is an example showing how to use try-catchblocks to catch and handle exceptions:

#include <iostream>
using namespace std;

int main() {
    
    
    int number;

    cout << "请输入一个整数:" << endl;
    
    try {
    
    
        cin.exceptions(ios_base::failbit); // 启用cin的异常抛出机制
        cin >> number;

        cout << "您输入的整数是:" << number << endl;
    } catch (exception& e) {
    
    
        cout << "发生了异常:" << e.what() << endl;
    }

    return 0;
}

In the above code, we use the enabled cinexception throwing mechanism to make it throw an exception when an exception occurs ios_base::failure. Then, use try-catcha block to catch and handle the exception.

If the user input is not an integer, an exception will be triggered and catchthe block will be entered for related processing.

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/132241390