在课堂上取得进步,学习C
我已经决定按照我的课堂作业的顺序逐步输入功能,如下所示的上下文,试图逐段排除代码:
Psuedocode我被告知要遵循:
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;
}
到目前为止,有人可以更正我的代码吗?
答案 0 :(得分:2)
参数名称省略
意味着,编译器没有看到它所期望的参数名称......
这是因为您无法使用define
和具有相同名称的参数名称。在编写#define size 3
时,预处理器会将代码中看到的每个size
替换为3
,然后在调用带参数size
的函数时,而不是{{1} }},你得到struct employee read_all_employee(..., int size)
,导致struct employee read_all_employee(..., int 3)
类型的参数没有有效名称(&#39; 3&#39;不是有效名称)。< / p>
我建议您使用带有CAPS的int
或一些独特的名称,这样您就不会感到困惑,例如define
或者请记住您有SIZE
}符号并在函数中使用其他参数名称,例如size