2017-05-28 96 views
1

因此,与一流的工作进展,学习CC - 参数名称省略?

我决定progressivly把函数按我的简短从我的课堂作业,如下所示的背景下,试图通过一块排查一段代码:

Structure Chart

Functions Brief

伪码香港专业教育学院被告知要遵循:

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 

但是编译程序,我把整个误差对尺寸为#define size 3后声明的每个函数“省略的参数名称”来插入,和“)”的名字预期

这是我到目前为止写的代码:

#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; 
} 

有人可以请更正我的代码到目前为止?

回答

2

参数名称略

手段,编译器不明白一个参数名称,其中,预计...

这是因为你不能使用define和参数名称与同名。当写#define size 3预处理器替换它看到的代码3每个size然后,当你调用与参数size的功能,而不是struct employee read_all_employee(..., int size),你struct employee read_all_employee(..., int 3),导致int类型的参数没有有效的名称( '3'不是有效的名称)

我会建议使用define与CAPS,或者用一些独特的名字,这样你就不会感到困惑,像SIZE或者只是记住,你有size符号并使用你的函数参数的其他名称,如input_size