简体   繁体   English

c ++循环永远运行而无需等待输入

[英]c++ loop runs forever without waiting for input

I am trying to write code that will loop and input user input to a class and print out a chart. 我正在尝试编写将循环并将用户输入输入到类并打印出图表的代码。 This is my main method: 这是我的主要方法:

int main()
{
    Company productMatrix;
    int inputNumber = 0;
    cout << "enter the salesman id or -1 to quit." << endl;
    cin >> inputNumber;
    while(inputNumber != -1)
    {
        int salesman = inputNumber;
        cout << "enter the product id." << endl;
        cin >> inputNumber;
        int product = inputNumber;
         cout << "enter the amount sold." << endl;
        cin >> inputNumber;
        double dollarValue = inputNumber;
        productMatrix.inputSales(salesman, product, dollarValue);
        cout << "enter the salesman id or -1 to quit." << endl;
        cin >> inputNumber;
    }
    productMatrix.printChart();
    cout << "Goodbye!";
    return 0;
}

when I run the program, it will let me input one set of data and then loop forever without waiting for me to stop. 当我运行程序时,它会让我输入一组数据,然后永远循环而不等我停下来。 This is what it looks like: 这就是它的样子:

enter the salesman id or -1 to quit.
3
enter the product id.
2
enter the amount sold.
55.99
enter the salesman id or -1 to quit.
enter the product id.
enter the amount sold.
enter the salesman id or -1 to quit.
enter the product id.
enter the amount sold.
// etc...

I'm guessing there is something wrong with my loop. 我猜我的循环有问题。 How can I fox this? 我怎么能这个狐狸呢?

You're writing a double 55.99 to an integer, so cin takes 55 and it has '.' 你正在写一个双55.99到整数,所以cin需要55,它有'.' in the buffer which is always !=-1 but never gets read into as integer. 在缓冲区中始终是!=-1但永远不会被读入整数。

The problem is with following line. 问题在于以下几行。

double dollarValue = inputNumber;

inputNumber is a integer type and dollar value is a float. inputNumber是一个整数类型,而美元值是一个浮点数。 So there is a type mismatch. 所以存在类型不匹配。 You can create another variable like dollarInput and store the dollar value there 您可以创建另一个变量,如dollarInput,并将美元值存储在那里

inputNumber is an int . inputNumber是一个int But you entered a value (55.99) that can not be interpreted as an int . 但是您输入的值(55.99)无法解释为int This put cin into an error state. 这使得cin陷入错误状态。 Until the error is cleared, all future operations with cin fail. 在错误被清除之前, cin所有未来操作都会失败。 So it doesn't wait for your input, and the variables retain their values, and you can never get that -1 that the loop needs to terminate. 因此它不会等待您的输入,并且变量保留它们的值,并且您永远不会得到循环需要终止的-1

To check for an error, just use a plain old if statement: 要检查错误,只需使用普通的旧if语句:

if (cin) {
    // cin is okay
}
else {
    // cin is not okay
}

You can also be a little more concise and put your input operation directly in an if statment: 您也可以更简洁一点,并将输入操作直接放在if语句中:

if (cin >> inputNumber) {

To clear the error: 要清除错误:

cin.clear();

You will also probably need to clear the input stream, otherwise the erroneous input will remain in the input buffer, and cin will just try to read it again: 您可能还需要清除输入流,否则错误的输入将保留在输入缓冲区中,而cin将尝试再次读取它:

cin.ignore(); // discard one character from the input buffer
// or
cin.ignore(N); // discard N characters from the input buffer

Anyway, that's the cause of the infinite loop. 无论如何,这是无限循环的原因。 But if you had just input directly into a double , instead of an int , you wouldn't have seen this issue. 但是如果你只是直接输入一个double而不是int ,你就不会看到这个问题。 Isn't that what you want anyway? 这不是你想要的吗?

To add to prajmus's answer, you can see the additional 'junk' in the input stream by adding the following 'cin' read: 要添加到prajmus的答案,您可以通过添加以下'cin'读取来在输入流中看到额外的“垃圾”:

...
double dollarValue = inputNumber;
productMatrix.inputSales(salesman, product, dollarValue);
cout << "enter the salesman id or -1 to quit." << endl;

double myDbl;
cin >> myDbl;
cout << "read the following double:" << myDbl << endl;
...

The added "cin >> myDbl" will read the '.99' from the input stream and the added cout will yield: 添加的“cin >> myDbl”将从输入流中读取'.99',添加的cout将产生:

0.99

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

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