C++ 检查节点输入验证的整数
我试图在程序中已经预定义了数据的地方进行输入验证。当我希望用户输入一个新的员工id时,我希望它检查该id是否已经在数据库中,如果是,我如何继续循环它,直到用户输入一个不在数据库中的idC++ 检查节点输入验证的整数,c++,C++,我试图在程序中已经预定义了数据的地方进行输入验证。当我希望用户输入一个新的员工id时,我希望它检查该id是否已经在数据库中,如果是,我如何继续循环它,直到用户输入一个不在数据库中的id struct employee { int empNo, salary, performance; string name, phoneNo, address, depNo, depName; employee* next; employee* previous; } *temp,
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);
您可以这样做:
int输入员工编号(){
整数;
做{
数量;
bool fail=cin.fail();
如果(失败)
你可以这样做:
int输入员工编号(){
整数;
做{
数量;
bool fail=cin.fail();
如果(失败)
cout建议的输入验证方法是,将所有内容放入字符串中,如定义string inputLine
并使用std::getline()
,然后使用std::istringstream(inputLine)
对其进行解析。如果要继续验证,直到验证成功,请将下面的代码放入循环中
if ( stringStream >> ID >> std::ws && stringStream.get() == EOF)
//At this point you have Employee Id in ID variable , Now you can do database check.
if(validate(ID)) // if validation succeeds.
break; // Break out of loop, otherwise continue till you get correct input.
建议的输入验证方法是,将所有内容都放在一个字符串中,比如定义string inputLine
并使用std::getline()
,然后使用std::istringstream(inputLine)
对其进行解析。如果要继续,直到验证成功,请将下面的代码放入循环中
if ( stringStream >> ID >> std::ws && stringStream.get() == EOF)
//At this point you have Employee Id in ID variable , Now you can do database check.
if(validate(ID)) // if validation succeeds.
break; // Break out of loop, otherwise continue till you get correct input.
definetemp
和head
变量在哪里?在代码中,您只需要一个while循环。可能您在while(verify==true)时被困在这个循环中//如果用户键入了在数据库`@MatthieuH中找到的id,我已经修改了临时和头部定义的代码variables@NikitaSmirnov如果我使用if verify==true,系统只检测一次错误,而不是多次错误,其中definetemp
和head
变量?在代码中,您只需要一个while循环。可能您被困在这个循环中,`while(verify==true)//如果用户键入了在数据库`@MatthieuH中找到的id,我已经修改了临时和头部定义的代码variables@NikitaSmirnov如果我使用if verify==true,系统只检测一次错误,而不是多次在代码中我看到,当它说在数据库中找到员工时,它不会拒绝输入,我想在如果一个人键入数据库中已经存在的id,它应该拒绝并要求用户在您的代码中找到一个新的id,我看到当它说在数据库中找到员工时,它不会拒绝输入,我想做的是当一个人键入数据库中已经存在的id时,它应该拒绝并要求用户找到一个新的id