help with coding?

hey guys, i need some help with this code here. i dont know what's wrong, everytime it runs the getline(cin, name), it crashed. here's the code:

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;

class student
{
	string name, dummy;
	char gender;
	int ID;
	
	public:
	
	student() : name("Unknown"), ID(0), gender('n')
	{}
	student (string name, int ID, char gender)
	{
		this->name = name;
		this->ID = ID;
		this->gender = gender;
	}
	void getstudent();
	void print() const
	{
		cout << ID << ", " << name << ", " << gender << endl;
	}
};

void student::getstudent()
{
	cout << "Enter student's ID: ";
	cin >> ID;
	cout << "Enter student's gender (M/F): ";
	cin >> gender;
	getline(cin, dummy);
	cout << "Enter student's name: ";
	getline(cin, name);
}

class room
{
vector<student*> students;
vector<student*>::iterator it;

public:

void addstudent(student* s)
{
	students.push_back(s);
}
void print()
{
	for(int i = 0; i < students.size(); i++)
	students[i]->print();
}
};

int main()
{
	room r1;
	int i = 0;
	student s1[i];
	string name;
	char gender;
	char choice;
	cout << "Add student? (Y/N):";
	cin >> choice;
	choice = toupper(choice);
	
	while (choice == 'Y')
	{
		s1[i].getstudent();
		
		r1.addstudent(&s1[i]);
		
		cout << "add more students?(Y/N): ";
		cin >> choice;
		choice = toupper(choice);
		
		i++;
	}
	
	r1.print();
}


any ideas what could be causing the crash? in my mind it might be a memory leak or something but i dont know the source of it.
Last edited on
¿what is the size of the 's1' array?
it starts with 0, and then increases as i add more students..
Nope, arrays don't work that way.
They have fixed size, and the index goes from 0 to n-1. (that said, a 'zero-size' array is illegal).
ic, so what can i do to make it work?
use a vector, adding elements with push_back
i did that using the vectors in the class. i was hoping to push_back into an array to store the information in there. if i did not use the array, the program works but the 2nd input will override the first input.

for example the first input is, ID: 1, gender: M, name: dennis
2nd input is, ID: 2, gender: F, name: Alicia,

when i print out it will print only the second input like this

2, F, alicia
2, F, alicia
i tried placing making an array of size ten, but once again it crash whenever it runs the getline(cin, name)
any ideas?
Vector is actually a good idea to use, but if you're getting the same output on every line, then you're using the iterator wrong. I would suggest going back to whatever you had before where it was using a vector and printing out only the (last?) second input (there is a difference between last and second), and try to debug from there.
i manage to do it using vector reference instead of pointers, here's the changed code:

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
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;

class student
{
	string name, dummy;
	char gender;
	int ID;
	
	public:
	
	student() : name("Unknown"), ID(0), gender('n'){}
	student (string name, int ID, char gender)
	{
		this->name = name;
		this->ID = ID;
		this->gender = gender;
	}
	student(const student& s): name(s.name), ID(s.ID), gender(s.gender){}
	virtual void getstudent();
	void print() const
	{
		cout << ID << ", " << name << ", " << gender << endl;
	}
};

void student::getstudent()
{
	cout << "Enter student's ID: ";
	cin >> ID;
	cout << "Enter student's gender (M/F): ";
	cin >> gender;
	getline(cin, dummy);
	cout << "Enter student's name: ";
	getline(cin, name);
}

class room: public student
{
vector<student> students;
vector<student*>::iterator it;

public:

void addstudent(student& student)
{
	students.push_back(student);
}
void print()
{
	for(int i = 0; i < students.size(); i++)
	students[i].print();
}
};

int main()
{
	room r1;
	student s1;
	string name;
	char gender;
	char choice;
	cout << "Add student? (Y/N):";
	cin >> choice;
	choice = toupper(choice);
	
	while (choice == 'Y')
	{
		s1.getstudent();
		
		r1.addstudent(s1);
		
		cout << "add more students?(Y/N): ";
		cin >> choice;
		choice = toupper(choice);
	}
	
	r1.print();
}


im sure it could be done with pointers too, if anyone knows how please let me know.
I guess you do it with pointers but the way your classes are structured it would just make it harder to maintain. I would lose all the pointers and just pass in copies of the student. And why did you make room inherit student?

Inheritance is for (is-a) relationships. Is a room a type of student? ; ) If you want to make a special type of student or a type of room than inheritance can help you.


Using pointers to dynamic data. It's not exactly great structure for it though.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
using namespace std;


class student
{
public:
	student() : name("Unknown"), ID(0), gender('n'){}
	student (string initName, int initID, char initGender)
	{
		name = initName;
		ID = initID;
		gender = initGender;
	}
	student(const student& s): name(s.name), ID(s.ID), gender(s.gender){}
	virtual void getstudent();
	void print() const
	{
		cout << ID << ", " << name << ", " << gender << endl;
	}
private:
	string name, dummy;
	char gender;
	int ID;
};

void student::getstudent()
{
	cout << "Enter student's ID: ";
	cin >> ID;
	cout << "Enter student's gender (M/F): ";
	cin >> gender;
	getline(cin, dummy);
	cout << "Enter student's name: ";
	getline(cin, name);
}

class room
{
public:
	~room() {
		for (it = students.begin(); it != students.end(); it++)
			delete *it;
	}
	void addstudent(student* s)
	{
		students.push_back(s);
	}
	void print()
	{
		for(int i = 0; i < students.size(); i++)
			students[i]->print();
	}
private:
	vector<student*> students;
	vector<student*>::iterator it;
};

int main()
{
	room r1;
	student* tmp = NULL;
	string name;
	char gender;
	char choice;
	cout << "Add student? (Y/N):";
	cin >> choice;
	
	while (toupper(choice) == 'Y')
	{
		tmp = new student;		
		tmp->getstudent();
		r1.addstudent(tmp);		

		cout << "add more students?(Y/N): ";
		cin >> choice;
	}
	
	r1.print();

	return 0;
}
Last edited on
i think if destructor can also be written as ,

1
2
3
4
5
6
7
8
9
~room() {
                  student *tmp;

		for (it = students.begin(); it != students.end(); it++)
                 {
                     tmp = *it; 
			delete tmp;
                 }
	}


Topic archived. No new replies allowed.