C 使用循环打印结构
如何使用循环打印结构内容?如果是指针,如何将指针分配给结构?我附上了我制作的代码,它使用用户输入的数据构建了一个结构,并以有序的方式打印出来。我的问题是,您必须编写大量的C 使用循环打印结构,c,C,如何使用循环打印结构内容?如果是指针,如何将指针分配给结构?我附上了我制作的代码,它使用用户输入的数据构建了一个结构,并以有序的方式打印出来。我的问题是,您必须编写大量的printf()和get\s()语句来接收和打印输入。我觉得使用for循环更容易做到这一点。我试图为循环创建一个,,正如您在使用指针的代码中所看到的那样,但是它没有编译,而且我非常确定,将指针分配给结构是错误的 TL;DR:如何使用for循环打印结构内容?如果是指针,如何将指针分配给结构 #define _CRT_SECURE_
printf()
和get\s()
语句来接收和打印输入。我觉得使用for循环
更容易做到这一点。我试图为循环创建一个,
,正如您在使用指针的代码中所看到的那样,但是它没有编译,而且我非常确定,将指针分配给结构是错误的
TL;DR:如何使用for循环打印结构内容?如果是指针,如何将指针分配给结构
#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;
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
#定义maxName 50
#定义最大值50
#定义最大年龄100
结构宇航员
{
char firstName[maxName];
char lastName[maxName];
炭化[最大化];
int年龄=0;
字符任务名[maxAge];
国际导弹协会[50];
};
int main()
{
int i=0;
结构宇航员候选人1;
结构宇航员候选人2;
结构宇航员候选人3;
结构宇航员任务1;
结构宇航员任务2;
结构宇航员任务3;
printf(“请输入候选人的名字:”);
获取(候选1.firstName);
printf(“请输入候选人的姓氏:”);
获取(候选1.lastName);
printf(“请输入候选人的国籍:”);
获得(候选人1.国家);
printf(“请输入候选人的年龄:”);
scanf(“%d”、&candidate1.年龄);
printf(“请输入候选人的名字:”);
获取(候选2.firstName);
printf(“请输入候选人的姓氏:”);
获取(候选2.lastName);
printf(“请输入候选人的国籍:”);
获得(候选人2.国家);
printf(“请输入候选人的年龄:”);
scanf(“%d”和候选2.年龄);
printf(“请输入候选人的名字:”);
获取(候选3.firstName);
printf(“请输入候选人的姓氏:”);
获取(候选3.lastName);
printf(“请输入候选人的国籍:”);
获得(候选人3.国家);
printf(“请输入候选人的年龄:”);
scanf(“%d”和候选3.年龄);
printf(“输入任务名称:”);
获取_s(mission1.missionName);
printf(“输入执行任务的年份:”);
scanf(“%d”和任务1.missionYear);
printf(“输入任务名称:”);
获取_s(mission2.missionName);
printf(“输入执行任务的年份:”);
scanf(“%d”和任务2.任务EAR);
printf(“输入任务名称:”);
获取_s(mission3.missionName);
printf(“输入执行任务的年份:”);
scanf(“%d”、&mission3.missionYear);
结构宇航员*ptr;
结构候选*ptr;
//printf(“\n\tAstronaut候选数据库\n”);
//printf(“姓名:%s%s\t年龄:%d\t国籍:%s\n”,candidate1.firstName,candidate1.lastName,candidate1.Age,candidate1.nation);
//printf(“姓名:%s%s\t年龄:%d\t国籍:%s\n”,candidate2.firstName,candidate2.lastName,candidate2.Age,candidate2.nation);
//printf(“姓名:%s%s\t年龄:%d\t国籍:%s\n”,candidate3.firstName,candidate3.lastName,candidate3.Age,candidate3.nation);
//printf(“\n\tAstronaut任务数据库\n”);
//printf(“任务名称:%s\t年份:%d\n”,任务1.missionName,任务1.missionYear);
//printf(“任务名称:%s\t年份:%d\n”,任务2.missionName,任务2.missionYear);
//printf(“任务名称:%s\t年份:%d\n”,任务3.missionName,任务3.missionYear);
对于(int-index=0;index<5;index++)
{
printf(“姓名:%s%s\t年龄:%d\t国籍:%s\n”,*ptr.firstName、*ptr.lastName、*ptr.Age、*ptr.nation);
ptr++;
}
_getch();
返回0;
}
我想您需要两个结构。宇航员和任务不是一回事。现在,您有几个未使用的字段,这取决于您是为宇航员
还是为任务
输入数据。你可以这样做:
#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;
}
这将允许您将MAX_宇航员任务与每位宇航员相关联。或者,更现实地说,一次任务可能与几个宇航员有关。在这种情况下,您可能希望数据结构更像这样
#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];
};
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];
};
不能使用循环打印单个变量(技术上可以,但这取决于系统/UB)。您需要使用数组或类似链表的东西。我将编写一个函数,该函数接受指向
struct-audion
的指针,该指针接受所有用户输入。然后,您可以让一行程序对每个struct-audion
调用该函数。。。每个候选人
和任务
可能有不同的功能。。但在我看来,你需要两个独立的结构。。一个用于宇航员,一个用于任务。1)不要使用conio.h
。2) 编译的Post代码。如果没有,请发布错误消息。请参阅: