Printing the same line twice from a file and not the others.

When I run my code I get only the first line(Adara Starr,94) of the file printed twice. I'm having trouble getting it to read all of the line and print them all out. Any help will do. Thank You.

The "grades.txt" file looks like this:

Adara Starr 94
David Starr 91
Sophia Starr 94
Maria Starr 91
Danielle DeFino 94
Dominic DeFino 98
McKenna DeFino 92
Taylor McIntire 99
Torrie McIntire 91
Emily Garrett 97
Lauren Garrett 92
Marlene Starr 83
Donald DeFino 73

My code is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <fstream>
#include <iostream>
using namespace std;


const int  MAXNAME = 20;

int main()
{
	ifstream inData;
	inData.open("grades.txt");

	char name[MAXNAME + 1];  // holds student name
	float average;  // holds student average

	
	inData.get(name, MAXNAME + 1);

	while (inData)
	{
		
		cout << name;
		inData >> average;
		cout <<  average << endl;
		// FILL IN THE CODE to print out name and 
		// student average
		

		
		// FILL IN THE CODE to complete the while 
		// loop so that the rest of the student
		// names and average are read in properly
	}
	system("pause");
	return 0;
}
what if you replace line 19 with

 
while (!indata.eof())
My teacher included that in the program that I have to complete. The places have I have to input code are specified with comments. I dont think she would let me change that.
Well then you need to talk to your teacher because the program will fail for most if not all of the lines in your file because of line 17. Almost all of the lines contain less than the 21 characters that line will try to retrieve. Plus if the line has any more than 19 characters you will be overflowing the array for the name.

Ok. Im considering using your suggestion. What exactly does that function do? After i place it into my code it prints the first line infinitely.
Last edited on
If I understand correctly, the file data.txt looks like this:

Adara Starr          94
David Starr          91
Sophia Starr         94
Maria Starr          91
Danielle DeFino      94
Dominic DeFino       98
McKenna DeFino       92
Taylor McIntire      99
Torrie McIntire      91
Emily Garrett        97
Lauren Garrett       92
Marlene Starr        83
Donald DeFino        73

That is to say, it is in a fixed-width layout. In which case the original code was at least somewhat reasonable, though it may need some adjustment.
Sorry for the late response -- Someone needed me in real life for a while and I just got back.

@b29hockey
Do not loop on EOF.

@jlb
You are close to correct. The professor accidentally left off an important piece of information: the delimiting character should be a space.

17
18
19
20
	inData.get(name, MAXNAME + 1, ' ');

	while (inData)
	{

This is correct code, though it does not follow your standard C++ idiom. OP's professor may have done this on purpose so that his students understand what is being done before teaching the normal idiom.

At no time can it overflow the array.

@bankaijoey
Make sure you do mention the missing delimiter character to your professor.

Where it says "FILL IN THE CODE to print out name and student average" ... it is already done. (I don't know if you did that or if you just did it and stuck in above the comment.)

Where it says "FILL IN THE CODE to complete the while loop...", remember that you must read two things every time through the loop: the name and the grade.

1 Before entering the loop, you read the name.
2 If that was successful, you entered the loop.
  - Inside the loop, you get the grade.
  - What must you do inside the loop before going back to step 2 (looping back to step 2)?

Hope this helps.
@jlb
You are close to correct. The professor accidentally left off an important piece of information: the delimiting character should be a space.

Actually the fields should probably be delimited with a tab character ('\t') since there is a space between the first and second name. Or by using a fixed width as you suggested with the width of the string longer than the size specified in the get() call.

However since the OP stated he couldn't change anything but in the area provided the OP really should check with the teacher before making any changes.

At no time can it overflow the array.

You are correct, I missed the fact that the array was similarly sized.


Last edited on
Topic archived. No new replies allowed.