检查数字是否已在数组中的程序

Program that checks whether numbers are already in an array

本文关键字:程序 数组 数字 是否 检查      更新时间:2023-10-16

该程序应该接受来自键盘的值,并要求用户重新输入员工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(你可以稍后将同样的功能用于你将来需要编写的任何其他"这个员工存在吗?"支票。

一般来说,您希望在一个单一的万事通上拥有许多简单、易于测试的函数。