1d Arrays in structs

Hi i wanted to know how do i put a struct in a 1d array and out put it.
It has to be like


1
2
3
4
5
 struct Person
{
   string name[5];
   string surname[5];
};


The output has to be
John Meyer
Joe Bob
James Steve
Kevin Phillips

Could someone please help
Last edited on
You mean put an array in a struct.

Something like this,
1
2
3
Person p;
p.name[0]="John";
p.surname[0]="Meyer";


Aceix.
@yaaz32

With a struct, you create one instance of what makes up the data. So, in your case,
1
2
3
4
5
 struct Person
{
   string name;
   string surname;
};

you need only a name and surname. Then, you create your group of how many.

Person Group[5];

You wanted 5 people.

Then, fill it.
1
2
3
4
Group[0].name = "John";
Group[0].surname = "Meyer";
Group[1].name = "Joe";
Group[1].surname = "Bob";


If you're filling in the names by input, you need a push_back command.
Do i have to assign it by saying Group[0].name etc.
Can't i just hardcode all the names in at once and just call it with a for loop?

Thanks for the help. Really appreciate it
Can't i just hardcode all the names in at once and just call it with a for loop?


Yes, you could.
Topic archived. No new replies allowed.