There are a couple of problems here.
1 2 3 4 5
|
while (infile)
{
//Read data into infile
infile>>studentid[idsize];
infile>>answers[answersize];
|
One, studentid[idsize] is a single character located in memory just past the end of the studentid array. That is, it doesn't read a string, it just reads a character. And unfortunately it stores it in a memory location which is not owned by your program, causing potential corruption of some other data with unpredictable results.
It should be simply
infile>>studentid;
The same applies to the answers array.
The second problem is the while loop. It is testing the status of the file
before reading from it. But what the program really needs to know is what is the status
after attempting to reading from the file.
Thus the code should be changed like this, solving both issues:
1 2 3 4 5 6 7
|
// Read data from infile
while (infile >> studentid >> answers)
{
// output data into outfile
outfile<<"\nID: ";
idreader(studentid,10,outfile);
}
|
But there we encounter a second problem. What is the value '10' passed as a parameter to idreader()? Apparently it tells that function that it needs to output 10 characters from the studentid[] array , even though the actual data is only 7 characters long. "abcdefs".
It would be simpler and safer to simply put:
1 2 3 4 5 6
|
// Read data from infile
while (infile >> studentid >> answers)
{
// output data to outfile
outfile<<"\nID: " << studentid;
}
|