So my program works all right, but I can't read my information from a file into my vector, what am I doing wrong?
Code:
vector<char> grades;
grades.push_back ('A');
grades.push_back ('B');
grades.push_back ('C');
grades.push_back ('D');
grades.push_back ('E');
// declare a place to hold scores read from a file
//int scores[10]={0};
vector<int> scores ;
// declare the stream object and open the file
ifstream theDataFile;
theDataFile.open("c:\\scores.txt");
if (theDataFile.rdstate() !=0)
{
cout << "\nError opening file.";
exit(1);
}
else
cout <<"\nThe File opened correctly";
// read the scores into the array
int i = 0;
int aScore = 0;
int index = 0;
while(!theDataFile.eof( ))
{
theDataFile >> aScore;
if(!theDataFile.good( )) // the read failed ...
{
if (!theDataFile.eof( )) // and it was not an eof condition
{
cout << "\nError reading file.";
exit(1);
}
break; // it was an eof, so break out of the loop
}
//scores[index++] = aScore;
scores.push_back(aScore);
}
// print out the values just read and give each a grade
for (int i = 0; i < index; i++)
{
cout << scores.size () ;
if (scores[i] < 61)
cout << grades [4]; // grade is an 'E'
else if (scores[i] < 71)
cout << grades [3]; // grade is a 'D'
else if (scores[i] < 81)
cout << grades [2]; // grade is a 'C'
else if (scores [i] < 91)
cout << grades [1]; // grade is a 'B'
else
cout << grades [0]; // grade is an 'A'
cout << endl;
}
system("PAUSE");
return 0;
Comment