gpt4 book ai didi

c - 数据结构/记录

转载 作者:行者123 更新时间:2023-11-30 15:31:39 25 4
gpt4 key购买 nike

我当前的程序如下所示。这个程序应该询问一个人的 5 条记录并显示它们。

#include<stdio.h>

struct rec {
char name[100],address[100];
double age,mobileno;
}x;

main()
{
int i;
clrscr();

for(i=1;i<=5;i++)
{
printf("Enter Your Name: ");
scanf("%s",&x.name);

printf("Enter Your Age: ");
scanf("%lf",&x.age);

printf("Enter Your Address: ");
scanf("%s",&x.address);

printf("Enter Your Mobile No.: ");
scanf("%lf",&x.mobileno);

}

printf("\n\nThe Information has been added");
printf("\n\nNAME AGE ADDRESS MOBILE NUMBER");

for(i=1;i<=5;i++)
{
printf("\n%s %.0lf %s %.0lf",x.name,x.age,x.address,x.mobileno);
}
getch();
}

我在显示 5 条不同的记录时遇到问题。如何在一个 printf 中显示 5 条记录?

最佳答案

您只需要一个结构集合来保存数据,目前您每次都只是覆盖数据。有很多方法可以做到这一点,但您可以使用静态数组之类的方法,例如:

struct rec {
char name[100],address[100];
double age,mobileno;
};

int main() {
struct rec records[5];

for(i=0;i<5;i++) // <-- note this is 0 to 4, not 1 to 5
{
printf("Enter Your Name: ");
scanf("%s",records[i].name); // <-- don't add the & for the string input

下面的 printf() 也是如此:

for(i=0; i<5; i++)
{
printf("Name is: %s\n", records[i].name);

关于c - 数据结构/记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24615088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com