I did point to that problem already in my previous post:
Chervil wrote: |
---|
So far there is only space to hold a single lesson, |
The solution, as I mentioned several times in this thread, is to allow space for multiple lessons, by using an
array or
vector.
In my previous post, I added this code:
1 2 3 4 5
|
void Lesson::addLesson(int num, std::string lesson)
{
nr = num;
this->lesson = lesson;
}
|
This is actually needed. Because we don't know in advance how many different lessons there will be, then we must repeatedly
add each one as it is read from the file.
Of course currently, the values are always placed in the
same variables,
nr
and
lesson
, so whichever value was most recently stored will be the one you see printed out.
What you need to do is to keep a count of how many lessons have been added, and store each one in a new location in the array.
For example you could change
1 2
|
int nr;
string lesson;
|
to:
1 2 3
|
int nr[6];
string lesson[6];
unsigned int count;
|
Then, set the initial value of count to 0 when the Lesson object is created. Each time the function
addLesson()
is called, store the data in the latest position and add 1 to count.
There will be a few other changes following on as a consequence of making that change, for example like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int GetNr(unsigned int n) const {
if (n<count)
return nr[n];
else
return 0;
}
std::string GetLesson(unsigned int n) const {
if (n < count)
return lesson[n];
else
return "";
}
unsigned int getCount() const { return count; };
|