Structures Help

Muggy (7)
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*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>
using namespace 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;
	}
		
}
Last edited on
Code Assassin (295)
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:

http://www.dreamincode.net/forums/topic/146508-struct-within-a-struct/
Muggy (7)
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:

*deleted, bad code*
Last edited on
maeriden (244)
Isn't you assignemnt telling you to use two structs inside a main struct to hold the info? Or you don't care for that?
Last edited on
Muggy (7)
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.
maeriden (244)
It's as simple as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
};
Last edited on
Code Assassin (295)
Just like maeriden posted. Did you look at my link? It gave you an example.
Topic archived. No new replies allowed.