C++ 使用 cin.ignore() 提取整数时输出奇怪



以下程序需要用户以混合分数格式"whole_numbernumerator/分母"输入,并将值分配给相应的变量。

#include<iostream>
using namespace std;
int main()
{
    int whole, numerator, denominator;
    cout << "Input format: i<space>n/d" << endl;
    cin >> whole;
    cin.ignore(1000, ' ');
    cin >> numerator;
    cin.ignore(1000, '/');
    cin >> denominator;
    cout << whole << endl;
    cout << numerator << endl;
    cout << denominator << endl;
    return 0;
}

输入 1:
123 345/678
输出1:
123
345
678

输入 2
1111111111 1111111111/1111111111
输出2:

1111111111
1111111111
1111111111
输入3:
2222222222 2222222222/222222222
输出3:

2147483647
0
0
我一直无法弄清楚为什么该程序不适用于 Input3。

您溢出了 32 位整数 (2^31-1 ~= 2.147b) 的最大值。一旦发生这种情况,cin将无法正常工作,直到您清除标志。您应该检查错误,但短期解决方案是使您的号码无符号,或使用 64 位号码,例如 int64_t .您也不需要忽略该空间,因为默认情况下cin会跳过它。

您可以实现类似于此处的内容以确保有效的输入,但需要对其进行定制以适合您的特定输入格式。也许使用重载运算符将这三者封装到一个类型中,该运算符根据格式输入每个运算符会使语法更适合,因此您可以将示例中的age替换为MixedNumber对象。

我会将这样的东西视为一种通用的方法:

template <typename T> //any type will work
void getValidInput (T &var, std::string prompt = "Input: ") {
    while ((std::cout << prompt) && !(std::cin >> var)) { //if cin fails...
        std::cin.clear();                 //clear flag and discard bad input
        std::cin.ignore (std::numeric_limits<std::streamsize>::max(), 'n');
        std::cout << "Invalid input; please re-enter.n"; //let the user know
    } 
}

然后你可以让你的程序如下:

struct MixedNumber { //a data structure, so it's like using plain variables
    int64_t whole; 
    int64_t numerator;
    int64_t denominator;
};
std::istream &operator>> (std::istream &in, MixedNumber &num) { //so cin works
    in >> num.whole >> num.numerator;
    in.ignore(); //yes, you could enforce the format a bit more
    in >> num.denominator;
    return in;
}
int main() {
    MixedNumber num; //easy to "make" a mixed number, a constructor works well too
    getValidInput (num, "Input format: i<space>n/d: "); 
    std::cout << num.whole << 'n' << num.numerator << 'n' << num.denominator;
}

最新更新