检查数字是否已在数组中的程序
Program that checks whether numbers are already in an array
该程序应该接受来自键盘的值,并要求用户重新输入员工ID号的值。但是,即使我输入正确的值,它也会不断输出"无效变量"。它只需要在已输入值的情况下输出该值。例如如果我输入"3453"作为 ID 号,即使我之前没有输入过该数字,它仍然会输出"无效变量"。
#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;
}
}
}
我的程序也不会接受名称中有多个字母的值。如果我尝试将其输入程序,程序会一直打印"无效条目",直到我按ctrl + c。
for (int i = 0; i < SIZE; ++i)
这将检查数组中的每个元素,包括您刚刚读取的元素。你可能想把
for (int i = 0; i < count; ++i)
这将检查每个元素,直到(但不包括(您刚刚阅读的元素。
in
for (int i = 0; i < SIZE; ++i)
{
if (employee[i].idNum == employee[count].idNum)
error = 1;
}
employee[count]
是您要比较的employee[i]
之一,这意味着在某些时候您将
if (employee[count].idNum == employee[count].idNum)
这保证是真的。
但如果相反,你
int tempId;
cin >> tempId;
for (int i = 0; i < SIZE; ++i)
{
if (employee[i].idNum == tempId)
error = 1;
}
然后设置
employee[count].idNum = tempId;
稍后,您可以避免此问题。
附录:我建议选择这个逻辑并将其放在自己的函数中。这样 A( 您不必在循环中重复它几行,在那里您每次重试重复检查,它使逻辑摆脱了其余代码的方式。B(你可以稍后将同样的功能用于你将来需要编写的任何其他"这个员工存在吗?"支票。
一般来说,您希望在一个单一的万事通上拥有许多简单、易于测试的函数。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 显示错误输出的简单数组排序程序
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 为什么使用数组元素查找最大数字的程序不起作用?
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 有人可以在下面的示例 C++ 程序中解释字符数组 KCHAR1 的奇怪行为吗?
- C ++程序如何返回我的数组或写入全局变量
- For 循环在尝试读取数组 c++ 时程序崩溃
- 无法将 2D 数组传递到 c++ 中的帮助程序函数中
- 将数组作为多线程应用程序中函数的返回传递
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 我编写了一个程序来显示数组的内容,但它显示的是它的地址
- C ++程序在字符数组中插入单词
- 数组 C++ 上的异常处理程序
- 第一次尝试使用new动态创建结构数组,程序挂起没有错误
- 使用终止程序的指针在数组中输入字符串
- C++引物动态数组初始化程序的数目超过大小