Need Nested Struct Help

Can someone tell me what i did wrong with this code:

#include

using namespace std;

//Represent a person
//1. Name
//2. Age
//3. Gender

struct Person{

string name;
int age;
string gender;

};

struct Marriage{
Person personA;
Person personB;
int ageOfMarrige;
int numberOfPeopleAtWedding;

};

void printPersonInfo(Person p) {
cout << "The name of the person is "<<p.name<<endl;
cout << "The age of the person is "<<p.age<<endl;
cout << "The gender of the person is "<<p.gender<<endl;
}

void printMarriageInfo(Marriage m){
cout << m.personA.name << " married "<< m.personB.name <<endl;
cout << " They have been married for "<< m.ageOfMarrige << " Number of years "<<endl;
cout << m.numberOfPeopleAtWedding << " People visited thier wedding "<<endl;
}

int main()
{

Person david = {"David", 21, "Male"};
Person clint = {"Clint", 31, "Male"};
Person sara = {"Sara", 29, "Female"};

Marriage marriage = {ivan, sara, 20, 200};


printMarriageInfo(marriage);


return 0;

}

I followed the video on Nested Struct, but my code does not work.

Thanks!

You defined person david and use ivan in the struct.

Ok i got it!

Thank you!

I have a question on struct, and I wonder if anyone has encountered this and has any ideas.
I have created the various attributes of a person, but for height, I want to include 2 numbers, like the below, I thought I would try and use the double g, but it didn’t really work, do I need to do height at a string and not an int?

Person Andre;
Andre.name = “Andre”;
Andre.gender = “male”;
Andre.height = cout double g = 5’11;
Andre.age = 30;

I mean, I did it as a string, but it’s an int right and should be classified as such?

Double is a decimal number. If you want to use decimals you should use proper notation. Instead of 5’11 use 5.11. :slight_smile:

Ok, I changed it back to int, but it only prints 5 to the console

image

I think I figured it out by doing the next reading assignment, I just did the below,
image

1 Like

Yes using double was correct. It was the decimal notation that was wrong. Int is for integers. :slight_smile: