【问题标题】:C - Parameter Name Omitted?C - 参数名称省略?
【发布时间】:2017-10-28 16:02:28
【问题描述】:

所以在课堂上进步,学习C

我决定按照我在课堂作业中的简介逐步添加函数,如下所示的上下文,以尝试逐段排除代码故障:

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

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

【问题讨论】:

    标签: c windows macos terminal sublimetext3


    【解决方案1】:

    参数名称省略

    意思是,编译器没有看到它期望的参数名称......

    这是因为您不能使用define 和同名的参数名称。在编写#define size 3 时,预处理器将其在代码中看到的每个size 替换为3,然后,当您调用带有参数size 的函数时,而不是struct employee read_all_employee(..., int size),您将得到struct employee read_all_employee(..., int 3),结果在int 类型的参数中没有有效名称​​('3' 不是有效名称)

    我建议使用带有大写字母的define,或者使用一些唯一的名称,这样您就不会感到困惑,例如SIZE,或者请记住您有size 符号并使用其他参数名称你的函数,比如input_size

    【讨论】:

      猜你喜欢
      • 2012-02-05
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多