Basic structure question

Hey guys, brand new poster here. So I was assigned to create a .h file containing the structure definition for a student to be used with code my professor provided. He gave me some notes to follow so I can understand how it works, and even though I am trying to apply it to my code, it just wont work. Here is the .cpp he provided me for the assignment.

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
#include<iostream>
#include"student4-13.h"   // define student and address structures
using namespace std;

void main()
{
    student s[2];  // array of two student structures
    int i, j;

    for (i = 0; i < 2; i++)
    {
    cout << "Enter Student First Name:  ";
    cin >> s[i].first;   // input into the ith student's first name field
    cout << "Enter Student Last Name:  ";
    cin.ignore(1, '\n');   // clear enter
    cin >> s[i].last;
    cout << "Enter Street Address:  ";
    cin.ignore(1, '\n');
    cin.getline(s[i].home.street, sizeof(s[i].home.street), '\n'); /* whole line for street*/
    cout << "Enter City:  ";
    cin.getline(s[i].home.city, sizeof(s[i].home.city), '\n');
    cout << "Enter State (ex. NY):  ";
    cin >> s[i].home.state;
    cout << "Enter Zip Code:  ";
    cin.ignore(1, '\n');
    cin >> s[i].home.zip;
    cout << "Enter Class Year:  ";
    cin >> s[i].year;
    cout << endl;
    }

            // pass student array to separate function - student pointer

    for (j = 0; j < 2; j++) // change to while pointer comparison loop
    {
    cout << "\n\n";    // change to print fields using pointer and arrow operator
    cout << s[j].first << " " << s[j].last << "  " << s[j].year << endl;
    cout << s[j].home.street << endl;
    cout << s[j].home.city << ", " << s[j].home.state << "  " << s[j].home.zip << endl;
    }
}


and here is the .h file I created with the definitions of both student and home.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
struct student
{
	char first[15];
	char last[20];
	int year[4];
};

struct homeStructure
{
	student home;
	char street[30];
	char state[2];
	int zip[5];
};


One of the errors that I am getting is "'home' : is not a member of 'student'"

And finally, the notes he provided as an example of how to do 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
#include <iostream>
using namespace std;

struct name
{
	char first[12];
	char middle;
	char last[20];
};

struct student
{
	name id;
	char SS[12];
	int classyear;
	float GPA;
	int credits;
};

void main()
{
	student cis202[10];  // array type student   10 students
	int i, best = 0;
	cout <<("Enter ten lines with names and GPAs\n");
	for (i = 0; i < 10; i++)
	{
	cin >> cis202[i].id.first >> cis202[i].id.middle
	>> cis202[i].id.last >> cis202[i].GPA;
	}
	for (i = 1; i < 10; i++)
	{
	if (cis202[i].GPA > cis202[best].GPA) best = i;
	}
	cout <<  cis202[best].id.first << " " 
	<< cis202[best].id.middle << " "
	<< cis202[best].id.last << " is the best\n"; 
}


I cant see what I am doing that would cause this error, while the notes he provided run without any problem. Could you explain how to fix this? He wont get any email I send him until next Monday, and by then I will be even further behind. Thank you in advance for your time :D

edit: added some clarification to what I needed
Last edited on
closed account (D80DSL3A)
home is a member of homeStructure, not student. Perhaps line 7 in main() should be homeStructure s[2]; instead of student s[2];
EDIT: You will then get other errors. eg. line 13 cin >> s[i].first; will need to be cin >> s[i].home.first; instead.
Last edited on
I reread the directions and made some changes, but I am still getting errors. I cant change the main code, only the header file.

Anyway, I came up with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct address
{
	char street[30];
	char state[2];
	int zip[5];
};

struct student
{
	address home;
	char first[15];
	char last[20];
	int year[4];
};


which is giving me errors such as "'city' is not a member of 'address'"

declarations such as cin >> s[i].last; & cin >> s[i].home.state; do not work.

However, in another program, declarations such as

1
2
cin >> cis202[i].id.first >> cis202[i].id.middle
	>> cis202[i].id.last >> cis202[i].GPA;


works with the structures

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct name
{
	char first[12];
	char middle;
	char last[20];
};

