C在main中调用print函数

时间:2022-11-04 19:59:25

I am really new to C and am writing a code that creates a struct with an array of employee information. As you can see in my code, there's employees (there's actually 4 but I cut it down to 1 here for space), and each employee has corresponding info. (Just FYI I left out some stuff in order to save space, like declaring the struct workerT).

我是C的新手,我正在编写一个代码,用于创建一个包含员工信息数组的结构。正如您在我的代码中看到的那样,有员工(实际上有4个员工,但我将其减少到1个空间),每个员工都有相应的信息。 (仅仅是因为我省略了一些东西以节省空间,比如声明struct workerT)。

I made another function (prntWorker) that should print all the employees and their information, but I don't know how what perimeters to use when I call it in main(). No matter what perimeters I use, CodeBlocks returns that there's too few arguments. I'm sorry I know this is a novice question, but I would greatly appreciate any help!

我创建了另一个应该打印所有员工及其信息的功能(prntWorker),但是当我在main()中调用它时,我不知道如何使用perimeter。无论我使用什么周长,CodeBlocks都会返回参数太少。对不起,我知道这是一个新手问题,但我非常感谢任何帮助!

The goal is "given an array of workerT structs that holds siz elements, print all employee information found in the array element list[indx]

目标是“给定一个包含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;
}

2 个解决方案

#1


1  

Several issue here:

这里有几个问题:

You never call initWorkerArray to initialize the array:

您永远不会调用initWorkerArray来初始化数组:

int main()
{
    workerT roster[MAX_ROSTER];
    initWorkerArray(roster, MAX_ROSTER);   // add this call
    prntWorker(roster, MAX_ROSTER, 0);
    return 0;
}

Inside of initWorkerArray, the first for loop is not needed, as you're updating specific indexes in your array.

在initWorkerArray内部,不需要第一个for循环,因为您正在更新数组中的特定索引。

You never initialize month100_paycheck, either in the second for loop or for the initial fields, so this field contains garbage:

您永远不会在第二个for循环或初始字段中初始化month100_paycheck,因此该字段包含垃圾:

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;
}

Then in prntWorker, you're not using the loop index i to iterate through the list. You're using the parameter indx instead, which you don't really need:

然后在prntWorker中,您没有使用循环索引i迭代列表。您正在使用参数indx,而您实际上并不需要:

void prntWorker(workerT list[], int siz) {
    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");    // put a newline at the end for nicer formatting
    }
    return;
}

After changing this function, also change the call in main:

更改此功能后,还可以更改main中的呼叫:

    prntWorker(roster, MAX_ROSTER);

After making these changes, you should get the following output:

进行这些更改后,您应该获得以下输出:

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

#2


1  

Here is your tweaked code. Primarily I called initWorkerArray() from main(), but removed its loop that initialises the struct 4 times over. I also changed the loop control variable and added a newline in prntWorker() after printing each record. However the last field month100_paycheck is printing absurd values, because it has not been calculated.

这是你的调整代码。主要是我从main()调用initWorkerArray(),但删除了它初始化结构4次的循环。我还更改了循环控制变量,并在打印每条记录后在prntWorker()中添加了换行符。但是,最后一个字段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;
}

Program output:

节目输出:

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

#1


1  

Several issue here:

这里有几个问题:

You never call initWorkerArray to initialize the array:

您永远不会调用initWorkerArray来初始化数组:

int main()
{
    workerT roster[MAX_ROSTER];
    initWorkerArray(roster, MAX_ROSTER);   // add this call
    prntWorker(roster, MAX_ROSTER, 0);
    return 0;
}

Inside of initWorkerArray, the first for loop is not needed, as you're updating specific indexes in your array.

在initWorkerArray内部,不需要第一个for循环,因为您正在更新数组中的特定索引。

You never initialize month100_paycheck, either in the second for loop or for the initial fields, so this field contains garbage:

您永远不会在第二个for循环或初始字段中初始化month100_paycheck,因此该字段包含垃圾:

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;
}

Then in prntWorker, you're not using the loop index i to iterate through the list. You're using the parameter indx instead, which you don't really need:

然后在prntWorker中,您没有使用循环索引i迭代列表。您正在使用参数indx,而您实际上并不需要:

void prntWorker(workerT list[], int siz) {
    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");    // put a newline at the end for nicer formatting
    }
    return;
}

After changing this function, also change the call in main:

更改此功能后,还可以更改main中的呼叫:

    prntWorker(roster, MAX_ROSTER);

After making these changes, you should get the following output:

进行这些更改后,您应该获得以下输出:

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

#2


1  

Here is your tweaked code. Primarily I called initWorkerArray() from main(), but removed its loop that initialises the struct 4 times over. I also changed the loop control variable and added a newline in prntWorker() after printing each record. However the last field month100_paycheck is printing absurd values, because it has not been calculated.

这是你的调整代码。主要是我从main()调用initWorkerArray(),但删除了它初始化结构4次的循环。我还更改了循环控制变量,并在打印每条记录后在prntWorker()中添加了换行符。但是,最后一个字段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;
}

Program output:

节目输出:

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