我的任务是使用结构来获取有关员工的信息,并输出他们的薪水。
它似乎可以正常运行,直到我结束并尝试printf()
计算出的结果为止。编译器告诉我[Error] request for member '*' in something not a structure or union
。(将*替换为ID,名称,grossPay,netPay。)
对不起,如果有人问。我对structs / pointer / etc还是很陌生,所以我怀疑这是一个简单的错误。只是没有跳到我身上。我浏览了前面的一些问题,但许多问题是针对特定情况的。
// Matt Posey - PP #20
#include <stdio.h>
struct employee
{
char ID[6];
char name[20];
float hours;
float payRate;
float grossPay;
float netPay;
};
int main()
{
int j, i = 0;
int employees;
printf("This program computes pay.\n");
printf("\nNumber of employees: ");
scanf("%d", &employees);
fseek(stdin,0,SEEK_END);
// Get data
for (i = 0; i < employees; i++)
{
j = i;
struct employee j;
printf("\nFor employee %d:", i+1);
printf("\nID: ");
gets(j.ID);
fseek(stdin,0,SEEK_END);
printf("Name: ");
gets(j.name);
fseek(stdin,0,SEEK_END);
printf("Pay rate: ");
scanf("%f", &j.payRate);
fseek(stdin,0,SEEK_END);
printf("Hours worked: ");
scanf("%f", &j.hours);
fseek(stdin,0,SEEK_END);
j.grossPay = j.hours * j.payRate;
if (j.hours > 40)
{
printf("Overtime!");
float OT = (j.hours - 40) * (j.payRate * 0.5);
j.grossPay += OT;
}
j.netPay = j.grossPay * 0.75;
}
// Output data
printf("\n\nID | Name | Gross Pay | Net Pay");
printf("\n------ | -------------------- | --------- | -------");
for (i = 0; i < employees; i++)
{
j = i;
printf("\n%c | %c | $%7.2f | $%7.2f", j.ID, j.name, j.grossPay, j.netPay);
}
return 0;
}
问题代码有几个问题...
1)
// Get data
for (i = 0; i < employees; i++)
{
j = i;
struct employee j;
该变量j
仅在声明它的作用域(即大括号)内可见。通过将其移至“ main()”范围来解决此问题:
int main()
{
int j, i = 0;
int employees;
struct employee j;
当然,这会引起另一个问题,即已经定义了一个“ int j”。摆脱它:
int main()
{
int i = 0;
int employees;
struct employee j;
2)接下来,您需要j
一个可以容纳所有员工的数组。不幸的是,您不知道(在编译时)用户将需要多少员工。因此,只需创建一个指针即可为以后分配一些内存:
int main()
{
int i = 0;
int employees;
struct employee *j=NULL;
让用户指出有多少雇员,然后为该阵列分配足够的内存:
printf("\nNumber of employees: ");
scanf("%d", &employees);
fseek(stdin,0,SEEK_END);
j = malloc(employees * sizeof(*j));
if(NULL == j)
{
fprintf(stdout, "malloc() failed.\n");
goto CLEANUP;
}
然后,在return语句之前放置一个“ goto标签”:
CLEANUP:
return 0;
}
为了使用该malloc()
功能,您将必须包含另一个标头:
#include <stdio.h>
#include <stdlib.h> /* Added for 'malloc()' */
3)现在摆脱这j=i;
件事:
// Get data
for (i = 0; i < employees; i++)
{
printf("\nFor employee %d:", i+1);
4)现在,到处都j
在引用,就像数组一样引用它:
printf("\nID: ");
gets(j[i].ID);
fseek(stdin,0,SEEK_END);
printf("Name: ");
gets(j[i].name);
fseek(stdin,0,SEEK_END);
printf("Pay rate: ");
scanf("%f", &j[i].payRate);
fseek(stdin,0,SEEK_END);
printf("Hours worked: ");
scanf("%f", &j[i].hours);
fseek(stdin,0,SEEK_END);
j[i].grossPay = j[i].hours * j[i].payRate;
if (j[i].hours > 40)
{
printf("Overtime!");
float OT = (j[i].hours - 40) * (j[i].payRate * 0.5);
j[i].grossPay += OT;
}
j[i].netPay = j[i].grossPay * 0.75;
这里也是:
printf("\n%c | %c | $%7.2f | $%7.2f", j[i].ID, j[i].name, j[i].grossPay, j[i].netPay);
5)摆脱了j = i;
在main()
:
for (i = 0; i < employees; i++)
{
printf("\n%c | %c | $%7.2f | $%7.2f", j[i].ID, j[i].name, j[i].grossPay, j[i].netPay);
}
6)修复printf()格式字符串。应该是打印字符串,而不是字符:
printf("\n%s | %s | $%7.2f | $%7.2f", j[i].ID, j[i].name, j[i].grossPay, j[i].netPay);
现在,代码可以正常工作了。
但是,仍有一些代码存在风险。例如,应该使用fgets()(而不是gets())(如chris&McLovin所指出的),等等。
printf("\nID: ");
fgets(j[i].ID, sizeof(j[i].ID), stdin);
fseek(stdin,0,SEEK_END);
printf("Name: ");
fgets(j[i].name, sizeof(j[i].name), stdin);
如果结尾有换行符,则该报告将更加清晰:
printf("\n");
CLEANUP:
return 0;
}
并且正如Ed Heal所指出的,不需要此行:
fseek(stdin,0,SEEK_END);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句