【问题标题】:Is that kind of controling the while() loop a good practice? C++这种控制 while() 循环是一个好习惯吗? C++
【发布时间】:2018-11-17 16:34:15
【问题描述】:

我正在做一个小型数据库项目。我已经用switch() 函数创建了一个接口,它应该是调用函数并循环,直到我选择了“EXIT”选项。我通过将指定值设置为int Loop 变量来控制循环。以这种方式处理这种循环是一种好习惯吗?如果不是,那为什么?在其他函数中,当我有多个条件时,我甚至两次使用那种变量。也许我应该做不同的事情?如果我在这种情况下使用 try(), throw(), catch() 异常是否有意义,那么它会是什么样子?这是我的一段代码:

void mainMenu() {

    vector<Employee> firmEmployees;
    vector<Intern> firmInterns;

    int Loop = 0;
    while (Loop == 0) {

        cout << endl << endl;
        cout << "================ EMPLOYEE DATABASE ================" << endl << endl;
        cout << " HIRE NEW EMPLOYEE (1)" << endl;
        cout << " MANAGE EMPLOYEES  (2)" << endl;
        cout << " HIRE NEW INTERN   (3)" << endl;
        cout << " MANAGE INTERNS    (4)" << endl;
        cout << " EXIT              (5)" << endl;
        cout << " Choose option... ";
        int option;
        cin >> option;

        if (option < 1 || option > 5 || !cin) {
            cout << endl << "---Wrong input!---";
            clearInput(); // cleaning cin
        } else {

            switch (option) {
                default:
                    break;
                case 1:
                    hireEmployee(firmEmployees);
                    break;
                case 2:
                    employeeMenu(firmEmployees);
                    break;
                case 3:
                    hireIntern(firmInterns);
                    break;
                case 4:
                    internMenu(firmInterns);
                    break;
                case 5:
                    Loop = 1;
                    break;
            }
        }
    }
}

编辑:另一个例子,更多变量。

void fireEmployee(vector<Employee>& sourceEmployee) {

    int repeat = 0;

    while (repeat == 0) {

        cout << "Enter ID to fire an employee: ";
        int id;
        cin >> id;

        if (cin.fail()) {
            clearInput();
            cout << "ID number needed!" << endl;
        } else {

            int buf = 0;

            for (auto &i : sourceEmployee) {

                if (i.getID() == id) {
                    i.Fire();
                    i.setSalary(0);
                    cout << i.getName() << " " << i.getSurname() << " (" << i.getID() << ") has been fired" << endl;
                    buf = 1;
                    repeat = 1;
                }
            }
            if (buf == 0) {
                cout << "No employee with ID: " << id << endl;

            }
        }
    }
}

【问题讨论】:

  • 如果这是 1971 年和 computers looked like this,那么这种互动是可以预料的。这些天来,我们期待更好的东西。考虑编写一个命令行解释器,您可以在其中执行hire bobfire bob 等操作。键入 2 是您在电话语音菜单上所做的。
  • @tadman 这可能是真的,但这并不是问题的真正答案。
  • 该死的。很高兴我出生于 73 年。

标签: c++ loops variables while-loop


【解决方案1】:

最佳做法是将while 循环提取到一个函数中,然后执行return 而不是Loop = 1;。那将是易于阅读和维护的显式流控制。例如:

void mainMenuLoop(vector<Employee>& firmEmployees, vector<Intern>& firmInterns) {
    for(;;) {
        cout << "\n\n================ EMPLOYEE DATABASE ================\n\n";
        cout << " HIRE NEW EMPLOYEE (1)\n";
        cout << " MANAGE EMPLOYEES  (2)\n";
        cout << " HIRE NEW INTERN   (3)\n";
        cout << " MANAGE INTERNS    (4)\n";
        cout << " EXIT              (5)\n";
        cout << " Choose option... " << flush; // Must flush here.
        int option = -1; // Assign a wrong initial option.
        cin >> option;
        switch (option) {
            case 1:
                hireEmployee(firmEmployees);
                break;
            case 2:
                employeeMenu(firmEmployees);
                break;
            case 3:
                hireIntern(firmInterns);
                break;
            case 4:
                internMenu(firmInterns);
                break;
            case 5:
                return;
            default:
                cout << "\n---Wrong input!---" << endl;
                clearInput(); // cleaning cin
                break;
        }
    }
}

void mainMenu() {
    vector<Employee> firmEmployees;
    vector<Intern> firmInterns;
    mainMenuLoop(firmEmployees, firmInterns);
}

还要注意,在

int option;
cin >> option;

如果cin &gt;&gt; option 失败option 保留其原始不确定值,这可以是可用选项之一。为它分配一个不是有效选项的初始值会更安全,例如-1

【讨论】:

  • 我不明白它的外观以及如何简化代码。我想不出另一种方法来解决我的问题。你能举个例子吗?
  • @Taknie 为您添加了一个示例。
猜你喜欢
  • 2022-12-13
  • 2014-11-05
  • 1970-01-01
  • 1970-01-01
  • 2021-02-13
  • 1970-01-01
  • 2010-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多