【问题标题】:C++, getting a infinite loopC++,得到一个无限循环
【发布时间】:2013-11-28 21:48:27
【问题描述】:

我尝试使用 switch 做一个简单的菜单。我还想检查用户是否输入了有效的输入(仅从 1 到 4 的整数)。输入 -4 或 44 可以正常使用此检查。但是如果我输入像“w”这样的东西,它会给我一个无限循环。 我猜我需要另一个 if / else with if (!cin) blabla else 继续使用 switch。 但我不确定我是如何做到的,否则正在启动开关。

 int menu() {
        int enter;
        bool exit = false;
        do {
            cout << "Wie soll angefangen werden: " << endl; //Enter your choice
            cout << "1 - Spiel starten" << endl; // do game();
            cout << "2 - Highscore " << endl; //do score();
            cout << "3 - Quiz starten " << endl; //do quiz();
            cout << "4 - Ende " << endl; //end the programm

        cin >> enter; 

        switch (enter) {
            case 1:
                game();
                break;
            case 2:
                score();
                break;
            case 3:
                showQuizDialog();
                break;
            case 4:
                exit = true;
                break;
            default:
                cout << "Keine gültige Eingabe, nochmal: " << endl; //invalid input, again
                void flushCin();
        } //end of switch
    } while (exit == false); 

}//end of menu();

【问题讨论】:

  • 什么是flushCin?您不需要刷新已读取的流。
  • 尝试将int enter 更改为char entercase 1: 更改为case '1':
  • 两点风格。第一:将bool 与常数进行比较是没有意义的;最终条件应该只是while ( !exit )。其次,你真的应该在默认情况下添加break;

标签: c++ loops menu switch-statement infinite


【解决方案1】:

这是因为输入试图获取一个整数。当输入不是整数时,输入留在缓冲区中,所以下一次循环中相同的输入仍然存在。

此外,在默认情况下,您并没有调用flushCin 函数,而是声明它。您可能想要删除 void 关键字。我猜它做正确的事? (即调用std::cin.ignore()std::cin::clear()。)

【讨论】:

    【解决方案2】:

    读入字符串并尝试转换为int:

    #include <sstream>
    #include <string>
    using namespace std;
    
    int menu() {
            int enter;
            string str;
    
    
            bool exit = false;
            do {
                cout << "Wie soll angefangen werden: " << endl; //Enter your choice
                cout << "1 - Spiel starten" << endl; // do game();
                cout << "2 - Highscore " << endl; //do score();
                cout << "3 - Quiz starten " << endl; //do quiz();
                cout << "4 - Ende " << endl; //end the programm
    
            cin >> str; 
            istringstream buffer(str);
            buffer >> enter;
    
            switch (enter) {
                case 1:
                    game();
                    break;
                case 2:
                    score();
                    break;
                case 3:
                    showQuizDialog();
                    break;
                case 4:
                    exit = true;
                    break;
                default:
                    cout << "Keine gültige Eingabe, nochmal: " << endl; //invalid input, again
                    void flushCin();
            } //end of switch
        } while (exit == false);
    
        return enter;
    
    }//end of menu();
    

    如果将数字以外的其他内容直接输入到 int 值中,这可能不适合 int 的保留空间,并且可能导致有趣的行为。所以只需先读入一个字符串,然后再解释它。

    【讨论】:

    • 你不需要stringstreamstd::stoi 就可以完成这项工作。
    • @Mgetz 对,在 c++ 中有上千种方法可以做同样的事情;-)
    • 同意,但是在这种情况下,因为 OP 只读取 int,所以如果他们进行复杂的格式化,就不需要这么重的东西了……那就不同了。
    猜你喜欢
    • 2022-01-12
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 2021-08-31
    相关资源
    最近更新 更多