仅接受整数输入 [英] Accept only integer to input

查看:125
本文介绍了仅接受整数输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个类似的问题被问了很多遍了,但是我仍然找不到我的解决方案。

I found this similar question being asked so many times but I still couldn't find a solution for mine.

在我的情况下,我想显示一些东西用户输入1到5之间的数字时,如果输入错误的字符(例如字符 3g, 3。, b3和任何浮点数字),则会出错。

In my case, I want to display something when the user enters a number from 1 - 5, give an error when he inputs something wrong like characters, "3g", "3.", "b3" and any float number.

我尝试了下面的代码,但它创建了许多其他问题。就像我输入 3g 3.5 一样,它只会使用 3 并忽略其余部分,因此(!cin)根本不起作用。

I tried the code below, but it created so many other problems. Like if I enter 3g or 3.5, it'll only take the 3 and ignore the rest so the (!cin) doesn't work at all.

第二,如果我输入类似字符的内容, __ userChoice 会自动转换为 0 ,程序会打印出请选择一个从1到5的数字。 而不是输入无效,请输入一个整数。\n

Second, if I input something like a character, the __userChoice will be automatically converted into 0 and the program prints out "Please select a number from 1 to 5." instead of "Invalid input, please input an integer number.\n", which is what I want.

cout << "Please select: ";
cin >> __userChoice;
if (__userChoice > 0 && __userChoice < 5) {
    cout << "You select menu item " << __userChoice <<". Processing... Done!\n";
}
else if (__userChoice == 5) {
    Finalization(); //call exit
}
else if (__userChoice <= 0 || __userChoice > 5) {
    cout << "Please select a number from 1 to 5.\n";
}
else (!cin) {
    cout << "Invalid input, please input an integer number.\n";
}
cin.clear();
cin.ignore(10000, '\n');


推荐答案

operator>> 不能保证在发生故障时输出有意义的整数值,但是在评估 __ userChoice 以及如果是结构化的, else(!cin)支票将永远无法实现。但是,即使 operator>> 成功,您也不会检查用户是否仅输入了一个整数。

operator>> is not guaranteed to output a meaningful integer value if a failure occurs, but you are not checking for failure before evaluating __userChoice, and the way your ifs are structured the else (!cin) check will never be reached. But even if operator>> is successful, you are not checking if the user entered more than just an integer.

要执行您要的操作,您应该从 std :: cin 读入 std :: string 首先使用 std :: getline() ,然后使用 std :: istringstream std:stoi() (或等效)将字符串转换为 int 和错误检查。

To do what you are asking for, you should read from std::cin into a std::string first using std::getline(), and then use std::istringstream or std:stoi() (or equivilent) to convert the string to an int with error checking.

例如:

bool strToInt(const std::string &s, int &value)
{
    std::istringstream iss(s);
    return (iss >> value) && iss.eof();

    // Or:

    std::size_t pos;
    try {
        value = std::stoi(input, &pos);
    }
    catch (const std::exception &) {
        return false;
    }
    return (pos == input.size());
}

...

std::string input;
int userChoice;

std::cout << "Please select: ";
std::getline(std::cin, input);

if (strToInt(input, userChoice))
{
    if (userChoice > 0 && userChoice < 5)
    {
        std::cout << "You selected menu item " << userChoice <<". Processing... Done!\n";
    }
    else if (userChoice == 5)
    {
        Finalization(); //call exit
    }
    else
    {
        std::cout << "Please select a number from 1 to 5.\n";
    }
}
else
{
    std::cout << "Invalid input, please input an integer number.\n";
}

这篇关于仅接受整数输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