Help with HW using pointers and dynamic arrays

I'm trying to pass info from a .txt file into a struct I'm having trouble with the part of passing the classes into the string *courses. I have to keep the struct design as it is. Thanks for any help I've been looking at this for a while and can't figure it out.

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

struct Student
{
	string name;
	int ncourses;
	string *courses;
};

int main()
{
	ifstream fin;
	int sizeA;
	int sizeB;
	string temp;


	fin.open("courses.txt");
	if (fin.fail())																
	{
		cerr << "Open Failure\n\n";
		system("pause");
		exit(1);
	}

	fin >> sizeA;
	fin.ignore(80, '\n');
	Student *list = new Student[sizeA];

	for (int ct = 0; ct < sizeA; ct++)
	{
		getline(fin, list[ct].name);
		fin >> list[ct].ncourses;
		sizeB = list[ct].ncourses;
		Student *temp = new Student[sizeB];
		
		for (int k = 0; k < sizeB; k++)
		{
			fin >> temp;
			list[ct].courses[k] = &temp;
		}
	}

	
	

	fin.close();
	return 0 << system("pause");
}


this is what the .txt file looks like

12
John Milligan
3 CIS100 CIS105 MAT113
Jill Kerning
5 CIS100 CIS105 MAT232 BIO100 ENG101
Aaron Spencer
4 CIS201 CIS225 MAT232 ENG101
Damon Hill
2 CIS334 CIS400
Kaitlyn Stamen
4 CIS100 CIS10 MAT113 BIO100
Debbie Martin
5 CIS100 CIS105 MAT232 CHY112 ENG101
Greg Nolan
2 CIS334 CIS450
Lynn Sanders
4 CIS334 CIS450 MAT250 BIO100
Alicia Thomas
4 CIS226 CIS450 MAT232 CHY112
Alan Turner
5 CIS100 CIS105 MAT232 BIO100 ENG101
Paul Henley
5 CIS100 CIS105 CIS334 ENG101 MAT232
Tim Copeland
1 CIS450
Last edited on
Why don't you just dynamically allocate the 'courses' variable and do it like the students list?

38
39
40
41
42
fin >> list[ct].ncourses;
list[ct].courses = new std::string[list[ct].ncourses];

for (int k = 0; k < list[ct].ncourses; ++k)
    fin >> list[ct].courses[k];


Don't forget to free the memory at the end, too:
50
51
52
for (int i = 0; i < sizeA; ++i)
    delete list[i].courses;
delete list;


EDIT:
Just noticed: What on earth is going on with line 53? That doesn't look like it should compile (even though it should when you think about it). Make that line more sensible, please (preferably replacing system("pause") with something better, like std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');).
Last edited on
Hi,

Please refer to the following code, corrected code shown in bold

'courses' are of type 'string', but you are trying to create student objects of sizeB


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int ct = 0; ct < sizeA; ct++)
{
	getline(fin, list[ct].name);
	fin >> list[ct].ncourses;
	sizeB = list[ct].ncourses;
	list[ct].courses=new string[sizeB];
		
	for (int k = 0; k < sizeB; k++)
	{
		fin >> temp;
		list[ct].courses[k] = temp;
	}

	getline(fin,temp);//read till end of the courses
}
Last edited on
Thanks I appreciate the help, sorry it took me a little bit to reply had to go to class. My professor had confused the heck out of me when she covered this subject. As for line 53 yea I know that's something special going on there. That was the way our professor taught us just as a simple fix. I'll definitely correct that line, I didn't really know the proper way of doing it and knew we where just given a simple fix to the problem.
Last edited on
Topic archived. No new replies allowed.