Nov 24, 2011 at 1:55pm UTC
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 Nov 27, 2011 at 5:54pm UTC
Nov 24, 2011 at 7:53pm UTC
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.
Nov 24, 2011 at 8:05pm UTC
what do you mean? do i need to move the brackets?
Nov 24, 2011 at 8:08pm UTC
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.
Nov 24, 2011 at 8:36pm UTC
firstName and surName should probably be strings, not integers
Nov 24, 2011 at 8:43pm UTC
ok now there is a red line under the >> after aStudent.id of line 32 saying no operator matches these operands
Nov 24, 2011 at 8:50pm UTC
What does the error message tell you?
Nov 24, 2011 at 9:17pm UTC
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)
Nov 24, 2011 at 9:22pm UTC
I don't know.
What type did you change firstName and surName to? std::string?
Nov 24, 2011 at 9:29pm UTC
ya its string. the problem must be with the string because there was no errors when firstName and surName were int
Nov 24, 2011 at 10:56pm UTC
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 Nov 24, 2011 at 10:56pm UTC
Nov 24, 2011 at 11:02pm UTC
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.
Nov 24, 2011 at 11:04pm UTC
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
Nov 25, 2011 at 11:17am UTC
still doesnt work on a differerent computer