Array of Structures

Hi all, I'm having a little bit of trouble with my code about structures. I posted the question, and I have a little bit of code. Any suggestion will help! Thanks!
_____________________________________________________________________________
Design a structure Student containing the following members:
1) Name;
2) Date of birth (it should be in turn a structure containing three members Year, Month, Day);
3) Phone number;
4) Major field of study.

Find appropriate data types for each field.

Test the Student structure in a main function. Creating an object of the Student type, assigning some meaningful values to all its fields and then printing them out.
Then in the same main function define an array of structures belonging to the Student structure and containing 3 elements. Assign all values to the elements of this array using initialization at the time of its definition. Display the content of the array created (use a loop).
_____________________________________________________________________________
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
45
46
47
48
49
#include <iostream>
#include <cstdlib>

using namespace std;

struct student
{
	char first_name[100];
	char last_name[100];
	double dob;
	double number;
	char major[100];

	void print_structure()
	{
		cout << "First name: " << endl << first_name;
		cout << "Last name: " << endl << last_name;
		cout << "Date of Birth: " << endl << dob;
		cout << "Phone number: " << endl << number;
		cout << "Major: " << endl << major;
	}
};

const int array_size = 5;

int main()
{
	student stdnt[array_size];
	for (int i = 0; i < 1; i++)
	{
		cout << "Enter first name" << endl;
		cin >> stdnt[i].first_name;
		cout << "Enter last name" << endl;
		cin >> stdnt[i].last_name;
		cout << "Enter date of birth (YYYYMMDD)" << endl;
		cin >> stdnt[i].dob;
		cout << "Enter phone number" << endl;
		cin >> stdnt[i].number;
		cout << "Enter major" << endl;

		for (int i = 0; i < 1; i++)
		{
			stdnt[i].print_structure();
		}
	}
cout << "Student Information" << stdnt[array_size] <<endl;

	return 0;
};
Last edited on
In your for-loops: If you want 3 just write 3 (not 1).

The second loop shouldn't be inside the first loop. So move it outside.
Move the last cout before the second loop (remove << stdnt[array_size], it is wrong and doesn't make sense).
I put cout << "Student Information" << stdnt[array_size] <<endl; so I can try and output the inputted answers in a single block (trying to answer the first part of the question).
Last edited on
You're missing the point. array_size is 5. You're trying to cout stdnt[5] which is an out of bounds reference. Valid instances of stdnt are [0] to [4].

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.pickinpatchfarm.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
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
45
46
47
48
49
50
51
#include <iostream>
#include <cstdlib>

using namespace std;

struct student
{
	char first_name[100];
	char last_name[100];
	double dob;
	double number;
	char major[100];

	void print_structure()
	{
		cout << "First name: " << endl << first_name;
		cout << "Last name: " << endl << last_name;
		cout << "Date of Birth: " << endl << dob;
		cout << "Phone number: " << endl << number;
		cout << "Major: " << endl << major;
	}
};

const int array_size = 5;

int main()
{
	student stdnt[array_size];
	for (int i = 0; i < 3; i++)
	{
		cout << "Enter first name" << endl;
		cin >> stdnt[i].first_name;
		cout << "Enter last name" << endl;
		cin >> stdnt[i].last_name;
		cout << "Enter date of birth (YYYYMMDD)" << endl;
		cin >> stdnt[i].dob;
		cout << "Enter phone number" << endl;
		cin >> stdnt[i].number;
		cout << "Enter major" << endl;
		cin >> stdnt[i].major;
	}

	cout << "Student Information" << stdnt[4].print_structure <<endl;

	for (int i = 0; i < 3; i++)
	{
		stdnt[i].print_structure();
	}

	return 0;
}


I'm still getting a build error, and I'm not 100% sure from what
Line 43: A few problems with this line.
1) print_structure is a function, therefore it requires () to call it.

2) However, print_structure is a void function, which means it returns nothing. You can't cout a void (nothing).

3) stdnt[4] makes no sense. Your for loop at line 29 entered only 3 students. Therefore stdnt[4] is uninitialized (garbage).

I would change line 43 to simply:
 
    cout << "Student Information" << endl;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct student
{
	char first_name[100];
	char last_name[100];
	double dob;
	double number;
	char major[100];

	void print_structure()
	{
		cout << "First name: " << endl << first_name;
		cout << "Last name: " << endl << last_name;
		cout << "Date of Birth: " << endl << dob;
		cout << "Phone number: " << endl << number;
		cout << "Major: " << endl << major;
	}
};

