第七章:C++中的输入与输出

第七章:C++中的输入与输出

C++中的输入与输出

在C++中,输入和输出是程序与用户或外部设备进行交互的重要方式。本文将深入讲解C++中输入与输出的概念、常用的输入输出函数和相关操作符,并结合大量的代码示例和实际案例来帮助您更好地理解。

输入

在C++中,常用的输入函数是cin,它允许程序接收来自用户的输入并存储到相应的变量中。

以下是不同场景下的示例,演示了如何使用cin获取用户输入:

示例 1:获取整数输入

#include <iostream>
using namespace std;

int main() {
    
    
    int number;

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

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

    return 0;
}

运行结果示例:

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

示例 2:获取字符串输入

#include <iostream>
using namespace std;

int main() {
    
    
    string name;

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

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

    return 0;
}

运行结果示例:

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

示例 3:获取带有空格的字符串输入

#include <iostream>
using namespace std;

int main() {
    
    
    string full_name;

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

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

    return 0;
}

运行结果示例:

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

输出

C++中的输出函数是cout,它用于将程序中的数据显示在控制台上,便于用户查看。

以下是不同场景下的示例,展示了如何使用cout进行输出:

示例 1:输出变量值

#include <iostream>
using namespace std;

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

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

    return 0;
}

运行结果示例:

整数:42
字符串:John Doe

示例 2:格式化输出

#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;
}

运行结果示例:

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

文件输入和输出

除了控制台输入和输出之外,C++还提供了对文件的输入和输出操作。通过使用相关的文件流类,我们可以从文件中读取数据或将数据写入到文件中。

以下是不同场景下的示例,展示了如何进行文件输入和输出:

示例 1:文件写入

#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;
}

执行该程序后,在同一目录下会生成一个名为data.txt的文件,并将指定的文本内容写入其中。

示例 2:文件读取

#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;
}

执行该程序后,会读取之前生成的data.txt文件,并将文件中的每行内容显示在控制台上。

异常处理

在实际编程中,需要对读取输入或写入输出进行错误处理。C++中提供了异常处理机制,用于应对可能出现的异常情况。

以下是一个示例,展示了如何使用try-catch块来捕获和处理异常:

#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;
}

上述代码中,我们通过启用cin的异常抛出机制,使其在发生异常时抛出ios_base::failure异常。然后,使用try-catch块来捕获并处理该异常。

如果用户输入的不是整数,则会触发异常,并进入catch块进行相关处理。

猜你喜欢

转载自blog.csdn.net/qq_51447496/article/details/132241390
今日推荐