簡體   English   中英

C在main中調用打印功能

[英]C calling a print function in main

我真的是C語言的新手,正在編寫代碼來創建具有一系列員工信息的結構。 正如您在我的代碼中看到的那樣,這里有員工(實際上有4個員工,但我在這里將其縮減為1個),每個員工都有相應的信息。 (僅供參考,為了節省空間,我遺漏了一些東西,例如聲明struct workerT)。

我制作了另一個函數(prntWorker),該函數應該打印所有員工及其信息,但是我不知道在main()中調用該控件時要使用什么邊界。 無論我使用什么周長,CodeBlocks都會返回參數太少的信息。 對不起,我知道這是一個新手問題,但是我將不勝感激!

目標是“給定保存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;
}

這是您調整后的代碼。 initWorkerArray()我從main()調用了initWorkerArray() ,但刪除了將結構初始化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;
}

程序輸出:

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

這里有幾個問題:

您永遠不會調用initWorkerArray來初始化數組:

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

initWorkerArray內部, initWorkerArray第一個for循環,因為您正在更新數組中的特定索引。

您永遠不會在第二個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;
}

然后在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;
}

更改此功能后,還可以更改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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM