<分区>
我正在尝试制作一个程序来存储有关员工的信息,但是当我尝试将薪水存储在那里时,输出是一个随机整数。
在我将原始的 Salary
(不是数组)转换为 Salary[i]
之前,代码运行良好,我这样做了能够计算总薪水 (totSlry
)
int NumofEmpo;
printf("How much Employees do you want to add?\n");
scanf(" %d", &NumofEmpo);
char Name[25], Service[20]; //All the variables that the user
int DOB, Salary[NumofEmpo], totSlry = 0; //info will get stored in
for(int i = 0; i != NumofEmpo; i++)
{
fgetc(stdin);
printf("Name: ");
fgets(Name, 25, stdin);
printf("Service: ");
fgets(Service, 20, stdin);
printf("Date of Birth: ");
scanf("%d", &DOB);
printf("Salary: ");
scanf("%d", &Salary[i]);
printf("\n");
totSlry = totSlry + Salary[i];
FILE * fpointer = fopen("Employees.txt", "a");
fprintf(fpointer, "Name: %s", Name);
fprintf(fpointer, "Service: %s", Service);
fprintf(fpointer, "Date of Birth: %d\n", DOB);
fprintf(fpointer, "Salary: %d\n\n", Salary);
fclose(fpointer);
}
FILE * fpointer0 = fopen("Employees.txt", "a");
fprintf(fpointer0, "Total Salary: %d", totSlry);
fclose(fpointer0);
我希望输出显示文件中所有员工的薪水,如下所示:
Name: Ahmed
Service: Security
Date of Birth: 1995
Salary: 100
Name: John
Service: Accountant
Date of Birth: 2000
Salary: 200
Total Salary: 300
但实际输出结果是这样的:
Name: Ahmed
Service: Security
Date of Birth: 1995
Salary: -24087712
Name: John
Service: Accountant
Date of Birth: 2000
Salary: -24087712
Total Salary: 300