1

我已经搜索了这个答案,似乎没有人知道如何解决这个错误。我希望输入严格地是一个 int。如果输入是双精度,我希望它发送错误。

int creatLegs = 0;
string trash;
bool validLegs = true;
do
{
    cout << "How many legs should the creature have? ";
    cin >> creatLegs;

    if(cin.fail())
    {
        cin.clear();
        cin >> trash; //sets to string, so that cin.ignore() ignores the whole string.
        cin.ignore(); //only ignores one character
        validLegs = false;
    }

    if (creatLegs > 0)
    {

        validLegs = true;
    }

    if (!validLegs)
    {
        cout << "Invalid value, try again.\n";
    }

} while (!validLegs);

它似乎几乎可以工作。它发送错误,但仅在进入下一个循环之后。我怎样才能解决这个问题?为什么它仍然显示错误消息但在显示之前仍然继续?

4

4 回答 4

2

输入可以是整数或浮点数的表示形式以外的其他东西。

请记住,数字不是它们的表示:(9十进制),017(八进制,à la C),0b1001(二进制,à la Ocaml),IX(罗马符号),8+1(算术表达式),neuf(法语)都是相同的表示第九号。

所以你必须决定你是否接受这样的输入9 x,或者9 (在数字后面有几个空格),......更一般地,你必须定义什么是可接受的输入(以及输入是否在行尾结束,如果应该接受空格或标点符号等...)。

您可以读取整行(例如使用std::getline)并使用 eg sscanf%n控制格式有用,返回的项目计数也是如此sscanf)或std::stol(使用结束指针)来解析它

另请注意,您的问题的措辞(“区分 int 和 double”)是错误的。C++中没有单一的 "intdouble" 类型(但int它是一种标量类型,并且double是 C++ 中的一种标量类型,您可以定义class带有标记的联合的 a来保存它们中的任何一个)。AFAIU,如果您声明int x; 然后使用std::cin >> x;用户输入12.64点和64之后的数字,则不会被解析,x将变为 12。

于 2015-04-03T06:42:24.047 回答
0

这个问题已经有一个公认的答案,但是我将提供一个解决方案来处理所有整数,即使是那些表示为浮点数(没有小数部分)的数字,并拒绝包含除空格之外的任何内容的输入数字。

可接受值的示例,这些都代表数字 4:

4
4.
4.0
+4
004.0
400e-2

拒绝值的示例:

3.999999
4.000001
40e-1x
4,
#include <iostream>
#include <sstream>
#include <cctype>
#include <string>

using namespace std;

bool get_int( const string & input, int & i ) {
    stringstream ss(input);
    double d;
    bool isValid = ss >> d;
    if (isValid) {
        char c;
        while( isValid && ss >> c ) isValid = isspace(c);
        if (isValid) { 
            i = static_cast<int>(d);
            isValid = (d == static_cast<double>(i));
        }
    }
    return isValid;
}

int main( int argc, char *argv[] )
{
    int creatLegs = 0;
    bool validLegs = false;

    do
    {
        string line;
        do {
            cout << "How many legs should the creature have? ";
        } while (not getline (cin,line));

        validLegs = get_int( line, creatLegs );

        if (creatLegs <= 0)
        {
            validLegs = false;
        }

        if (not validLegs)
        {
            cout << "Invalid value, try again." << endl;
        }

    } while (not validLegs);

    cout << "Got legs! (" << creatLegs << ")" << endl;

    return 0;
}

如果你想要严格的整数(没有小数点和科学记数法),那么使用这个更简单的 get_int函数:

bool get_int( const string & input, int & i ) {
    stringstream ss(input);
    bool isValid = ss >> i;
    if (isValid) {
        char c;
        while(isValid && ss >> c) isValid = isspace(c);
    }
    return isValid;
}
于 2015-04-03T17:55:22.870 回答
0
int creatLegs = 0;
do
{
    cout << "How many legs should the creature have? ";
    cin >> creatLegs;    // trying to get integer
    if(!cin.fail())      // if cin.fail == false, then we got an int and leave loop
        break;
    cout << "Invalid value, try again.\n"; // else show err msg and try once more
    cin.clear();
} while (1);
于 2015-04-03T06:42:08.697 回答
0

我认为您应该将数据读取为字符串,然后逐个字符地检查它以验证它是否为整数 - 如果每个字符都是数字,那么我们就有整数,我们可以解析它。

流的问题是,如果您尝试读取整数但传递了小数,它会读取到点的数字。这部分是一个适当的整数,所以cin.fail()返回false

示例代码:

#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>

using namespace std;

int main() {
    int creatLegs = 0;
    bool validLegs = true;
    do
    {
        cout << "How many legs should the creature have? ";
        string input;
        getline(cin, input);

        validLegs = true;
        for (string::const_iterator i = input.begin(); validLegs && i != input.end(); ++i) {
            if (!isdigit(*i)) {
                validLegs = false;
            }
        }

        if (!validLegs)
        {
            cout << "Invalid value, try again.\n";
        } else {
            creatLegs = atoi(input.c_str());
        }

    } while (!validLegs);

    cout << creatLegs << endl;
}

这当然不是一个完美的解决方案。如果有任何前导或尾随空格(或任何其他字符,如+or -),程序将失败。但是,如果需要,您始终可以添加一些代码来处理这些情况。

于 2015-04-03T07:00:23.240 回答