2017-05-25 3 views
-2

So bin ich versucht, zur Zeit zu lernen, C, durch eine Aufgabe arbeiten, und ich immer über die gleichen Fehler kommenMitarbeiter struct

Für Kontext:

Im erwartet ein Programm mit 4 Funktionen schreiben -

read_employee (liest den Namen, die ID und Gehalt und speichert diese in einem Mitarbeiter Variable, die dann zurückgeführt wird),

print_employee (liest die Mitarbeiter-Struktur, drucken Details Mitarbeiter),

employee_total_salary (eine Reihe von Mitarbeitern Werten in einem Parameter und Größe des Arrays in einem anderen Parameter, und gibt die Summe akzeptiert aller ihrer Gehälter.)

employee_index_search (sucht ID aus den Mitarbeitern struct)

Und das ist die psuedocode im erzählt von gehen:

1: Print the message "-- Enter Array 1 Employee Data –" 
2: Loop i for each index of test_array1 
3: Assign test_array1 at index i, the result of calling read_employee 
4: Print the message '-- Enter Array 2 Employee Data --' 
5: Loop i for each index of test_array2 
6: Assign test_array2 at index i, the result of calling read_employee 
7: Print the message "-- Test Array 1 --" 
8: Loop i for each index of test_array1 
9: Call print_employee, passing in test_array1 at index i 
10: Print 'Total : ', employee_total_salary (test_array1, 5) 
11: Print the message '-- Test Array 2 --' 
12: Loop i for each index of test_array2 
13: Call print_employee, passing in test_array2 at index i 
14: Print "Total : ", employee_total_salary (test_array2, 10) 
15: Store in i, the employee_index_search passing in test_array1 , and the id 123 and 
size 5 
16: if something was found 
17: Call print_employee passing in the located computer from testArray1 
18: else 
19: Print the message " array 1 does not contain a employee with id 123" 

Dies ist der Code, den ich geschrieben habe:

 #include <stdio.h> 

struct employee{ 
    char name[20]; 
    int emp_id; 
    float salary; 
}; struct employee e[]; 

void read_employee() 
{ 
    int i = 0; 
     printf("%dEnter Employee Name: ", i); 
     scanf("%s", &e[i].name); 
     printf("Enter in ID: "); 
     scanf("%d", &e[i].emp_id); 
     printf("Enter in Salary: "); 
     scanf("%f", &e[i].salary); 
     i++; 
} 

void print_employee(){ 
    for (int i = 0; i > 10; ++i) 
    { 
     if (e[i].salary < 4000) 
     { 
      printf("%s(%d):%f - Level A", e[i].name, e[i].emp_id, e[i].salary); 
     } 
     else if (e[i].salary > 5000) 
     { 
      printf("%s(%d):%f - Level B", e[i].name, e[i].emp_id, e[i].salary); 
     } 
     else 
     { 
      printf("%s(%d):%f", e[i].name, e[i].emp_id, e[i].salary); 
     } 
    } 
} 

//int employee_total_salary(){ 
    //int total; 
    //sizeof(e.salary); 

    //total = sizeof(e.salary) + e.salary; 
    //return total; 
//} 

int employee_index_search(){ 
    int *id; 
    int size = 5; 

    for (int i = 0; i < size; i++) 
    { 
     if (e[i].emp_id == id) 
     { 
      printf("%s(%d):%f", e[i].name, e[i].emp_id, e[i].salary); 
     } 
     else 
     { 
      printf("Array 1 does not contain an employee with ID 123"); 
      return -1; 
     } 
    } 
return 0; 
} 

int main(){ 
    int test_array1[5]; 
    int test_array2[10]; 
    int i; 

    printf("\n-- Enter Array 1 Employee Data --\n"); 
    for (int i = 0; i < 5; i++) 
    { 
     read_employee(&test_array1[i]); 
    } 
    printf("\n-- Enter Array 2 Employee Data --\n"); 
    for (int i = 0; i < 10; i++) 
    { 
     read_employee(&test_array2[i]); 
    } 
    printf("-- Test Array 1 --\n"); 
    for (int i = 0; i < 5; ++i) 
    { 
     print_employee(&test_array1[i]); 
    } 
    //printf("Total:"); 
    //gets(employee_total_salary(test_array1, 5)); 
    printf("-- Test Array 2 --\n"); 
    for (int i = 0; i < 10; ++i) 
    { 
     print_employee(&test_array2[i]); 
    } 
    return 0; 
    //printf("Total:"); 
    //gets(employee_total_salary(test_array2, 10)); 
    //employeee_index_search(); 
} 

ich folgende Fehlermeldungen erhalten:

employee.c:13:21: warning: format specifies type 'char *' but the argument has 
     type 'char (*)[20]' [-Wformat] 
     scanf("%s", &e[i].name); 
       ~~ ^~~~~~~~~~ 
employee.c:53:19: warning: comparison between pointer and integer 
     ('int' and 'int *') 
       if (e[i].emp_id == id) 
        ~~~~~~~~~~~^~~ 
employee.c:74:32: warning: too many arguments in call to 'read_employee' 
       read_employee(&test_array1[i]); 
       ~~~~~~~~~~~~~    ^
employee.c:79:32: warning: too many arguments in call to 'read_employee' 
       read_employee(&test_array2[i]); 
       ~~~~~~~~~~~~~    ^
employee.c:84:33: warning: too many arguments in call to 'print_employee' 
       print_employee(&test_array1[i]); 
       ~~~~~~~~~~~~~~    ^
employee.c:91:33: warning: too many arguments in call to 'print_employee' 
       print_employee(&test_array2[i]); 
       ~~~~~~~~~~~~~~    ^
employee.c:7:20: warning: tentative array definition assumed to have one element 
}; struct employee e[]; 

Would Kann mir jemand sagen, wo ich falsch gelaufen bin? Das Programm ruft auch nicht print_employee und drucken Sie Arrays

+0

'lesen' und' p' sind Zeigertypen !!!! –

+0

'print_employee()' hat keinen Rückgabewert. –

+0

Willkommen bei StackOverflow. Bitte nimm die [Tour]. Ich empfehle Ihnen, ein grundlegendes Tutorial zu finden, das die Funktionen des Schreibens demonstriert. Sie haben offenbar einige Details ihrer Syntax vergessen. – Yunnosch

Antwort

0
scanf("%d", &read.emp_id); 

Sie

ändern
scanf("%d", &read[i].emp_id); 

sollte (ich etwas davon bemerkte, wie ich mindestens 1-2 Werkfunktionen zuerst versuchen wollte) wie Sie read als Array von Struktur übergeben

Sie fehlen auch Klammern Argument () für main und employee_total_salary