struct student
{
	name id;
	char SS[12];
	int classyear;
	float GPA;
	int credits;
};


Is there any way I can modify my definitions for the structures of student and address, to work with the main code in my first post?

I am trying to only post the problem code, rather than the entire thing to keep my posts from being enormous walls of texts, but if there is anything that I did not include that would help with solving my problem, I can try to post it.

Thank you
Last edited on
This is a simple matter of understanding what structures are.

Take, for example, a structure of student. You want student to represent all data relevant to, what(?): A Student. So, what is a student? Well, we need a name, we need any other relevant information. If we were to pass this stuff to functions whenever we needed it, it would be a vortex of entropy. A better way to group such data (and a much more easily managable way) is to gather it into object-like constructs that represent the data as a group.

So, knowing this, we need to apply syntax to it.

You clearly don;t understand what a member function is: a member function of a structure, or class, is declared directly in that structure or class.

Example:

1
2
3
4
5
struct student{

char first_name[20], last_name[20], middle_initial;
int year;
};


So, here we have a data structure. We 'named' it student. So, what are the members? Well, they are clearly declared: there is first_name, last_name, middle_initial, and year. When we want to access a member function through an instance of a structure, we can use the following syntax:

1
2
3
4
student a_single_student;

//what is the name of this student??
cout<< a_sing_student.first_name<< endl; //psuedocode, I don't work with arrays too often 


here, we reference a member function of and instance of student called a_single_student, and we called a member variable of the structure called first_name. Note that whenever we call a member, the entire object can be treated as whatever member you're calling (so if you are referencing an array, and can use the .operator[]() on it, and it will reference the index of the array, etc...), although somtimes you may need parentheses to clarify for the compiler what you're refering to (look up operator precedence). Try not to do this too much at your level, I do a lot of 1-liners because of this, lol.

So, I hope you better understand data structures. You will be working with much much more complicated objects in the future (if you're CS major, that is), and it is impairative you understand these basic things.

Hope this helps, let me know if you need any more help. Pardon any mistakes, as it's nearly midnight for me...
Thank you for the lengthy reply :D I understand quite a bit more now. What I still don't understand is how am I able to apply a second data structure to this, so that each Student will have their own address which is defined separately. In main, I will use both inputs such as

1
2
 
cin >> s[i].last;  // with s being an array of 2 students and a for loop that uses i 


and

 
cin >> s[i].home.state;


so I thought that if I defined the address home in student, then I could use it in main. What I am using is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct address
{
	char street[30];
	char state[2];
	int zip[5];
};

struct student
{
	address home;
	char first[15];
	char last[20];
	int year[4];
};


I'm confused as why it is not working, while another example (the structures of student and name way up above) worked for its respective program. What do I need to change in order to make it work?

Thank you in advance
Last edited on
First thing is first: you're not defining addresses, of variables. Read on variable initialization, and assignment.


As for your other problem:
think for a second: what is s[i].home.state? Is it an integer? An array? a character?

When you call a member, you have to use the correct syntax.

the reason your code is failing, is because you're basically doing this:

1
2
char lotsofchars[10] = "1234567890";
cout<< lotsofchars; //YOU CAN'T DO THIS!! 


arrays don't have an operator<<(char*) (overloaded << operator), so the compiler will throw an error. You need to use the index operator, again, to specify which element of the object you are referencing.

s[i].home.state[0];

Also, arrays' indecies begin at 0 (as do all indecies in C++), in case you did not know.

Also, forgive any syntax errors of my own, and I forget if operator<<() is left or right associative (meaning that iether cout didn't overload with a char array parameter, or char arrays don't have an overloaded operator for the cout stream object; either way though...)
********************************************************************

Did you not learn these in class???
huh, I guess you can.

@bamaut
Ah, but you're using cin. I don't believe you can do that, however, I may be wrong. You may have to get each char with a for loop (easier to change the number of loops) and just get each char.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct address
{
	char street[30];
	char city[30];// added city member
	char state[2];
	int zip;// no need for array
};

struct student
{
	address home;
	char first[15];
	char last[20];
	int year;//no need for an array
};

http://ideone.com/e8y2xM
Topic archived. No new replies allowed.