cin.get();当我把它放在if语句中时不起作用

时间:2014-02-06 16:57:31

标签: c++

我使用cin.get()让程序暂停并等待用户输入,并且工作正常。我把它放在if语句中的那一刻,它只是跳过了“等待”期并继续使用代码?我该怎么解决这个问题。这是不起作用的部分。

  do
    {
        cout << "\n\n\nEnter the number of one of the following and I will explain!\n";
        cout << "1.integer  2.boolian   3.floats   4.doubles   5.character";
        cout << "\n\n[when you are done type 'done' to continue]\n\n";
        cin >> option;

        if (option = 1);
        {
            cout << "\nInteger is the variable abbreviated as 'int' this allows C++ to only";
            cout << "\nreadwhole and real numbers \n\n";
            cin.get(); //this is the part where it just skips.. it should wait
        }

    } while (var = 1);

1 个答案:

答案 0 :(得分:1)

问题是cin >> option将提取输入流中的任何整数但会留下以下的换行符(在键入值后命中输入)。当你执行cin.get()时,它只是提取已存在的换行符。像这样的许多其他问题,解决方案是在你提取到option之后清空输入流:

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

您还在使用作业(=)进行比较(==)。