Hello adventccy,
@gunnerfunner I was thinking cin works fine, as my input does not include any spaces. |
That may work for now, but something may change in the future. And you never know what a user of the program might do. A future assignment might have you validate the file name that a user will input where you might have to deal with a space.
@HandyAndy The number of files needed to be opened depends on how many subjects |
. That was not clear in the beginning, but now I understand. I did not that all you do in the program is open files, but you never close a file. Not sure what the limit is on open files so this could become a problem at some point.
I was starting the loop as counter=1,and the loop stops reading from the first text file after counter becomes 10, surely this would prevent that? |
Yes and no. See point 2) of AbstractionAnon message. All I can add to that is when the subscript is at maximum and you are outside the bounds of the array you do not know what bit of memory you are messing with or what kind of problem it will cause with your code or something else. If you have forgotten this any version of the C languages along with several other programing languages are zero based. Starting your counter at 1 means that you are not using the very first element of the array which would make that wasted memory.
In the code of your last message line 7 should have been
moved to line 5 not
copied. As you have it you are reading into the same variable twice and that will through everything off. The code should be:
1 2 3 4 5 6 7 8 9 10
|
else
{
int y=1;
while(get>>subjects[counter].students[y].ID)
{
get>>(subjects[counter].students[y].name);
get>>subjects[counter].students[y].marks;
y++;
}
}
|
this way when the end of file is reached the while loop will fail and you will not have an empty record at the end.
(stream>>var) stream refers to any kind of stream i.e., cin, cout, cerr, a file or any other way of receiving input/ The ">>var" tells the input where to go. I look at that as a shorthand rather than using proper names.
Hope that helps,
Andy