使用循环打印结构

时间:2017-12-04 04:19:04

标签: c

如何使用for-loop打印结构内容?如果指针,如何指定结构指针?我附加了我创建的代码,它使用用户输入的数据构建一个结构,并以有序的方式打印出来。我的问题是你必须写很多printf()gets_s()语句来接收和打印输入。我觉得使用for-loop更容易做到这一点。我尝试使用指针在代码中看到for-loop,但它没有编译,我很确定这是指向结构指针的错误方法。 / p>

TL; DR:如何使用for-loop打印结构内容?如果指针,如何指定结构指针?

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>

#define maxName 50
#define maxNation 50
#define maxAge 100

struct astronaut
{
    char firstName[maxName];
    char lastName[maxName];
    char nation[maxNation];
    int age = 0;
    char missionName[maxAge];
    int missionYear[50];
};

int main()
{
    int i = 0;
    struct astronaut candidate1;
    struct astronaut candidate2;
    struct astronaut candidate3;
    struct astronaut mission1;
    struct astronaut mission2;
    struct astronaut mission3;

    printf("Please enter the first name of the candidate: ");
    gets_s(candidate1.firstName);
    printf("Please enter the last name of the candidate: ");
    gets_s(candidate1.lastName);
    printf("Please enter the nationality of the candidate: ");
    gets_s(candidate1.nation);
    printf("Please enter the age of the candidate: ");
    scanf("%d", &candidate1.age);

    printf("Please enter the first name of the candidate: ");
    gets_s(candidate2.firstName);
    printf("Please enter the last name of the candidate: ");
    gets_s(candidate2.lastName);
    printf("Please enter the nationality of the candidate: ");
    gets_s(candidate2.nation);
    printf("Please enter the age of the candidate: ");
    scanf("%d", &candidate2.age);

    printf("Please enter the first name of the candidate: ");
    gets_s(candidate3.firstName);
    printf("Please enter the last name of the candidate: ");
    gets_s(candidate3.lastName);
    printf("Please enter the nationality of the candidate: ");
    gets_s(candidate3.nation);
    printf("Please enter the age of the candidate: ");
    scanf("%d", &candidate3.age);

    printf("Enter a Mission Name: ");
    gets_s(mission1.missionName);
    printf("Enter the year the mission was conducted: ");
    scanf("%d", &mission1.missionYear);

    printf("Enter a Mission Name: ");
    gets_s(mission2.missionName);
    printf("Enter the year the mission was conducted: ");
    scanf("%d", &mission2.missionYear);

    printf("Enter a Mission Name: ");
    gets_s(mission3.missionName);
    printf("Enter the year the mission was conducted: ");
    scanf("%d", &mission3.missionYear);

    struct astronaut *ptr;
    struct candidate *ptr;

//  printf("\n\tAstronaut Candidate Database\n");
//  printf("Name: %s %s \t Age: %d \t Nationality: %s\n",     candidate1.firstName, candidate1.lastName, candidate1.age, candidate1.nation);
//  printf("Name: %s %s \t Age: %d \t Nationality: %s\n",     candidate2.firstName, candidate2.lastName, candidate2.age, candidate2.nation);
//  printf("Name: %s %s \t Age: %d \t Nationality: %s\n",     candidate3.firstName, candidate3.lastName, candidate3.age, candidate3.nation);
//  printf("\n\tAstronaut Mission Database\n");
//  printf("Mission Name: %s \t Year: %d\n", mission1.missionName,     mission1.missionYear);
//  printf("Mission Name: %s \t Year: %d\n", mission2.missionName,     mission2.missionYear);
//  printf("Mission Name: %s \t Year: %d\n", mission3.missionName,     mission3.missionYear);

    for (int index = 0; index < 5; index++)
    {
        printf("Name: %s %s \t Age: %d \t Nationality: %s\n",     *ptr.firstName, *ptr.lastName, *ptr.age, *ptr.nation);
        ptr++;
    }

    _getch();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我认为你需要2个结构。 astronautmission不是同一件事。就像现在一样,您有几个未使用的字段,具体取决于您是否为astronautmission输入数据。你可以这样做:

#include <stdio.h>

#define maxName 50
#define maxNation 50
#define maxAge 100

struct astronaut
{
    char firstName[maxName];
    char lastName[maxName];
    char nation[maxNation];
    int age;
};

struct mission
{
    char missionName[maxAge];
    int missionYear;  // why do you have an array of 50 ints for the missionYear?
};

void enter_candidate_data(struct candidate* cand)
{
    // not famliar with gets_s .. I would use fgets here, you can read the manpage on that if you desire
    printf("Please enter the first name of the candidate: ");
    gets_s(cand->firstName);
    printf("Please enter the last name of the candidate: ");
    gets_s(cand->lastName);
    printf("Please enter the nationality of the candidate: ");
    gets_s(cand->nation);
    printf("Please enter the age of the candidate: ");
    scanf("%d", &(cand->age));
}

void enter_mission_data(struct mission* mis)
{
    printf("Enter a Mission Name: ");
    gets_s(mis->missionName);
    printf("Enter the year the mission was conducted: ");
    scanf("%d", &(mis->missionYear));
}

int main()
{
    int i;
    struct astronaut candidates[3];
    struct mission missions[3];

    for (i=0; i<3; i++)
    {
        enter_candidate_data(&(candidates[i]));
        // you can put this in a separate loop if you want to enter all
        // candidate data first
        enter_mission_data(&(missions[i]));
    }

    // you could also write functions to print the data instead, depends on
    // how you want it all presented

    for (int i=0; i<3; i++)
    {
        printf("Name: %s %s \t Age: %d \t Nationality: %s\n",
            candidates[i].firstName, candidates[i].lastName, candidates[i].nation);
        printf("Mission Name: %s, year %d\n", missions[i].missionName,
            missions[i].missionYear);
    }

    return 0;
}

您可能需要与特定宇航员相关的许多任务(甚至只需一个)。如果是这样,我会定义像这样的数据结构

#define MAX_ASTRONAUT_MISSIONS 20

struct mission
{
    char missionName[maxAge];
    int missionYear;
};

struct astronaut
{
    char firstName[maxName];
    char lastName[maxName];
    char nation[maxNation];
    int age;
    struct mission missions[MAX_ASTRONAUT_MISSIONS];
};

这将允许您将MAX_ASTRONAUT_MISSION任务与每位宇航员联系起来。或者,更现实地说,一个任务可能与几名宇航员有关。在这种情况下,您可能希望数据结构更像这样

struct mission
{
    char missionName[maxAge];
    int missionYear;
};

struct astronaut
{
    char firstName[maxName];
    char lastName[maxName];
    char nation[maxNation];
    int age;
    // using a pointer to missions will allow you to create one mission, and
    // all the astronauts on that mission could get a pointer to it,
    // designating they were all on that singular mission.
    struct mission* missions[MAX_ASTRONAUT_MISSIONS];
};