结构数组 - 不拉动数据的功能

时间:2016-10-06 14:56:31

标签: c++ arrays struct

我正在学习BscCS所以我熟悉编程概念,但是我似乎总是卡在结构和结构数组上。

我需要将并行数组转换为结构数组。我已经这样做了,但由于某种原因,信息没有正确地发送到函数,程序不断崩溃。

如果有什么我遗失的话,你能帮我找出我错的地方吗?我不需要确切的答案代码,只需要一些指导。这是代码:

#define SIZE 3
struct Employee {

string firstName[SIZE];
string lastName[SIZE];
int id[SIZE];
int hoursWorked[SIZE];
int payRate[SIZE];
int stat[SIZE];

};

//Functions
int menu();
void printReport(Employee & employees);
void search(Employee & employees);
void calculatePay(Employee & employees);
void orderByLastName(Employee & employees);
void orderByid(Employee & employees);
void printActive(Employee & employees);
void printInactive(Employee & employees);

//Display Main Menu
int menu()
{
int choice;
cout << "1. Print out Employee Report. " << endl;
cout << "2. Search Employee Records. " << endl;
cout << "3. Display the Report in Sorted order on Last Name or ID. << endl;
cout << "4. Calculate Pay. " << endl;
cout << "5. Display Active Employees." << endl;
cout << "6. Display Inactive Employees." << endl;
cout << "7. Quit" << endl;
cout << "Enter your choice. ";
cin >> choice;

return choice;
}

//Display the employee data in a formatted order
void printReport(Employee & employees)
{
cout << setw(10) << "First Name" <<
    setw(10) << "Last Name" <<
    setw(10) << "ID" <<
    setw(10) << "Hours" <<
    setw(10) << "Rate" <<
    endl;
ios::fixed;
for (int index = 0; index < SIZE; index++)
    cout << setw(10) << employees.firstName[index] <<
    setw(10) << employees.lastName[index] <<
    setw(10) << employees.id[index] <<
    setw(10) << employees.hoursWorked[index] <<
    setw(10) << employees.payRate[index] << endl;
}

//Search for employee ID
//Show "Not Found" if unable to locate
void search(Employee & employees)
{
bool found = false;
int idNumber;
int pos = -1;

cout << "Enter id number ";
cin >> idNumber;

for (int index = 0; index < SIZE && !found; index++)
{
    if (employees.id[index] == idNumber)
    {
        found = true;
        pos = index;
    }
}

if (!found)
    cout << "Not Found. " << endl;
else
    cout << setw(10) << employees.firstName[pos] <<
    setw(10) << employees.lastName[pos] <<
    setw(10) << employees.id[pos] <<
    setw(10) << employees.hoursWorked[pos] <<
    setw(10) << employees.payRate[pos] << endl;
} 

//Calculate total weekly pay
void calculatePay(Employee & employees)
{
cout << setw(10) << "First Name" <<
    setw(10) << "Last Name" <<
    setw(10) << "ID" <<
    setw(10) << "Hours" <<
    setw(10) << "Rate" <<
    setw(10) << "Total Pay" <<
    endl;
ios::fixed;
for (int index = 0; index < SIZE; index++)
    cout << setw(10) << employees.firstName[index] <<
    setw(10) << employees.lastName[index] <<
    setw(10) << employees.id[index] <<
    setw(10) << employees.hoursWorked[index] <<
    setw(10) << employees.payRate[index] <<
    setw(10) << employees.hoursWorked[index] * employees.payRate[index] <<     endl;
}

//Sort employee data by last name
void orderByLastName(Employee & employees)
{
for (int i = 0; i < SIZE; i++)
{
    for (int j = 0; j < SIZE - 1; j++)
    {
        if (employees.lastName[j] > employees.lastName[j + 1])
        {
            string temp = employees.lastName[j];
            employees.lastName[j] = employees.lastName[j + 1];
            employees.lastName[j + 1] = temp;

            temp = employees.firstName[j];
            employees.firstName[j] = employees.firstName[j + 1];
            employees.firstName[j + 1] = temp;

            int tempid = employees.id[j];
            employees.id[j] = employees.id[j + 1];
            employees.id[j + 1] = tempid;

            int temphours = employees.hoursWorked[j];
            employees.hoursWorked[j] = employees.hoursWorked[j + 1];
            employees.hoursWorked[j + 1] = temphours;

            int temppayrate = employees.payRate[j];
            employees.payRate[j] = employees.payRate[j + 1];
            employees.payRate[j + 1] = temppayrate;

        }
    }
 }
}

