C++ 检查数字是否已在数组中的程序
该程序应该接受来自键盘的值,并要求用户重新输入员工id号的值。然而,即使我输入了正确的值,它也会继续输出“无效变量”。它只需要在已经输入值的情况下输出。例如 如果我输入“3453”作为id号,它仍然会输出“无效变量”,即使我以前没有输入该编号C++ 检查数字是否已在数组中的程序,c++,C++,该程序应该接受来自键盘的值,并要求用户重新输入员工id号的值。然而,即使我输入了正确的值,它也会继续输出“无效变量”。它只需要在已经输入值的情况下输出。例如 如果我输入“3453”作为id号,它仍然会输出“无效变量”,即使我以前没有输入该编号 #include <iostream> using namespace std; struct Employee { int idNum; double payRate; char firstName, lastNam
#include <iostream>
using namespace std;
struct Employee
{
int idNum;
double payRate;
char firstName, lastName;
};
int main()
{
int error;
const int SIZE = 5;
Employee employee[SIZE];
for (int k = 0; k < SIZE; ++k)
{
employee[k].idNum = 0;
employee[k].payRate = 0;
}
for (int count = 0; count < SIZE; ++count)
{
error = 0;
cout << "Enter the employee's id number " << endl;
cin >> employee[count].idNum;
for (int i = 0; i < SIZE; ++i)
{
if (employee[i].idNum == employee[count].idNum)
error = 1;
}
while (error == 1)
{
cout << "Invalid entry. Please enter a new id number " << endl;
cin >> employee[count].idNum;
for (int i = 0; i < SIZE; ++i)
{
error = 0;
if (employee[i].idNum == employee[count].idNum)
error = 1;
}
}
cout << "Enter the employee's pay rate " << endl;
cin >> employee[count].payRate;
cout << "Enter the employee's first name " << endl;
cin >> employee[count].firstName;
cout << "Enter the employee's last name " << endl;
cin >> employee[count].lastName;
int choice;
cout << "Enter 1 to search for an employee by id number, enter 2 to search by last name, and enter 3 to search by pay "
<< endl;
cin >> choice;
}
int choice;
cout << "Enter 1 to search for an employee by id number, enter 2 to search by last name, and enter 3 to search by pay "
<< endl;
cin >> choice;
if (choice == 1)
{
int idNumC;
cout << "Enter an id number ";
cin >> idNumC;
for (int count = 0; count < SIZE; ++count)
{
if (employee[count].idNum == idNumC)
cout << employee[count].idNum;
}
}
if (choice == 2)
{
char name;
cout << "Enter the employee's last name " << endl;
cin >> name;
for (int count = 0; count < SIZE; ++count)
{
if (employee[count].lastName == name)
cout << "ID number: " << employee[count].idNum
<< " First name: " << employee[count].firstName
<< " Last Name: " << employee[count].lastName
<< " Hourly Pay: " << endl;
}
}
if (choice == 3)
{
int name;
cout << "Enter the employee's last name " << endl;
cin >> name;
for (int count = 0; count < SIZE; ++count)
{
if (employee[count].payRate == name)
cout << "ID number: " << employee[count].idNum
<< " First name: " << employee[count].firstName
<< " Last Name: " << employee[count].lastName
<< " Hourly Pay: " << endl;
}
}
}
#包括
使用名称空间std;
结构雇员
{
int-idNum;
双倍工资率;
char firstName,lastName;
};
int main()
{
整数误差;
常数int SIZE=5;
雇员[人数];
对于(int k=0;kidNumC;
对于(int count=0;count
这肯定是真的
但是如果你
int tempId;
cin >> tempId;
for (int i = 0; i < SIZE; ++i)
{
if (employee[i].idNum == tempId)
error = 1;
}
稍后,您可以避免此问题
附录:我建议将此逻辑提取出来,并将其放在自己的函数中。这样一来,A)您不必在循环中重复它,每次重试都要在循环的几行中重复检查,它会使逻辑脱离代码的其余部分。B)您可以在以后对任何其他“此员工是否存在”使用相同的函数您将来需要填写的支票
通常,您希望在一个各行各业的单片插孔上拥有许多简单、易于测试的功能。for(int i=0;i for (int i = 0; i < SIZE; ++i)
这将检查数组中的每个元素,包括刚刚读取的元素
for (int i = 0; i < count; ++i)
for(int i=0;i
将检查每个元素(但不包括)你刚刚读过的那一个。备份你的文件,然后删除除了输入和验证代码以外的所有内容。然后用调试器或纸上的笔来完成。看看你是否能在这里的人得到答案之前弄清楚。如果你写了一个构造函数,你不需要在外部设置
idNum
或payRate
。当您检查是否已输入id时,您会检查所有数组元素,包括您刚输入的元素,以便始终找到它。可能会跳过当前项?顺便说一句,如果读取了第一个员工id,数组中有多少个?数组中总共有5个
employee[count].idNum = tempId;
for (int i = 0; i < SIZE; ++i)
for (int i = 0; i < count; ++i)