My intention is to test for a data type token in the
studentdata.txt file, either "HWK," "EXM," or "END."
If it is HWK then the next number is added to a homework
score array; if it is EXM, then the next number is added to
an exam score array. After END is parsed, the inside while
loop breaks and finishes the calculations for the particular
student, and then outputs the information in an organized
manner.
The studentdata.txt file is set up like this:
John Doe HWK 67 HWK 43 EXM 88 HWK 76 HWK 49 EXM 95 HWK 28 HWK 64 EXM 80 END
Adam Rest HWK 48 HWK 82 EXM 98 HWK 73 HWK 55 EXM 100 HWK 68 HWK 54 EXM 70 END
Et cetera. 6 homework scores, 3 exam scores. If you need the
actual file, ask.
No values are being added into the homework array because dataType == "HWK" and dataType == "END" are NEVER being true. Although, for testing, I printed out dataType and it printed out as "HWK," so I don't know why it is not working. Also, the while (dataType != "END") is never ending, because it seems as though "END" is never being put into dataType.
I'd be grateful for any help. I'm new at C++, and my teacher isn't
much help because he totally doesn't care, so if there is some
stupid mistake, go easy on me. I'd appreciate any help beyond
the span of human comprehension.
Code:#include<iostream> #include<fstream> using std::cout; using std::cin; using std::endl; using std::ios; using std::ifstream; using std::ofstream; int main() { ifstream fin; ofstream fout; char firstName[30]; //student's first and char lastName[30]; //last name char dataType[4]; //holds type of following score int homework[6] = {0}; //array of student's homework scores int exam[4] = {0}; //array of student's exam scores float totalAvg = 0; //Total weighted average float examTotal = 0;//Exam scores totaled float examAvg = 0; //Exam average float homeworkTotal = 0; //Homework scores totaled float homeworkAvg = 0; //Homework average int score = 0; int a = 0; //holds homework index int b = 0; //holds exam index //Opens the data file fin.open("studentdata.txt", ios::in); fout.open("studentGrades.txt", ios::out); //Tests to see if the input file were opened. //If opened correctly, we move on. if (!fin) { cout << "File could not be opened for input."; exit(1); } //Tests to see if the output file were opened/created. //If opened/created correctly, we move on. if (!fout) { cout << "File could not be opened/created for output."; exit(1); } //Outputs headings to output file. fout << "Student Name" << "\t" << "Overall" << "\t" << "Homework Scores:" << "\t" << "Hwk Avg" << "\t" << "Exam Scores:" << "\t" << "Exam Avg" << "\t\n"; //Continue loop until end of file while (!fin.eof()) { //Takes student's name fin >> firstName >> lastName; while (dataType != "END") { fin >> dataType; //Determines where to place score if ( dataType == "HWK" ) { fin >> homework[a]; a++; } else if ( dataType == "EXM" ) { fin >> exam[b]; b++; } } //Determines homework total for (int e = 0; e <= 5; e++) { homeworkTotal += homework[e]; } //Determines homework average homeworkAvg = homeworkTotal / 6; //Determines exam total for (int f = 0; f <= 2; f++) { examTotal += exam[f]; } //Determines exam average examAvg = examAvg / 3; //Determines total weighted average totalAvg = (examAvg * .6) + (homeworkAvg * .4); //Outputs student's name to the output file fout << firstName << " " << lastName << "\t"; //Outputs the student's total weighted average fout << totalAvg << "\t"; //Outputs the student's homework scores for (int c = 0; c <= 5; c++) { fout << homework[c] << " "; } //Outputs the student's homework average fout << "\t\t" << homeworkAvg << "\t"; //Outputs the student's exam scores for (int d = 0; d <= 2; d++) { fout << exam[d] << " "; } //Outputs the student's exam average fout << "\t\t" << examAvg << "\n"; } fin.close(); fout.close(); return 0; }