【问题标题】:C++ codeblock Linux console menuC++ 代码块 Linux 控制台菜单
【发布时间】:2021-11-04 17:36:33
【问题描述】:

我正在尝试使用 CodeBlock 在 C++ 上的控制台上构建一个菜单。用于课程。

我想验证用户输入的垃圾。例如,用户必须输入一个数字。如果他输入了错误的号码,没问题,程序运行并继续。但是如果他输入了一个字母或一些垃圾,程序就会无限循环启动。

我无法使用系统(暂停),因为我在 Linux 上编程。 我尝试了一些类似 cin.get() 的代码或使用 cin.get() 做 while 但没有结果。

这是我的代码:

 #include <iostream>
    
    using namespace std;
    
    void showMenu()
    {
        cout << "---------------MENU --------------" << endl;
        cout << "1- Check balance :" << endl;
        cout << "2- Check deposit :" << endl;
        cout << "3- Withdraw:" << endl;
        cout << "4- Exit" << endl;
        cout << "--------------------------------" << endl;
    }
    
    int main()
    {
        int option;
        double balance = 500;
    
        do
        {
            showMenu();
            cout << "Option: ";
            cin >> option;
            cout << "\033[2J\033[1;1H";
    
            switch(option)
            {
               case 1:
                 cout << "Balance is: " << balance << " $" << endl;
                 break;
    
               case 2:
                 cout << "Deposit amount: " << endl;
                 double depositAmount;
                 cin >> depositAmount;
                 balance += depositAmount;
                 break;
    
               case 3:
                 cout << "Withdraw amount: " << endl;
                 double withdrawAmount;
                 cin >> withdrawAmount;
    
                 if (withdrawAmount <= balance) {
                     balance -= withdrawAmount;
                 }
                 else {
                    cout << "Not enough money" << endl;
                 }
                 break;
    
               default:
                  cout << "Your choice is invalid ";
                  do {
                    cout << '\n' << "Press the Enter key to continue.";
                  }while (cin.get() != '\n');
    
            }
            
        } while(option != 4);
    
        return 0;
    }

您知道如何轻松验证用户输入的垃圾吗?

感谢您的帮助

【问题讨论】:

  • 如果输入完全是垃圾,cin &gt;&gt; option; 可能会失败。如果您使用 if (cin &gt;&gt; option) ,您可以使用 else 清理用户留下的烂摊子,使用 clear 方法清除错误标志并使用 ignore 删除错误输入。
  • 通常我会编写一个单独的函数,从用户那里获取一个整数,并且在我知道我得到一个正确范围内的有效整数之前不会返回。您可以在任何需要获取整数的地方使用此函数,避免代码重复和浪费时间。
  • 将输入作为字符串并测试转换是否成功是另一种方法。我第二次将所有这些放入一个函数中。
  • 非常感谢!我将 cin.clear() 和 cin.ignore() 放在默认部分。有用 !我尝试了一个函数,但没有成功。

标签: c++ linux menu console


【解决方案1】:

它适用于默认部分的代码:cin.clear 和 cin.ignore()。这最后一个很重要。感谢user4581301

#include <iostream>

using namespace std;

void showMenu()
{
    cout << "---------------MENU --------------" << endl;
    cout << "1- Check balance :" << endl;
    cout << "2- Check deposit :" << endl;
    cout << "3- Withdraw:" << endl;
    cout << "4- Exit" << endl;
    cout << "--------------------------------" << endl;
}


int main()
{
    int option;
    double balance = 500;

    do
    {
        showMenu();
        cout << "Option: ";
        cin >> option;
        cout << "\033[2J\033[1;1H";

        switch(option)
        {
           case 1:
             cout << "Balance is: " << balance << " $" << endl;
             break;

           case 2:
             cout << "Deposit amount: " << endl;
             double depositAmount;
             cin >> depositAmount;
             balance += depositAmount;
             break;

           case 3:
             cout << "Withdraw amount: " << endl;
             double withdrawAmount;
             cin >> withdrawAmount;

             if (withdrawAmount <= balance) {
                 balance -= withdrawAmount;
             }
             else {
                cout << "Not enough money" << endl;
             }
             break;

           default:

              cout << "\033[2J\033[1;1H"; //for clean screen with Linux
              cout << "Your choice is invalid " << endl;
              cin.clear();
              cin.ignore();
            
        }
        

    } while(option != 4);

    return 0;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 2018-01-08
相关资源
最近更新 更多