s7000001 10 10 12 75
s8000002 9 7 10 65
s0000001 10 15 15 75
Top Student List
s0000001 85.00
s7000001 77.00
s8000002 65.00
bool readStudents(string fName, studentType stuList[], int& size); { // declare an ifstream // open the ifstream using fName as the filename // if unsuccessfule, return false // intialize size to 0. // set up loop // read data into stuList[size] // increment size // if more data exists, repeat loop // close ifstream // return true }
Hey
I made this function i think it'll do the job. But i keep getting an expected declaration before "}". What do you guys think?
bool readStudents(string fName, studentType stuList[], int& size)
{
ifstream inFile;
studentType stu;
size = 0;
inFile.open (fName.c_str());
if (inFile.is_open())
{
inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total;
while(inFile)
{
size++;
cout << stu.id << ' ' << stu.assignments[0] << ", " << stu.exam << ' ' << stu.total << endl;
inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total;
}
return true;
}
else
return false;
// the close() function takes no parameters,
// the name of the file is already known by the ifstream object,
// so just simply ...
inFile.close ();
}
} // <- This brace must be removed
int main() { for (int i = 0; i < 5;i++) { for (int j= 0; j < 5;j++) { if (j==i) { cout << "yeah"; } } } return 0; }
int main() { for (int i = 0; i < 5;i++) { for (int j= 0; j < 5;j++) { if (j==i) { cout << "yeah"; } } } return 0; }
bool readStudents(string fName, studentType stuList[], int& size) { ifstream inFile; studentType stu; size = 0; inFile.open (fName.c_str()); if (inFile.is_open()) { inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total; while(inFile) { int n = 0; size++; cout << stu.id << ' ' << stu.assignments[n] << ", " << stu.exam << endl; inFile >> stu.id >> stu.assignments[n] >> stu.exam; n++; for( int i = 0; i < 10; i++ ) // loads the student list into the array { cin >> stuList[i].id >> stuList[i].assignments[0] >> stuList[i].assignments[1] >> stuList[i].assignments[2] >> stuList[i].exam; cout << endl; } } return true; } else return false; }
Ok thanks guys for all the help sorry to be such a n00b at all this. I've got it to sorta work now, but it seems to only do one line and not load all the values of the text document into the stuList[] array.
Can anybody see a fix for this?
C++ Syntax (Toggle Plain Text)
bool readStudents(string fName, studentType stuList[], int& size) { ifstream inFile; studentType stu; size = 0; inFile.open (fName.c_str()); if (inFile.is_open()) { inFile >> stu.id >> stu.assignments[0] >> stu.exam >> stu.total; while(inFile) { int n = 0; size++; cout << stu.id << ' ' << stu.assignments[n] << ", " << stu.exam << endl; inFile >> stu.id >> stu.assignments[n] >> stu.exam; n++; for( int i = 0; i < 10; i++ ) // loads the student list into the array { cin >> stuList[i].id >> stuList[i].assignments[0] >> stuList[i].assignments[1] >> stuList[i].assignments[2] >> stuList[i].exam; cout << endl; } } return true; } else return false; }
struct studentType { string id; // unique id float assignments[3]; // scores for 3 assignments - in the scope of [0,15] float exam, total; // member exam is the exam score in [0, 100]; member total is the total score in [0, 100] };
s9000004 6 9 6 70
s7000001 10 10 12 75
s8000002 9 7 10 65
s8000004 6 9 6 50
s7000002 9 7 10 45
s7000004 6 9 6 50
s0000002 9 7 10 35
s0000004 6 9 6 50
s0000001 10 15 15 75
struct studentType { string id; // unique id float assignments[3]; // scores for 3 assignments - in the scope of [0,15] float exam, total; // member exam is the exam score in [0, 100]; member total is the total score in [0, 100] };
s9000004 6 9 6 70
bool readStudents(string fName, studentType stuList[], int& size) { ifstream inFile; studentType stu; size = 0; inFile.open (fName.c_str()); if (inFile.is_open()) { inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam; while(!inFile.eof()) { size++; cout << stu.id << ", " << stu.assignments[0] << ", " << stu.assignments[1] << ", " << stu.assignments[2] << ", " << stu.exam << endl; inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam; for( int i = 0; i < 10; i++ ) // loads the student list into the array { cin >> stuList[i].id >> stuList[i].assignments[0] >> stuList[i].assignments[1] >> stuList[i].assignments[2] >> stuList[i].exam; cout << endl; } } return true; } else return false; inFile.close (); }
Hey guys. The program is still having issues. It seems to only get the first line from the text file and i dont think its putting it into the array.
Any help would be appreciated
C++ Syntax (Toggle Plain Text)
bool readStudents(string fName, studentType stuList[], int& size) { ifstream inFile; studentType stu; size = 0; inFile.open (fName.c_str()); if (inFile.is_open()) { inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam; while(!inFile.eof()) { size++; cout << stu.id << ", " << stu.assignments[0] << ", " << stu.assignments[1] << ", " << stu.assignments[2] << ", " << stu.exam << endl; inFile >> stu.id >> stu.assignments[0] >> stu.assignments[1] >> stu.assignments[2] >> stu.exam; for( int i = 0; i < 10; i++ ) // loads the student list into the array { cin >> stuList[i].id >> stuList[i].assignments[0] >> stuList[i].assignments[1] >> stuList[i].assignments[2] >> stuList[i].exam; cout << endl; } } return true; } else return false; inFile.close (); }
| DaniWeb Message | |
| Cancel Changes | |