Wrong output from a file. Help please

I have made the following code here that will read the test scores from this input file here.
34
34
34
234
213
23
12
Please be aware that more tests scores will be entered thus me not finding the exact size of the array. The total number of tests would be less then 50 though. Now that was the input here, the output after constructing my code. I got this here
34
234
23
12
May anyone please tell me what i'm doing wrong. I would highly appreciate it. Thank you for taking your time to read this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 void studenttestreader (int testscores[],int size,ofstream &out, ifstream&in)
{
	//Total number of students not defined. There would be less then 50 though. So have numberofstudents declared to 0;
	int numberofstudents=0;


	//while file is open

	while (in>>testscores[numberofstudents] && numberofstudents<size)
	{
		in>>testscores[numberofstudents];
		
			out<<testscores[numberofstudents];
			out<<endl;
		
		
		numberofstudents++;
	
	
	}
	


Aslo, there is a similar post like this, so please ignore that. I tried deleting it, but realized I couldn't really on this forum.
Last edited on
Psuedocode for your algorithm:
1
2
3
4
5
6
7
8
    nStudents = 0 ;
    while ( extraction from input stream for student is successful and nStudents is less than size )
    {
         extract info for student from input stream
         write student to output stream

         increment nStudents
    }


How many times should you extract information from the input stream for one student?
For one student, I should extract information only once...I did a similar approach a couple of days ago and it worked like this too, but i'm just totally lost how to do those while loops when reading files :(


I was using this approach from another assignments problem; however im aware im wrong but don't know how. would like to know what the problem is :(

Edit: meaning to say that I was using this approach from this following code which works.
1
2
3
4
5
6
7
8
9
10
void ReadStudentid_TestAnswers(ifstream &infile, ofstream& outfile,char studentid[], int idsize,char answers[],int answersize)
{
	studentid[idsize];
	answers[answersize];
	while (infile>>studentid>>answers)
	{
	//Read data into infile 
	
	infile>>studentid;
	infile>>answers;
Last edited on
Edit: meaning to say that I was using this approach from this following code which works.


For some definition of works. That code ignores every other record.
Cire,mince you said that, why exactly does it ignore every other record?
Totally lost, if your talking about my other code ignoring every other record, then do you mind explaining why and what is the best solution for it please.
Also, that other code which I posted did work, except I got one random output. In the end.
1
2
3
4
5
6
    while ( infile >> studentid >> answers )
    {
        infile >> studentid ;
        infile >> answers ;
        // ...
    }

Cire,mince you said that, why exactly does it ignore every other record?


You read one record in in the loop condition. You don't do anything with that record (iow, you ignore it.) You immediately overwrite it by reading in the next record inside the loop body. This happens for every iteration of the loop.

Perhaps your actual code differs from your reproduction.


I was looking and researching over the internet and they all seem to be using this example. Not saying your wrong. I'm pretty sure your right aince your more expierenced them me. Meanwhile, if this does ignore every other record, then what can I do not to ignore it?

Also,while(infile>>studentid>>answers) when I enter in a data for instance
87
. Does this input start from "while(infile>>......) or underneath the while part of code.
Cire,I think I know how to fix it. Would I just have to delete the line of code indile>>studentidand infile>>answers under the while loop
Cire, I have it fixed. I deleted the lines and it worked fine this time :) Thanks for the hints!!! :)
Topic archived. No new replies allowed.