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.
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');).
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
}
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.