我们的教授告诉我们用C语言做一个薪资系统,并要求10名员工。我真的不知道接下来要做什么。所以该计划应该要求10名员工,在我投入10名员工后,我现在可以返回3个选项来询问所有记录。问题是我不知道如何在案例1中显示所有记录。它是C中的薪资系统
#include <stdio.h>
#include <stdlib.h>
struct employee
{
int empId, hrW, hrR;
char empName[30], taxCode;
};
void display(struct employee e1)
{
printf("%d \t %s \t %d \t %d \t %s \n", e1.empId, e1.empName, e1.hrW, e1.hrR, e1.taxCode);
return;
}
int main(void)
{
int choice, i, ch, option, ok = 0;
struct employee *e1;
while (ok == 0) {
// Choices
printf("1.) Show All Records\n");
printf("2.) Add 10 Records\n");
printf("3.) Exit\n\n");
printf("========================\n");
printf("Enter Choice: ");
scanf("%d", &choice);
printf("========================\n");
switch(choice)
{
case 1: printf("\nEmployee ID\t Employee Name\t Hours Worked\t Hourly Rate\n");
break;
case 2: printf(" Witholding Tax :\n");
printf("Code:\tDefinition:\tDeduction:");
printf("\n\nS\tSingle\t\t500");
printf("\n\nH\tHead of the\t450");
printf("\n\tFamily");
printf("\n\nM2\tMarried with\t400");
printf("\n\t2 dependents");
printf("\n\nM3\tMarried with\t300");
printf("\n\t3 dependents");
for (i = 1; i <= 10; i++)
{
printf("\n\nEmployee ID: ");
scanf("%d", &(e1[i].empId));
printf("Employee Name: ");
scanf(" %[^\n]s", &(e1[i].empName));
printf("Hours Worked: ");
scanf("\n%d", &(e1[i].hrW));
printf("Hourly Rate: ");
scanf("\n%d", &(e1[i].hrR));
printf("Enter Tax Code: ");
scanf("%s", &(e1[i].taxCode));
}
break;
case 3:
break;
default: printf("Invalid Choice");
}
printf("\nDo you want to continue? (Y/N): "); // Option choices if you wish to continue
scanf("%s", &option);
if (option == 'y'||option == 'Y')
{
printf("\n");
}
else if (option == 'n'||option == 'N')
{
ok++;
}
}
return 0;
}
答案 0 :(得分:0)
如评论中所述。要打印案例1中的所有记录,您只需使用prinf。
例如:
case 1: printf("\nEmployee ID\t Employee Name\t Hours Worked\t Hourly Rate\n");
for (i = 1; i <= 10; i++){
printf("\n%d", e1[i].empId);
printf("\t\t%s", e1[i].empName);
printf("\t\t%d", e1[i].hrW);
printf("\t\t$d", e1[i].hrR);
}
break;