简体   繁体   English

C ++检查cin的输入以获取正确的数据

[英]C++ Checking input of cin for proper data

Hello I am working on a program that is checking the users input and asking for the user to reinput if the data does not match the type the program is looking for (In this case I am looking for the double data type). 您好,我正在开发一个程序,该程序正在检查用户输入并要求用户重新输入数据是否与程序所寻找的类型不匹配(在这种情况下,我正在寻找double数据类型)。 Below is my current code. 下面是我当前的代码。

#include <iostream>
using namespace std;


int main()
{
    //Declare Variables and define.
    double var1,
           var2,
           var3,
           var4,
           var5,
           sum,
           avg;



        cout << "Input Five Values to be averaged. Press Enter to Continue" << endl;

    do {
        cin.clear();
        cin.ignore();
        cout << "Input first value:" << endl;
        cin >> var1;


    } while(cin.fail());

    do {
        cin.clear();
        cin.ignore();
        cout << "Input second value:" << endl;
        cin >> var2;


    } while (cin.fail());



    do {
        cin.clear();
        cin.ignore();
        cout << "Input third value:" << endl;
        cin >> var3;


    } while (cin.fail());



    do {
        cin.clear();
        cin.ignore();
        cout << "Input Fouth value:" << endl;
        cin >> var4;


    } while (cin.fail());


    do {
        cin.clear();
        cin.ignore();
        cout << "Input fifth value:" << endl;
        cin >> var5;


    } while (cin.fail());




//Sum the values.
sum = var1 + var2 + var3 + var4 + var5;

//Calculate the Average of Sum.
avg = sum / 5;

// Display the results.
cout << "The average is: " << avg << endl;

system("pause");


return 0;
}

The problems come when I input "aa", "s2", of "2ss" it will not ask for the data again once, but for as many characters are in the cin, or in the case of "s2"/"22s" it will go to the next input request. 当我输入“ 2ss”的“ aa”,“ s2”或“ s2” /“ 22s”的字符数过多时,问题就不会再次出现。它将转到下一个输入请求。 Why is this happening and how can I correct the behavior? 为什么会发生这种情况,我该如何纠正这种行为?

Thank you in advance! 先感谢您!

Instead of cin.ignore(); 代替cin.ignore(); , use: , 采用:

cin.ignore(std::numberic_limits<std::streamsize>::max(), '\n');

This will ignore either 256 characters, or every character until a newline (whichever comes first). 这将忽略256个字符或每个字符,直到换行符为止(以先到者为准)。 As of now, you only clear one character from the buffer, which--as you've found--isn't always enough. 截至目前,您仅从缓冲区中清除了一个字符,正如您所发现的那样,这并不总是足够的。

Thank you to @Arnav and @Dietmar for the proper number of characters to ignore! 感谢@Arnav和@Dietmar提供了忽略的正确字符数!

The other, better answers explained how to make it stop. 其他更好的答案说明了如何使其停止。 This is why it's happening: 这就是它发生的原因:

double d;
cin >> d;

will read as many "double-like" characters as possible and then stop. 会读取尽可能多的“双重字符”字符,然后停止。 So, for example, if you entered "2ss" the extractor would consume the "2" and leave the get pointer at the first 's'. 因此,例如,如果您输入“ 2ss”,则提取器将使用“ 2”,并将get指针保留在第一个“ s”处。 If you enter "aa" the extractor consumes nothing. 如果输入“ aa”,则提取器不消耗任何东西。 What happens to the extra characters (as well as the newline) is up to you. 多余的字符(以及换行符)如何处理取决于您。

Since you didn't consume all the characters in the input buffer, when your loop cycled, one of the remaining characters was consumed by ignore() and you tried again, until they were all gone. 由于您没有消耗输入缓冲区中的所有字符,因此当循环循环时,其余字符之一被ignore()消耗,然后再次尝试,直到所有字符消失。

The actual requirement you were trying to implement was either that only a number could be entered on a line (in which case, the suggestion of getline / strod is appropriate), or that all extraneous characters on a line should be silently swallowed, in which case the more precise ignore() specification is correct. 您尝试实现的实际要求是要么只能在一行上输入一个数字(在这种情况下,建议使用getline / strod是适当的),要么应该默默地吞下一行上的所有多余字符,在这种情况下,更精确的ignore()规范是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM