c++ - 检查整数以进行节点的输入验证

标签 c++

我正在尝试在程序中已有预定义数据的地方进行输入验证。当我希望用户键入新的员工ID时,我希望它检查数据库中是否已存在该ID,如果是,我如何继续循环播放该ID,直到用户键入不在数据库中的ID?

struct employee {
    int empNo, salary, performance;
    string name, phoneNo, address, depNo, depName;
    employee* next;
    employee* previous;
} *temp, *head, *tail, *newhead, *newtail, *newnode;
void validate()
{
    cout << "Input not accepted, please try again!" << endl;
    cin.clear();
    cin.ignore(INT_MAX, '\n');
}
int main()
{
    int choice = 1;
    int num, salary, performance = 0;
    string name, hpnum, depName, depNo, address;
    bool execute = true;
    insertemployeerecord(0002, "Ethan Tan", 16000, "017-2399193", "Kuala Lumpur, Malaysia", "F001", "Finance", 2);
    insertemployeerecord(0003, "Nikshaen Kumar", 15000, "016-9188131", "Los Angeles, California", "A001", "Audit", 3);
    insertemployeerecord(0001, "Koughen Mogan", 17500, "014-1233241", "Los Angeles, California", "F001", "Finance", 4);
    while (execute)
    {
        cout << "..........................................................." << endl;
        cout << "                    EMERGE EMPLOYEE SYSTEM                 " << endl;
        cout << "..........................................................." << endl;
        cout << endl;
        cout << "1. Add an Employee Record" << endl;
        cout << "2. Display All Records" << endl;
        cout << "3. Search by Employee ID" << endl;
        cout << "4. Search by Employee overall performance" << endl;
        cout << "5. Sort and display by Employee ID in ascending order" << endl;
        cout << "6. Sort and display by Employee Salary in ascending order " << endl;
        cout << "7. Sort and display by Employee Overall Performance in ascending order " << endl;
        cout << "8. Modify an Employee Record" << endl;
        cout << "9. Delete an Employee Record" << endl;
        cout << "10. Calculate salary package of an employee" << endl;
        cout << "11. Exit" << endl;
        cout << endl << endl;

    cout << "Select your option: ";
    cin >> choice;
    if (choice == 1)
    {
        system("cls");
        cout << "Enter employee number: ";
        cin >> num;
        while (!cin >> num) //to see if user types anything besides number
        {
            validate();
            cout << "Enter employee number: ";
            cin >> num;
        }
        temp = head;
        bool verify = true;
        if (temp != NULL)
        {
            while (temp->empNo != num)
            {
                temp = temp->next;
                if (temp == NULL)
                {
                    verify = false;
                    break;
                }
            }
            while (verify == true)  //if the user types an id that is found in the database
            {
                validate();
                cout << "Employee found in database!" << endl;
                cout << "Enter employee number: " << endl;
                cin >> num;
            }
            if (verify == false)
            {
                cin.get();
            }
        }
        cout << endl;
        cout << "Enter employee name: ";
        getline(cin, name);

如您所见,我的输出可以检测是否输入了字母,以及用户是否在数据库中输入了一个ID,但是当我输入新的ID(例如4)时,系统仍会说它从数据库
Enter employee number: a
Input not accepted, please try again!
Enter employee number: 1
Input not accepted, please try again!
Employee found in database!
Enter employee number:
2
Input not accepted, please try again!
Employee found in database!
Enter employee number:
3
Input not accepted, please try again!
Employee found in database!
Enter employee number:
4
Input not accepted, please try again!
Employee found in database!

最佳答案

您可以执行以下操作:

int enter_employee_number() {
    int number;
    do{
        cout << "Enter employee number: ";
        cin >> number;
        bool fail = cin.fail();
        if(fail)
            cout << "Input not accepted, please try again!" << endl;
     } while (fail);
     return number;
}

string enter_employee_name() {
    string name;
    do{
        cout << "Enter employee name: ";
        cin >> name;
        bool fail = cin.fail();
        if(fail)
            cout << "Input not accepted, please try again!" << endl;
     } while (fail);
     return name;
}

employee get_employee(int number) {
    // retrieve your employee and return it.
}

int main() {
    // other part of your code
    if (choice == 1) {
        system("cls");
        while(true) {
            int empNo = enter_employee_number();
            employee emp = get_employee();
            if (emp != nullptr) {
                cout << "Employee found in database!" << endl;
                string empName = enter_employee_name();
                // call method that use empName
            }
            else {
                cout << "Employee not found in database!" << endl;
            }
            cout << endl;
        }
    }
}

关于c++ - 检查整数以进行节点的输入验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59576695/

相关文章:

c++ - 使用 atomic<bool> 的简单自旋锁中的数据竞争

c++ - C++的system()是同步的吗?

c++ - 如何使用dll?

c++ - 如何用智能指针构造树结构?

c++ - 如何在嵌套列表初始化中从 map<> 中辨别 vector<>?

c++ - 如何在 Mac OS X (C++) 中使用 dylib

c++ - While/Switch 语句奇怪的输出

c++ - 分支预测和分支目标预测优化

c++ - 未知错误可能是由命名冲突引起的?

C++ ODB 支持不同的 DBMS