【发布时间】:2015-11-04 19:06:28
【问题描述】:
我真的是 C 的新手,正在编写一个代码来创建一个包含员工信息数组的结构。正如您在我的代码中看到的那样,有员工(实际上有 4 个,但我在这里将其减少为 1 个),每个员工都有相应的信息。 (仅供参考,为了节省空间,我遗漏了一些东西,比如声明 struct workerT)。
我创建了另一个函数 (prntWorker),它应该打印所有员工及其信息,但我不知道在 main() 中调用它时要使用什么周界。无论我使用什么边界,CodeBlocks 都会返回参数太少。很抱歉,我知道这是一个新手问题,但我将不胜感激!
目标是“给定一个包含 siz 元素的 workerT 结构数组,打印在数组元素 list[indx] 中找到的所有员工信息
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ROSTER 10 //roster of employees
typedef struct workerT_struct {
char name[81]; //employee name
char title[81]; //employee title
int empID; //unique ID for employee
int empStatus;
int year100_salary; //before-tax salary, in cents
int year100_401k; //annual contribution to retirement, in cents
double taxRate; //fraction used to determine payroll taxes
int month100_paycheck; //monthly paycheck, in cents
} workerT;
void initWorkerArray(workerT list[], int siz);
void prntWorker(workerT list[], int siz, int indx);
int main()
{
workerT roster[MAX_ROSTER];
prntWorker(roster, MAX_ROSTER, 0); //FIXME
return 0;
}
void initWorkerArray(workerT list[], int siz) {
int i = 0;
for (i = 0; i < 4; ++i) {
strcpy(list[0].name, "Buggy, Orson");
strcpy(list[0].title, "Director/President");
list[0].empID = 1;
list[0].empStatus = 1;
list[0].year100_salary = 12015000;
list[0].year100_401k = 950000;
list[0].taxRate = 0.45;
strcpy(list[1].name, "Czechs, Imelda");
strcpy(list[1].title, "Chief Financial Officer");
list[1].empID = 2;
list[1].empStatus = 1;
list[1].year100_salary = 8020000;
list[1].year100_401k = 960000;
list[1].taxRate = 0.39;
strcpy(list[2].name, "Hold, Levon");
strcpy(list[2].title, "Customer Service");
list[2].empID = 6;
list[2].empStatus = -1;
list[2].year100_salary = 8575000;
list[2].year100_401k = 1133000;
list[2].taxRate = 0.39;
strcpy(list[3].name, "Andropov, Picov");
strcpy(list[3].title, "Shipping Coordinator");
list[3].empID = 7;
list[3].empStatus = 1;
list[3].year100_salary = 4450000;
list[3].year100_401k = 375000;
list[3].taxRate = 0.31;
}
for (i = 4; i < siz; ++i) {
strcpy(list[i].name, "none");
strcpy(list[i].title, "none");
list[i].empID = -1;
list[i].empStatus = -1;
list[i].year100_salary = 0;
list[i].year100_401k = 0;
list[i].taxRate = 0.0;
}
return;
}
void prntWorker(workerT list[], int siz, int indx) {
int i = 0;
for (i = 0; i < siz; ++i) {
printf("%s ", list[indx].name);
printf("%s ", list[indx].title);
printf("%d ", list[indx].empID);
printf("%d ", list[indx].empStatus);
printf("%d ", list[indx].year100_salary);
printf("%d ", list[indx].year100_401k);
printf("%lf ", list[indx].taxRate);
printf("%d ", list[indx].month100_paycheck);
}
return;
}
【问题讨论】:
-
你试过
#include <stdio.h>吗? -
是的,我做了,这只是我为了节省空间而剪掉的东西之一
-
prntWorker( roster, MAX_ROSTER, 0 )应该可以工作,但是您的功能很混乱。它在indx打印该项目,并重复siz次。 -
请发布我们可以编译的代码...例如没有
workerT定义,底部有一个杂散的return或缺少}。 -
你从不调用 initWorkerArray,所以没有什么可打印的。
标签: c arrays function printing struct