C在main中调用print函数

C在main中调用print函数,c,arrays,function,printing,struct,C,Arrays,Function,Printing,Struct,我是C语言的新手,正在编写一段代码,用一组员工信息创建一个结构。正如您在我的代码中看到的,有员工(实际上有4名员工,但为了节省空间,我将其减少到1名),每个员工都有相应的信息。(仅供参考,为了节省空间,我省略了一些内容,比如声明struct workerT) 我做了另一个函数(prntWorker),它应该打印所有员工及其信息,但我不知道在main()中调用它时如何使用周长。无论我使用什么周长,代码块都会返回参数太少的信息。很抱歉,我知道这是一个新手问题,但我将非常感谢任何帮助 目标是“给定一个

我是C语言的新手,正在编写一段代码,用一组员工信息创建一个结构。正如您在我的代码中看到的,有员工(实际上有4名员工,但为了节省空间,我将其减少到1名),每个员工都有相应的信息。(仅供参考,为了节省空间,我省略了一些内容,比如声明struct workerT)

我做了另一个函数(prntWorker),它应该打印所有员工及其信息,但我不知道在main()中调用它时如何使用周长。无论我使用什么周长,代码块都会返回参数太少的信息。很抱歉,我知道这是一个新手问题,但我将非常感谢任何帮助

目标是“给定一个包含siz元素的workerT结构数组,打印数组元素列表[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;
}
#包括
#包括
#包括
#定义最大花名册10//员工花名册
typedef结构workerT_结构{
char name[81];//员工姓名
char title[81];//员工职务
int empID;//员工的唯一ID
国际地位;
int Year 100_salary;//税前工资,单位为美分
int year100_401k;//每年退休供款,单位:美分
双重税率;//用于确定工资税的分数
int month100_工资支票;//每月工资支票,单位为美分
}沃克特;
void initworkerary(workerT列表[],整数大小);
无效prntWorker(工作任务列表[],内部大小,内部索引);
int main()
{
工人名册[最高名册];
prntWorker(花名册,最大花名册,0);//修复我
返回0;
}
void initWorkerArray(workerT列表[],整数大小){
int i=0;
对于(i=0;i<4;++i){
strcpy(列表[0]。名称,“Buggy,Orson”);
strcpy(列表[0]。标题为“董事/总裁”);
列表[0]。empID=1;
列表[0]。empStatus=1;
列表[0]。100年工资=12015000;
列表[0]。年份100_401k=950000;
列表[0]。税率=0.45;
strcpy(列表[1]。名称,“Czechs,Imelda”);
strcpy(清单[1]。头衔,“首席财务官”);
列表[1]。empID=2;
列表[1]。empStatus=1;
清单[1]。100年工资=8020000;
列表[1]。年份100401k=960000;
列表[1]。税率=0.39;
strcpy(列表[2]。名称为“Hold,Levon”);
strcpy(列表[2]。标题为“客户服务”);
列表[2]。empID=6;
列表[2]。empStatus=-1;
清单[2]。100年工资=8575000;
列表[2]。年份100_401k=1133000;
列表[2]。税率=0.39;
strcpy(列表[3]。名称,“安德罗波夫,皮科夫”);
strcpy(清单[3]。标题为“装运协调员”);
列表[3],empID=7;
列表[3]。empStatus=1;
清单[3]。100年工资=445000;
列表[3]。年份100_401k=375000;
列表[3]。税率=0.31;
}
对于(i=4;i
这是经过调整的代码。我主要是从
main()
调用了
initworkerary()
,但删除了初始化结构4次的循环。我还更改了循环控制变量,并在
prntWorker()中添加了一个
newline
打印每条记录后。但是最后一个字段
month100_paycheck
正在打印荒谬的值,因为它尚未计算

#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];
    initWorkerArray(roster, MAX_ROSTER);
    prntWorker(roster, MAX_ROSTER, 0); //FIXME
    return 0;
}

void initWorkerArray(workerT list[], int siz) {
    int i = 0;

    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[i].name);
        printf("%s ", list[i].title);
        printf("%d ", list[i].empID);
        printf("%d ", list[i].empStatus);
        printf("%d ", list[i].year100_salary);
        printf("%d ", list[i].year100_401k);
        printf("%lf ", list[i].taxRate);
        printf("%d ", list[i].month100_paycheck);
        printf("\n");
    }
    return;
}
这里有几个问题:

您从不调用
initworkerary
来初始化数组:

int main()
{
    workerT roster[MAX_ROSTER];
    initWorkerArray(roster, MAX_ROSTER);   // add this call
    prntWorker(roster, MAX_ROSTER, 0);
    return 0;
}
initworkerary
内部,不需要
for
循环的第一个
,因为您正在更新数组中的特定索引

无论是在第二个
循环中还是在初始字段中,您都不会初始化
月100_工资支票,因此此字段包含垃圾:

list[0].month100_paycheck = 0;
...
list[1].month100_paycheck = 0;
...
list[2].month100_paycheck = 0;
...
list[3].month100_paycheck = 0;
...
for (i = 4; i < siz; ++i) {
    ....
    list[i].month100_paycheck = 0;
}
更改此功能后,还要更改
main
中的调用:

    prntWorker(roster, MAX_ROSTER);
进行这些更改后,应获得以下输出:

Buggy, Orson Director/President 1 1 12015000 950000 0.450000 0
Czechs, Imelda Chief Financial Officer 2 1 8020000 960000 0.390000 0
Hold, Levon Customer Service 6 -1 8575000 1133000 0.390000 0
Andropov, Picov Shipping Coordinator 7 1 4450000 375000 0.310000 0
none none -1 -1 0 0 0.000000 0
none none -1 -1 0 0 0.000000 0
none none -1 -1 0 0 0.000000 0
none none -1 -1 0 0 0.000000 0
none none -1 -1 0 0 0.000000 0
none none -1 -1 0 0 0.000000 0

你有没有试着把
#包括进去?
#是的,这只是我为了节省空间而删掉的东西之一
应该可以工作,但是你的函数很混乱。它在
indx
打印项目,并重复
siz
次。请发布我们可以编译的代码…例如,没有
workerT
定义,底部有一个零散的
返回
,或者缺少
}
。你从不调用initworkerary,因此没有什么需要重新编译的int.谢谢,这很有效!下一步是计算月薪,这就是为什么它被忽略的原因。@peo965最好首先初始化该字段,这样就不必根据默认值计算未使用的记录。是的,我知道,我只是按照作业的说明这样做。我的教授做事的方式很奇怪。
    prntWorker(roster, MAX_ROSTER);
Buggy, Orson Director/President 1 1 12015000 950000 0.450000 0
Czechs, Imelda Chief Financial Officer 2 1 8020000 960000 0.390000 0
Hold, Levon Customer Service 6 -1 8575000 1133000 0.390000 0
Andropov, Picov Shipping Coordinator 7 1 4450000 375000 0.310000 0
none none -1 -1 0 0 0.000000 0
none none -1 -1 0 0 0.000000 0
none none -1 -1 0 0 0.000000 0
none none -1 -1 0 0 0.000000 0
none none -1 -1 0 0 0.000000 0
none none -1 -1 0 0 0.000000 0