我正在尝试在程序中已有预定义数据的地方进行输入验证。当我希望用户键入新的员工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/