C++ - 读取控制台输入(读取单个变量、读取多个变量、读取字符串、错误输入、错误输入的影响、错误输入的处理、读取字符串问题、连续读取)

一、读取单个变量

1、读取单个数字
#include <iostream>

using namespace std;

int main() {

    int num;
    cout << "请输入一个数字:";
    cin >> num;
    cout << "数字:" << num << endl;

    return 0;
}
  1. 输出一个数字,输出结果
请输入一个数字:1
数字:1
  1. 输入一个非数字,输出结果
请输入一个数字:q
数字:0
2、读取单个字符
#include <iostream>

using namespace std;

int main() {

    char c;
    cout << "请输入一个字符:";
    cin >> c;
    cout << "字符:" << c << endl;

    return 0;
}
  1. 输入一个字符,输出结果
请输入一个字符:q
字符:q
  1. 输入多个字符,输出结果
请输入一个字符:qwe
字符:q

二、读取多个变量

1、具体实现 1
#include <iostream>

using namespace std;

int main() {

    int a, b;
    cout << "请输入两个数字(空格分隔):";
    cin >> a >> b;
    cout << "和为:" << a + b << endl;

    return 0;
}
  • 输出结果
请输入两个数字(空格分隔):123 456
和为:579
2、具体实现 2
#include <iostream>
#include <string>

using namespace std;

int main() {

    int num1;
    cout << "请输入第一个数字:";
    cin >> num1;
    cout << "第一个数字:" << num1 << endl;

    int num2;
    cout << "请输入第二个数字:";
    cin >> num2;
    cout << "第二个数字:" << num2 << endl;

    return 0;
}
  • 输出结果
请输入第一个数字:123
第一个数字:123
请输入第二个数字:456
第二个数字:456

三、读取字符串

1、读取不含空格的字符串
#include <iostream>
#include <string>

using namespace std;

int main() {

    string str;
    cout << "输入内容(不带空格):";
    cin >> str;
    cout << "内容:" << str << endl;

    return 0;
}
  1. 输入一段字符串,不含空格
输入内容(不带空格):123
内容:123
  1. 输入一段字符串,含空格
输入内容(不带空格):123 45
内容:123
2、读取含空格的字符串
#include <iostream>
#include <string>

using namespace std;

int main() {

    string str;
    cout << "输入内容:";
    getline(cin, str);
    cout << "内容:" << str << endl;

    return 0;
}
  1. 输入一段字符串,不含空格
输入内容:123
内容:123
  1. 输入一段字符串,含空格
输入内容:123 45
内容:123 45

四、错误输入

1、基本介绍
  1. 合法输入时,cin.fail() 返回 false

  2. 非法输入时,cin.fail() 返回 true

2、演示
#include <iostream>
#include <string>

using namespace std;

int main() {

    int age;
    cout << "请输入你的年龄:";
    if (cin >> age) {
        cout << "你的年龄是:" << age << endl;
    }
    else {
        cout << "非法的输入" << endl;
    }
    
    cout << cin.fail() << endl;

    return 0;
}
  1. 合法输入,输出结果
请输入你的年龄:20
你的年龄是:20
0
  1. 非法输入,输出结果
请输入你的年龄:qwe
非法的输入
1

五、错误输入的影响

1、基本介绍
  1. 如果存在错误状态,cin 后续的读取都会直接跳过

  2. 直到清除错误状态,cin 后续的读取才会恢复正常

2、影响演示
#include <iostream>
#include <string>

using namespace std;

int main() {

    int age;
    cout << "请输入你的年龄:";
    if (cin >> age) {
        cout << "你的年龄是:" << age << endl;
    }
    else {
        cout << "非法的输入" << endl;
    }

    cout << cin.fail() << endl;

    int num;
    cout << "请输入一个数字:";
    cin >> num;
    cout << "数字:" << num << endl;

    return 0;
}
  • 输出结果
请输入你的年龄:qwe
非法的输入
1
请输入一个数字:数字:0
3、影响演示解读
  1. cin >> age 尝试读取整数

  2. 但用户输入的是字母,cin.fail() 变为 true

  3. age 的值未被修改,可能是未初始化的随机值

  4. 无效数据仍留在输入缓冲区中

  5. 后续输入 cin >> num 被跳过


六、错误输入的处理

1、基本介绍
  1. 调用 cin.clear() 清除错误状态

  2. 调用 cin.ignore() 清空缓冲区残留数据

2、演示
#include <iostream>
#include <string>

using namespace std;

int main() {

    int age;
    cout << "请输入你的年龄:";
    if (cin >> age) {
        cout << "你的年龄是:" << age << endl;
    }
    else {
        cout << "非法的输入" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 丢弃缓冲区中的无效数据,直到遇到换行符 \n 或者达到最大字符数
    }

    cout << cin.fail() << endl;

    int num;
    cout << "请输入一个数字:";
    cin >> num;
    cout << "数字:" << num << endl;

    return 0;
}
  • 输出结果
请输入你的年龄:qwe
非法的输入
0
请输入一个数字:123
数字:123

七、读取字符串问题

1、问题演示
#include <iostream>
#include <string>

using namespace std;

int main() {

    string str1;
    cout << "输入内容 1(不带空格):";
    cin >> str1;
    cout << "内容 1:" << str1 << endl;

    string str2;
    cout << "输入内容 2(不带空格):";
    cin >> str2;
    cout << "内容 2:" << str2 << endl;

    return 0;
}
  • 输出结果
输入内容 1(不带空格):123 456
内容 1:123
输入内容 2(不带空格):内容 2:456
2、问题演示解读
  1. 用户输入 123 456

  2. cin >> str1 读取 123

  3. 剩下的数据仍留在输入缓冲区中

  4. cin >> str2 读取 456

3、问题处理演示
#include <iostream>
#include <string>

using namespace std;

int main() {

    string str1;
    cout << "输入内容 1(不带空格):";
    cin >> str1;
    cout << "内容 1:" << str1 << endl;

    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 丢弃缓冲区中的无效数据,直到遇到换行符 \n 或者达到最大字符数

    string str2;
    cout << "输入内容 2(不带空格):";
    cin >> str2;
    cout << "内容 2:" << str2 << endl;

    return 0;
}
  • 输出结果
输入内容 1(不带空格):123 456
内容 1:123
输入内容 2(不带空格):123
内容 2:123

八、连续读取

#include <iostream>
#include <string>

using namespace std;

int main() {

    while (true)
    {
        string str;
        cout << "输入内容:";
        getline(cin, str);
        cout << "内容:" << str << endl;
    }

    return 0;
}
  • 输出结果
输入内容:123
内容:123
输入内容:456
内容:456
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值