//Sort employee data by ID
void orderByid(Employee & employees)
{
for (int i = 0; i < SIZE; i++)
{
    for (int j = 0; j < SIZE - 1; j++)
    {
        if (employees.id[j] > employees.id[j + 1])
        {
            string temp = employees.lastName[j];
            employees.lastName[j] = employees.lastName[j + 1];
            employees.lastName[j + 1] = temp;

            temp = employees.firstName[j];
            employees.firstName[j] = employees.firstName[j + 1];
            employees.firstName[j + 1] = temp;

            int tempid = employees.id[j];
            employees.id[j] = employees.id[j + 1];
            employees.id[j + 1] = tempid;

            int temphours = employees.hoursWorked[j];
            employees.hoursWorked[j] = employees.hoursWorked[j + 1];
            employees.hoursWorked[j + 1] = temphours;

            int temppayrate = employees.payRate[j];
            employees.payRate[j] = employees.payRate[j + 1];
            employees.payRate[j + 1] = temppayrate;

        }
    }
 }
}

void printActive(Employee & employees) 
{

bool found = false; 
int pos = -1;   

for (int index = 0; index < SIZE && !found; index++)
{
    if (employees.stat[index] == 1)
    {
        found = true;
        pos = index;            
    }

    if (!found) {

        cout << "No active employees." << endl;
    }

    else {
        cout << setw(10) << employees.firstName[pos] <<
        setw(10) << employees.lastName[pos] <<
        setw(10) << employees.id[pos] <<
        setw(10) << employees.hoursWorked[pos] <<
        setw(10) << employees.payRate[pos] << endl;
    }           
 }  
}

void printInactive(Employee & employees) 
{

bool found = false;
int pos = -1;

 for (int index = 0; index < SIZE && !found; index++)
 {
    if (employees.stat[index] == 0)
    {
        found = true;
        pos = index;
    }

    if (!found) {

        cout << "No inactive employees." << endl;
    }

    else {
        cout << setw(10) << employees.firstName[pos] <<
        setw(10) << employees.lastName[pos] <<
        setw(10) << employees.id[pos] <<
        setw(10) << employees.hoursWorked[pos] <<
        setw(10) << employees.payRate[pos] << endl;
    }
 }
}

int main()
{

struct Employee employees[SIZE];

//Read first and last name from user
for (int index = 0; index < SIZE; index++)
{
    cout << "Enter first name : ";
    cin >> employees[index].firstName[index];

    cout << "Enter last name : ";
    cin >> employees[index].lastName[index];

    //Read ID, hours, and pay rate from user
    while (employees[index].id[index] < 0)
    {
        cout << "Enter id : ";
        cin >> employees[index].id[index];

        if (employees[index].id[index] < 0)
        {
            cout << "Invalid Id number. " << endl;
            cout << "Enter id : ";
            cin >> employees[index].id[index];
        }
    }

    while (employees[index].hoursWorked[index] < 0)
    {
        cout << "Enter hours worked : ";
        cin >> employees[index].hoursWorked[index];

        if (employees[index].hoursWorked[index] < 0) 
        {
            cout << "Invalid hours. " << endl;
            cout << "Enter hours worked : ";
            cin >> employees[index].hoursWorked[index];
        }           
    } 

    while (employees[index].payRate[index] < 0)
    {
        cout << "Enter Pay Rate : ";
        cin >> employees[index].payRate[index];

        if (employees[index].payRate[index] < 0) 
        {
            cout << "Invalid pay rate. " << endl;
            cout << "Enter Pay Rate : ";
            cin >> employees[index].payRate[index];
        }
    }

    while (employees[index].stat[index] < 0)
    {
        cout << "Enter your status. (0 - Inactive, 1 - Active) : ";
        cin >> employees[index].stat[index];

        if (employees[index].stat[index] < 0) 
        {               
            cout << "Enter your status : ";
            cin >> employees[index].stat[index];
        }

        cout << endl;
    }
 }

 //Loop to display menu options until user quits program
 while (true)
 {
    //Call menu with options
    int ch = menu();

    //Different options to choose
    switch (ch)
    {
    case 1:
        printReport(employees[SIZE]);
        break;
    case 2:
        search(employees[SIZE]);
        break;
    case 3:
        int sortType;
        cout << "1.Sort by Last Name." << endl;
        cout << "2.Sort by ID." << endl;
        cout << "Enter your choice. ";
        cin >> sortType;

        if (sortType == 1)
            orderByLastName(employees[SIZE]); 
        else if (sortType ==2)
            orderByid(employees[SIZE]);
        break;
    case 4:
        calculatePay(employees[SIZE]);
        break;
    case 5: printActive(employees[SIZE]);
    case 6: printInactive(employees[SIZE]);
        break;
    case 7:
        exit(0);
    }
 }

 system("pause");
 return 0;
}

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我运行了你的程序,在编译器不自觉地停止尝试编译之前,我收到了20多个错误。所有这些都使用cout,cin,string和setw连接到代码行。您需要将这些关键字与std命名空间一起使用来解决此问题,并且需要包含iostream头文件(#include <iostream>)。您可以在所有这些关键字(即std::)前面写std::cout,也可以在预处理器(using namespace std;)中声明命名空间。此外,对于std::setw,您需要包含iomanip标头文件(#include <iomanip>)。之后,您在菜单功能和主功能末尾的括号中错过了引号。 (在声明中初始化结构成员的大小也存在一些问题,但我会把它留给你。)希望这有帮助!