Reputation: 17
My current program looks like this. This program should ask 5 records of a person and display them.
#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();
}
I have a problem in displaying the 5 different record. How do I display the five records in one printf?
Upvotes: 0
Views: 95
Reputation: 49463
You simply need a collection of structures to save the data into, currently you're just overwriting the data each time. There are a lot of ways to do this, but you could do something like use a static array, for example:
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
Same for the printf()
further down:
for(i=0; i<5; i++)
{
printf("Name is: %s\n", records[i].name);
Upvotes: 1