Hi everyone, I am having trouble working with structs. The objective of the assignment is:
In Chapter 9, you were asked to write a program that keeps track of important birthdays.
Modify this program to store one person’s birthday information in a struct data type.
The struct should consist of two other structs: one struct to hold the person’s first
name and last name, and another to hold the date (day, month, and year).
Here is my code so far (messy) and I used the structure tutorial on this site:
/*In Chapter 9, you were asked to write a program that keeps track of important birthdays.
Modify this program to store one person’s birthday information in a struct data type.
The struct should consist of two other structs: one struct to hold the person’s first
name and last name, and another to hold the date (day, month, and year). Consider including
other information as well, such as a vector of strings with a list of possible gift ideas.*/
#include<iostream>
#include <string>
usingnamespace std;
struct birthday
{
string name;
int day;
int month;
int year;
};
int main()
{
int i=0;
birthday person;
int times;
cout<<"How many birthdays do you want to input?"<<endl;
cin>>times;
for(i=0;i<times;i++)
{
cout<<"Enter person's first and last name: "<<endl;
cin>>person[i].name;
cout<<"Enter day of birth(dd): "<<endl;
cin>>person[i].day;
cout<<"Enter month of birth(mm): "<<endl;
cin>>person[i].month;
cout<<"Enter year of birth(yyyy): "<<endl;
cin>>person[i].year;
}
}
You really don't state your problem. All your saying is your having problem with structs - then you post your assignment. No ones going to do your code for you but I'll refer you to this thread:
I got it to work; I didn't realize structs within a struct didn't translate to calling out multiple structs within the struct (mind-boggling). Here's what I did for anyone interested:
Hm, yeah you're right. Do you think you can give me an example how I can do that? I've tried looking everywhere but I can't seem to find use of structs within a struct. My program is also not turning up parts of information.. think I effed up something somewhere. Currently doing a rehaul of everything.
struct Birthday
{
struct ID
{
string firstName, lastName;
} name; // we define how a struct ID looks like and also declare a variable of its type
struct DMY
{
int day, month, year;
} date; // same as above
/************************************************************
* Instead of declaring the variables right after the definition of the struct you
* can also do that later. Maybe to make the code look neater if you have
* more than one variable of that type
************************************************************/
// ID name;
// DMY date;
}
Note that you can also define the substruct outside the main struct. In this case only the second method of variable declaration can be used
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
struct ID
{
string firstName, lastName;
};
struct DMY
{
int day, month, year;
};
struct Birthday
{
ID name;
DMY date;
};