2017-05-28 1 views
1

So voran mit Klasse Arbeit, C lernenC - Parametername weggelassen?

ich nach meinem kurzen aus meiner Klasse Arbeit in Funktionen gestellt progressivly entschieden hatte, wie unten für Kontext gezeigt, in einem Versuch, Code Stück für Stück zu beheben:

Structure Chart

Functions Brief

psuedocode ive gesagt folgen:

use #define SIZE 3 
function : main 
----------------------- 
Local variables: 
- emp_array (an array of 3 employee detail values) 
- i (an integer used as the index for the arrays) 
- char str[20] to read in name of employee for search 
----------------------- 
1: call read_all_employee, passing in emp_array and SIZE 
2: Print the message ‘Employee details are’ 
3: call print_all_employee, passing in emp_array and SIZE 
4: Print 'Total : ', employee_total_salary (emp_array, SIZE) 
5: Print the message '—Employee with the largest salary is --' 
6: Store in i, the search_largest_salary_index passing in emp_array and SIZE 
7: Call print_employee, passing in emp_array at index i 
8: Print the message '— Enter employee name for the search--' 
9: read in the name in str array 
10: Store in i, the search_an_employee_salary passing in emp_array, SIZE and str 
11: if something was found 
12: Print the message 'The salary of xxxx is xxxx’ 
13: else 
14: Print the message "Array does not contain an employee named xxxx" 
15: Print the message '—Employee details in reverse order are --' 
16: Loop i starting from 2 to 0 for each index of emp_array 
17: Call print_employee, passing in emp_array at index i 

Aber das Programm kompiliert wird, halte ich für jede Funktion über den Fehler ‚Parametername Ausgelassene‘ kommen, wo Größe nach #define size 3 deklariert wird eingeführt wird, und ‚)‘ Name

erwartet

Dies ist der Code, den ich bisher geschrieben :

#include <stdio.h> 
#define size 3 

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

struct employee read_employee(){ 
    struct employee r_employee; 
    printf("Enter Employee Name: "); 
    scanf("%s", &r_employee.name); 
    printf("Enter ID: "); 
    scanf("%d", &r_employee.emp_id); 
    printf("Enter Salary: "); 
    scanf("%f", &r_employee.salary); 

    while (r_employee.salary < 0){ 
     printf("This is not a valid price, enter again\n"); 
     scanf("%f", &r_employee.salary); 
     } 
    return r_employee; 
} 

struct employee read_all_employee(struct employee emp_array[], int size){ 
    for (int i = 0; i < size; ++i) 
    { 
     emp_array[i] = read_employee(); 
    } 
} 

void print_employee(struct employee employee_data){ 
    printf("%s(%d): %f\n", employee_data.name, employee_data.emp_id, employee_data.salary); 
    if (employee_data.salary > 5000) 
    { 
     printf("Level A\n"); 
    } 
    if (employee_data.salary < 4000) 
    { 
     printf("Level B\n"); 
    } 
} 

float employee_total_salary(struct employee emp_array[], int size){ 
    int i; 
    float sum = 0; 
    for (int i = 0; i < size; i++) 
    { 
     sum = sum + employee_array[i].salary; 
    } 
    return sum; 
} 

int employee_index_search(struct employee emp_array[], int id, int size){ 
    int i; 
    for (int i = 0; i < size; i++) 
    { 
     if (employee_array[i].emp_id == id) 
     { 
      return i; 
     } 
    } 
    return -1; 
} 

int main(){ 
    struct employee emp_array[3]; 
    int i; 
    char str[20]; 

    printf("Line 1:\n"); 
    read_all_employee(emp_array, size); 
    printf("Employee Details are:\n"); 
return 0; 
} 

Könnte jemand bitte meinen Code bis jetzt korrigieren?

Antwort

2

Parametername

Mittel Ausgelassene, der Compiler einen Parameternamen nicht sehen, wo es erwartet ...

Dies ist, weil Sie nicht define und einen Parameternamen verwenden können, mit der selbe Name. Wenn #define size 3 der Vorprozessor Schreiben ersetzt jedes size es mit 3 im Code sieht und dann, wenn Sie eine Funktion mit dem Parameter size nennen, statt struct employee read_all_employee(..., int size), Sie struct employee read_all_employee(..., int 3) erhalten, was zu einem Argument vom Typ int ohne gültigen Namen ('3' ist kein gültiger Name).

Ich würde empfehlen, ein define mit CAPS, oder mit einem eindeutigen Namen verwenden, so dass Sie sich nicht verwirren werden, wie SIZE oder nur im Kopf behalten, dass Sie size Symbol haben und andere Parameternamen in Ihrer Funktion verwenden, wie zum Beispiel input_size

Verwandte Themen