getting strange error PLEASE HELP

Pages: 12
my text file wont display. when i run it the screen is blank. There are only 5 lines of text in the file
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
37
38
39
40
41
42
43
44
45
46
47
48
const int NUM_SCORES = 5;

typedef struct 
 {
	int day;
	int month;
	int year;
 } Date; 

typedef struct 
 {
	int id;
	int firstName;
	int surName;
	Date dob;
	float scores[NUM_SCORES];
 } Student;

typedef Student ItemType; 

ItemType aStudent;



int _tmain(int argc, _TCHAR* argv[])
{
	
	ifstream infile ("students.txt");	
    while(!infile.eof())
		for (int i = 0; i < 5; i++)
		{
			infile >> aStudent.id >> aStudent.firstName >> aStudent.surName >> aStudent.dob.day >> aStudent.dob.month >> aStudent.dob.year
			   >> aStudent.scores[0] >> aStudent.scores[1] >> aStudent.scores[2] >> aStudent.scores[3]
			   >> aStudent.scores[4];
		}

		cout << "Id\tFirst name\tSurname\t\tDate\t\tScores\n"; 
	for (int i = 0; i < 5; i++)
		{
			cout << aStudent.id << "\t" << aStudent.firstName << "\t" << aStudent.surName << "\t" <<  aStudent.dob.day << "/" << aStudent.dob.month <<"/" << aStudent.dob.year 
			<< "\t" << aStudent.scores[0] << "\t" << aStudent.scores[1] << "\t" << aStudent.scores[2] << "\t" << aStudent.scores[3] << "\t"
			<< aStudent.scores[4];
		}

	infile.close();	 
	
	return 0;
}


thanks in advance
Last edited on
can someone please help
Your indentation is a bit off so I'm not sure if it's intentional but only the first for loop is inside the while loop.

what do you mean? do i need to move the brackets?
You have no brackets for the while loop, so inside the while loop is only one for loop. I'm not sure if that is what you want.
ok but if i add more brackets it just makes an infinite loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ifstream infile ("students.txt");	
    while(!infile.eof())
	{
		for (int i = 0; i < 5; i++)
		{
			infile >> aStudent.id >> aStudent.firstName >> aStudent.surName >> aStudent.dob.day >> aStudent.dob.month >> aStudent.dob.year
			   >> aStudent.scores[0] >> aStudent.scores[1] >> aStudent.scores[2] >> aStudent.scores[3]
			   >> aStudent.scores[4];
		}

		cout << "Id\tFirst name\tSurname\t\tDate\t\tScores\n"; 
	for (int i = 0; i < 5; i++)
		{
			cout << aStudent.id << "\t" << aStudent.firstName << "\t" << aStudent.surName << "\t" <<  aStudent.dob.day << "/" << aStudent.dob.month <<"/" << aStudent.dob.year 
			<< "\t" << aStudent.scores[0] << "\t" << aStudent.scores[1] << "\t" << aStudent.scores[2] << "\t" << aStudent.scores[3] << "\t"
			<< aStudent.scores[4];
		}
	
	}
	infile.close();	
	

my problem was the the file wasnt displaying
Maybe you find this interesting http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5

If something goes wrong while reading the file I don't think eof() will give you true, so you will get stuck in an infinite loop.
ok but in that example while (std::cin >> x) they just want to output x. i just took out my while loop and now this is displaying

Id      First name      Surname         Date            Scores
10019   0               0               0/0/0           0 0 0 0 0
10019   0               0               0/0/0           0 0 0 0 0
10019   0               0               0/0/0           0 0 0 0 0
10019   0               0               0/0/0           0 0 0 0 0
10019   0               0               0/0/0           0 0 0 0 0
Press any key to continue . . .

as you can see its only displaying the first id 5 times and the rest of the values are 0

heres the text file i'm trying to display
10019 Lisa Collins 19 12 1988 1 1 1 1 1
10053 Tom Kelly 16 9 1985 2 2 2 2 2
10031 John Riley 1 5 1986 3 3 3 3 3
10012 Ann Bynes 12 3 1984 4 4 4 4 4
10044 Mary Burke 31 8 1986 5 5 5 5 5
firstName and surName should probably be strings, not integers
ok now there is a red line under the >> after aStudent.id of line 32 saying no operator matches these operands
What does the error message tell you?
its a huge error message. heres some of it
error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::basic_istream<_Elem,_Traits>' (or there is no acceptable conversion)
I don't know.

What type did you change firstName and surName to? std::string?
ya its string. the problem must be with the string because there was no errors when firstName and surName were int
any idea what to do?
show your code
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "stdafx.h"
using namespace std;
#include <iostream>
#include <fstream>

const int NUM_SCORES = 5;

typedef struct 
 {
	int day;
	int month;
	int year;
 } Date; 

typedef struct 
 {
	int id;
	string firstName;
	string surName;
	Date dob;
	float scores[NUM_SCORES];
 } Student;

typedef Student ItemType; 

ItemType aStudent;

int _tmain(int argc, _TCHAR* argv[])
{
	
	ifstream infile ("students.txt");		
		for (int i = 0; i < 5; i++)
		{
			infile >> aStudent.id >> aStudent.firstName >> aStudent.surName >> aStudent.dob.day >> aStudent.dob.month >> aStudent.dob.year
			   >> aStudent.scores[0] >> aStudent.scores[1] >> aStudent.scores[2] >> aStudent.scores[3]
			   >> aStudent.scores[4];
		}

		cout << "Id\tFirst name\tSurname\t\tDate\t\tScores\n"; 
	for (int i = 0; i < 5; i++)
		{
			cout << aStudent.id << "\t" << aStudent.firstName << "\t\t" << aStudent.surName << "\t\t" <<  aStudent.dob.day << "/" << aStudent.dob.month <<"/" << aStudent.dob.year 
			<< "\t\t" << aStudent.scores[0] << " " << aStudent.scores[1] << " " << aStudent.scores[2] << " " << aStudent.scores[3] << " "
			<< aStudent.scores[4] << endl;;
		}
	
	
	infile.close();	
	
	
	return 0;
}

it hasnt really changed from my first post
Last edited on
Strange because it compiles for me (after removing the microsoft specific parts).

Try putting using namespace std; after the includes. I don't know if that should make a difference but worth a try.
no didnt make a difference. maybe there is something wrong with the visual studios im using. i will have to wait til tomorrow and try again on a different computer
still doesnt work on a differerent computer
Pages: 12