cin检测非数字输入

cin检测非数字输入

先来一个简单示例

#include <iostream>
using namespace std;

int main() {
    int num;
    while (!(cin >> num)) // input terminated by non-numeric response
    {
        cin.clear(); // reset input
        cin.get(); // read an alphabet
    }
    cin.get(); // read end of line after last input
    cout << num;
    cin.get(); // wait for user to press <Enter>

    return 0;
}

实例解析

错误处理的关键代码

while (!(cin >> num)) {
    cin.clear(); // reset input
    while (cin.get() != '\n') {
        continue; // get rid of bad input
    }
    cout << "Please enter a number: ";
}

完整代码

// cingolf.cpp -- non-numeric input skipped
#include <iostream>
const int Max = 5;
int main() {
    using namespace std;
    // get data
    int golf[Max];
    cout << "Please enter your golf scores.\n";
    cout << "You must enter " << Max << " rounds.\n";
    int i;
    double total = 0.0;
    for (int i = 0; i < Max; i++) {
        cout << "round #"  << i + 1 << ": ";
        while (!(cin >> golf[i])) {
            cin.clear();    // reset input
            while (cin.get() != '\n') continue; // get rid of bad input
            cout << "Please enter a number: ";
        }
        total += golf[i];
    }
    // report results
    cout << total / Max << " = average score " << Max << " rounds\n";
    return 0;
}

运行结果

Please enter your golf scores.
You must enter 5 rounds.
round #1: 88
round #2: 87
round #3: must i?
Please enter a number: 103
round #4: 94
round #5: 86
91.6 = average score 5 rounds
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,您可以参考以下代码实现: ``` python count = 0 input_str = "" while True: s = input("请输入以$结尾的字符串: ") if s.endswith("$"): input_str += s break input_str += s for char in input_str: if not char.isnumeric() and char != "$": count += 1 print("数字字符的数量为:", count) ``` 这段代码通过 `input` 方法读取用户输入的一系列以 `$` 结尾的字符串,并使用一个字符串变量 `input_str` 将这些字符串拼接在一起。接着,遍历 `input_str` 中的字符,在字符为数字且不为 `$` 时,将计数器 `count` 加 1。 最后,将统计结果输出到控制台上。希望该代码片段能对您有所帮助! ### 回答2: 假设我们使用C++编程语言来实现该功能。 首先,我们需要使用一个循环来接收从键盘输入的一系列以$为结束符的字符串。在每次循环迭代中,我们可以使用cin来接收输入,并将其存储在一个字符串变量中。 然后,我们可以使用一个计数器变量来记录数字字符的数量。我们可以使用循环迭代字符串中的每个字符,并使用isdigit函数来检查字符是否为数字。如果某个字符不是数字,我们可以将计数器加一。 最后,我们可以在循环结束后输出计数结果。 以下是一个示例代码: ```cpp #include <iostream> #include <string> int main() { std::string input; int count = 0; std::cout << "请输入一系列以$为结束符的字符串:\n"; while (true) { std::cin >> input; if (input == "$") { break; } for (char c : input) { if (!isdigit(c)) { count++; } } } std::cout << "数字字符的计数结果是:" << count << std::endl; return 0; } ``` 在上述示例代码中,我们使用了一个无限循环,直到输入的字符串为"$"时才会跳出循环。然后,我们使用一个嵌套的循环来迭代每个字符串中的字符,并使用isdigit函数检查数字字符。 最后,我们输出计数结果并结束程序。 当我们运行程序并从键盘输入一系列字符串时,它将计算并显示出数字字符的数量。 ### 回答3: 要实现从键盘输入一系列以$为结束符的字符串,并对其中的数字字符进行计数,可以按照如下步骤进行: 1. 定义一个计数器变量,用于记录数字字符的数量,初始值为0。 2. 提示用户输入字符串,直到用户输入" $"为止。 3. 在用户输入的字符串中遍历每个字符。 4. 判断当前字符是否为数字字符。可以使用内置函数isdigit()来检测当前字符是否为数字。如果当前字符不是数字,则将计数器变量加1。 5. 继续遍历下一个字符。 6. 最后,输出计数器变量的值,即为数字字符的数量。 以下是示例代码: ```python count = 0 # 计数器变量,记录数字字符的数量 while True: string = input("请输入字符串(以$为结束符):") if string == "$": break for char in string: if not char.isdigit(): # 检测当前字符是否为数字 count += 1 print("数字字符的数量为:", count) ``` 这段代码将会循环提示用户输入字符串,直到输入的字符串为"$"为止。在每个字符串中,它将遍历每个字符并检查是否为数字字符。最后输出计数器变量的值,即为数字字符的数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值