2016-10-06 12 views
0

私は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

なぜあなたの構造体の属性はすべて「SIZE」の量ですか? – Treycos

+1

あなたの[MCVE]を提示してください –

+0

あなたがC++を学んでいるなら、属性として構造体の要素を含み、メソッドとして関数を使用することを検討してください。 – Treycos

答えて

0

あなたのプログラムを実行しましたが、コンパイラが意図せずにコンパイルをやめようとする前に、20を超えるエラーが発生しました。それらのすべては、cout、cin、string、およびsetwを使用してコード行に接続されています。これを修正するには、stdネームスペースでこれらのキーワードを使用する必要があります。また、iostreamヘッダーファイル(#include <iostream>)を含める必要があります。これらすべてのキーワードの前にstd::と書くことができます(つまり、std::cout)、またはプリプロセッサー(using namespace std;)で名前空間を宣言することができます。また、std::setwの場合は、iomanipヘッダーファイル(#include <iomanip>)を含める必要があります。その後、メニュー関数には引用符がなく、メイン関数の最後にはカッコがありません。 (宣言に構造体メンバのサイズを初期化するのにもいくつかの問題がありますが、私はそれをあなたに任せます)。

+0

私はそれを残念にしていました - 私はコードを少し小さくして人々に読ませようとしていました。私の間違い、私はそれを取り除くべきではありませんでした。 –