如何要求 NUMBER ONLY 用户输入

问题描述

所以我正在尝试制作一个菜单循环。 一切正常。 数字 1、2 和 3 都是选项。

但我想更进一步,让程序向用户询问一个数字 如果输入了字母/字符。

问题是,我遇到了一个奇怪的无限循环。 (我知道这看起来不对,我不知道从哪里开始。感谢您的帮助!)

int menuchoice; // Menu selection for user input.
int chips = 2000;
int earned = 0;

cout << "Enter a number to select: \n";
    cin >> menuchoice;

while (menuchoice > 0 || menuchoice < 4)
    {
        if (menuchoice == 1) { // Play Slots menu where credits + amount of bet player wants to play is added.
            cout << "===============================\n";
            cout << "Enter amount you want to bet \n";
            cin >> place_bet; // check for legality in menu 2 (not made yet)

        };
        if  (menuchoice == 2) {
            cout << "Thank you for playing !!! " << name << endl;
            break;
            // exit menu displaying "THANK YOU,press any key to exit.
        };
        if  (menuchoice == 3) {

            cout << "Your chips $" << chips << endl;
            cout << "Chips earned $" << earned << endl;
            cout << "\n";
            cout << "Enter a valid number from 1 to 3 to select: \n";
            cin >> menuchoice;
        };
        if ( menuchoice >= 4 ) {
            cout << "Enter a valid number from 1 to 3\nin order to select a menu option: \n";
            cin >> menuchoice;
        };
        if (menuchoice == 0) {
            cout << "Enter a valid number from 1 to 3\nin order to select a menu option: \n";
            cin >> menuchoice;
        };
        if (menuchoice != menuchoice) {
            cout << "Enter a valid number from 1 to 3\nin order to select a menu option: \n";
            cin >> menuchoice;

        }

```

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...