How am I supposed to show the information inputted / stored?

cout << "Student Information" << stdnt[4].print_structure <<endl;
This line was an attempt to display the inputted information as a single text block. If I'm not supposed to use void, what other function can I use?
You need to make the return type of the function, be the type of data you want to pass back to the calling function.

However, I don't understand why you're trying to output the return value from that function. Your print_structure() function already contains the statements that output the values; what extra information is that you want your main() to output?
I'm just trying to answer the question (in original post), it says:

"Test the Student structure in a main function. Creating an object of the Student type, assigning some meaningful values to all its fields and then printing them out.
Then in the same main function define an array of structures belonging to the Student structure and containing 3 elements. Assign all values to the elements of this array using initialization at the time of its definition. Display the content of the array created (use a loop)."

And I'm not entirely sure how to do so from here.
I'm just trying to answer the question (in original post), it says:

Well the first thing you need to do is follow the previous instructions as well.

Design a structure Student containing the following members:
1) Name;
2) Date of birth (it should be in turn a structure containing three members Year, Month, Day);
3) Phone number;
4) Major field of study.

Find appropriate data types for each field.


1. You need to have another structure for the Date of birth.

2. Why did you select C-strings instead of C++ strings? C++ strings are much safer and much much easier to use.

3. Where have you tried doing the following?
Test the Student structure in a main function. Creating an object of the Student type, assigning some meaningful values to all its fields and then printing them out.


You should be defining a single instance of this structure and assigning values to that single instance. No array needed for this part, the array comes later.

When you get to the array you will need to create some constructors so that you can initialize the structures when you declare your array, this will be much easier if you're using C++ strings instead of the horrible C-strings.






Thank you all for your help! I got it!
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
using namespace std;

struct student
{
	string name;
	string phone;
	string major;

	struct DOB
	{
		string day;
		string month;
		string year;
	};
	DOB x;
};

void print_stuff(student a)
{ 
	cout << "Student Name: " << a.name << endl;
	cout << "Date of Birth: " << a.x.month << "/" << a.x.day << "/" << a.x.year << endl;
	cout << "Phone Number: " << a.phone << endl;
	cout << "Field of Study: " << a.major << endl;
	return;
}

int main() 
{
	student user;
	student v1[3];
	user.name = "JB";
	user.x.day = "00";
	user.x.month = "00";
	user.x.year = "0000";
	user.phone = "718-862-8000";
	user.major = "Computer Engineering";

	print_stuff(user);

	for (int i = 0; i <= 2; i++) //Uses a loop to redo it three times
	{
		cout << endl;
		cout << "Student Record " << i + 1;
		cout << endl;
		cout << endl;
		cout << "Student Name: " << endl;
		getline(cin, v1[i].name);
		cout << "Year Born: " << endl;
		getline(cin, v1[i].x.year);
		cout << "Month Born: " << endl;
		getline(cin, v1[i].x.month);
		cout << "Day Born: " << endl;
		getline(cin, v1[i].x.day);
		cout << "Phone Number:" << endl;
		getline(cin, v1[i].phone);
		cout << "Major: " << endl;
		getline(cin, v1[i].major);
	}

	for (int i = 0; i <= 2; i++)
	{
		cout << "Student Record Print" << i + 1;
		cout << endl;
		print_stuff(v1[i]);
	}
	return 0;
}
Registered users can post here. Sign in or register to post.
  • 1007451581 2018-02-22
  • 8908121580 2018-02-22
  • 141161579 2018-02-22
  • 9421578 2018-02-22
  • 2826901577 2018-02-22
  • 3647361576 2018-02-22
  • 5717551575 2018-02-22
  • 523811574 2018-02-22
  • 6439871573 2018-02-22
  • 8109431572 2018-02-22
  • 8757321571 2018-02-22
  • 5265111570 2018-02-22
  • 3351351569 2018-02-22
  • 5109361568 2018-02-22
  • 4455391567 2018-02-22
  • 9091121566 2018-02-22
  • 24791565 2018-02-22
  • 2486841564 2018-02-21
  • 9847231563 2018-02-21
  • 9264681562 2018-02-21