x.Hming some issues with this one function definition in our final project for i
ID: 3618437 • Letter: X
Question
x.Hming some issues with this one function definition in our final project for intro to C++. The function named ReadGrades does not work properly, I have tried several variations, but it will not seem to work properly. The project compiles and runs, but not correctly. The project is four files compliled as one as a project or using makefile, depending on your OS. I am using Dev C++ to compile the files as a project. We are only allowed to make changes to the file Project_11.cpp. Here are the files: P11.h (the header file) #ifndef P11_H #define P11_H #include<iostream> #include<fstream> #include<string> #include<cstdlib> using namespace std; const int MAX_STUDENTS = 30; const int NUM_EXAMS = 3; const int NUM_QUIZZES = 5; const int NUM_HW = 5; const int NUM_SCORES = NUM_EXAMS + NUM_QUIZZES + NUM_HW; const int MAX_PTS = (NUM_EXAMS-1)*100 + 200 + NUM_QUIZZES*20 + NUM_HW*20; const int NWIDTH = 10; const int FWIDTH = 5; const int FWIDTH2 = 8;struct Name { string first; string last; }; struct Student { Name name; int scores[NUM_SCORES]; float percent; int numberPoints; }; bool ReadOneScore(string, int&, int& ); void PrintHeading(ofstream&); void OpenFiles( ifstream&, ofstream& ); bool ReadGrades( ifstream& , Student[], int& ); void CalculateGrade(Student[], int); void OutputGrades(ofstream&, const Student[], int); char DetermineLetter(float); #endif
Next file: P11_provided.cpp, the two given function definitions #include <iostream> #include <fstream> #include <string> #include <iomanip> #include "P11.h" bool ReadOneScore(string line, int& index, int& score) { char ch; bool scoreRead = true; string stringScore; ch = line[index]; while (ch == ' ') { index++; ch = line[index]; } if (ch == '-') { index++; score = 0; } else { stringScore = ""; while (ch != 0 && ch != ' ') { stringScore += ch; index++; ch = line[index]; } if (stringScore == "") { scoreRead = false; } else { score = atoi(stringScore.c_str()); } }
return scoreRead; } void PrintHeading(ofstream& out) { string header; char ch; out << left; out << setw(NWIDTH) << "Last" << setw(NWIDTH) << "First"; out << setw(FWIDTH) << "T1" << setw(FWIDTH) << "T2"; out << setw(FWIDTH) << "Fin";
for (int i = 1; i <= NUM_QUIZZES; i++) { header = "Q"; header += ch; out << setw(FWIDTH) << header; } for (int i = 1; i <= NUM_HW; i++) { ch = char(48 + i); header = "HW"; header += ch; out << setw(FWIDTH) << header; } out << setw(FWIDTH) << "Pts"; out << setw(FWIDTH2) << "Percent"; out << setw(FWIDTH) << "Letter"; out << endl; }
The next file P11_main.cpp, the file containing the main function: #include <iostream> #include <fstream> #include <string> #include <iomanip> #include "P11.h" // contains function prototypes, constants and structure declaration
using namespace std; int main() { Student students[MAX_STUDENTS];
ifstream inFile; // file containing the input information ofstream outFile; // file containing the results of the program
int numStudents; // number of students in the class bool fileStatus; // output file stream status after reading data OpenFiles(inFile, outFile); fileStatus = ReadGrades(inFile, students, numStudents); if (fileStatus) { CalculateGrade(students,numStudents); PrintHeading(outFile); OutputGrades(outFile, students, numStudents); } return 0; // Successful termination }
The last file (the one we can modify): Project_11.cpp, we have to write 5 function definitions, I have written all 5, the first 4 I am fairly confident that they work properly, the 5th, which has to work to check if any of the other ones work, does not. #include <fstream> #include <string> #include <iomanip> #include "P11.h" void OpenFiles(ifstream& inFile, ofstream& outFile) { string input, output; cout << "Enter the input file name: "; //prompt for input file cin >> input; //record their answer cout << input << endl; //echo print inFile.open(input.c_str()); //open the input file while(inFile.fail() ) //if the input file stream is in the fail state { inFile.clear(); //clear the input file stream cout << "==> Error. Invalid input file name. File " << input << endl; //error message cout << "==> could not be opened for input. Try again... " << endl; //error message cout << "Enter the input file name: "; //reprompt cin >> input; //record new input file name cout << input << endl; //echo print inFile.open(input.c_str()); //Open the new input file, repeat if necessary } cout << "Enter the output file name: "; //prompt for output file cin >> output; //record output file cout << output << endl; //echo print outFile.open(output.c_str()); //open the output file stream while(outFile.fail() ) //if the output file stream is in the fail state { outFile.clear(); //clear the output file stream cout << "==> Error. Invalid output file name. File " << The function named ReadGrades does not work properly, I have tried several variations, but it will not seem to work properly. The project compiles and runs, but not correctly. The project is four files compliled as one as a project or using makefile, depending on your OS. I am using Dev C++ to compile the files as a project. We are only allowed to make changes to the file Project_11.cpp. Here are the files: P11.h (the header file) #ifndef P11_H #define P11_H #include<iostream> #include<fstream> #include<string> #include<cstdlib> using namespace std; const int MAX_STUDENTS = 30; const int NUM_EXAMS = 3; const int NUM_QUIZZES = 5; const int NUM_HW = 5; const int NUM_SCORES = NUM_EXAMS + NUM_QUIZZES + NUM_HW; const int MAX_PTS = (NUM_EXAMS-1)*100 + 200 + NUM_QUIZZES*20 + NUM_HW*20; const int NWIDTH = 10; const int FWIDTH = 5; const int FWIDTH2 = 8;
struct Name { string first; string last; }; struct Student { Name name; int scores[NUM_SCORES]; float percent; int numberPoints; }; bool ReadOneScore(string, int&, int& ); void PrintHeading(ofstream&); void OpenFiles( ifstream&, ofstream& ); bool ReadGrades( ifstream& , Student[], int& ); void CalculateGrade(Student[], int); void OutputGrades(ofstream&, const Student[], int); char DetermineLetter(float); #endif
Next file: P11_provided.cpp, the two given function definitions #include <iostream> #include <fstream> #include <string> #include <iomanip> #include "P11.h" bool ReadOneScore(string line, int& index, int& score) { char ch; bool scoreRead = true; string stringScore; ch = line[index]; while (ch == ' ') { index++; ch = line[index]; } if (ch == '-') { index++; score = 0; } else { stringScore = ""; while (ch != 0 && ch != ' ') { stringScore += ch; index++; ch = line[index]; } if (stringScore == "") { scoreRead = false; } else { score = atoi(stringScore.c_str()); } }
return scoreRead; } void PrintHeading(ofstream& out) { string header; char ch; out << left; out << setw(NWIDTH) << "Last" << setw(NWIDTH) << "First"; out << setw(FWIDTH) << "T1" << setw(FWIDTH) << "T2"; out << setw(FWIDTH) << "Fin";
for (int i = 1; i <= NUM_QUIZZES; i++) { header = "Q"; header += ch; out << setw(FWIDTH) << header; } for (int i = 1; i <= NUM_HW; i++) { ch = char(48 + i); header = "HW"; header += ch; out << setw(FWIDTH) << header; } out << setw(FWIDTH) << "Pts"; out << setw(FWIDTH2) << "Percent"; out << setw(FWIDTH) << "Letter"; out << endl; }
The next file P11_main.cpp, the file containing the main function: #include <iostream> #include <fstream> #include <string> #include <iomanip> #include "P11.h" // contains function prototypes, constants and structure declaration
using namespace std; int main() { Student students[MAX_STUDENTS];
ifstream inFile; // file containing the input information ofstream outFile; // file containing the results of the program
int numStudents; // number of students in the class bool fileStatus; // output file stream status after reading data OpenFiles(inFile, outFile); fileStatus = ReadGrades(inFile, students, numStudents); if (fileStatus) { CalculateGrade(students,numStudents); PrintHeading(outFile); OutputGrades(outFile, students, numStudents); } return 0; // Successful termination }
The last file (the one we can modify): Project_11.cpp, we have to write 5 function definitions, I have written all 5, the first 4 I am fairly confident that they work properly, the 5th, which has to work to check if any of the other ones work, does not. #include <fstream> #include <string> #include <iomanip> #include "P11.h" void OpenFiles(ifstream& inFile, ofstream& outFile) { string input, output; cout << "Enter the input file name: "; //prompt for input file cin >> input; //record their answer cout << input << endl; //echo print inFile.open(input.c_str()); //open the input file while(inFile.fail() ) //if the input file stream is in the fail state { inFile.clear(); //clear the input file stream cout << "==> Error. Invalid input file name. File " << input << endl; //error message cout << "==> could not be opened for input. Try again... " << endl; //error message cout << "Enter the input file name: "; //reprompt cin >> input; //record new input file name cout << input << endl; //echo print inFile.open(input.c_str()); //Open the new input file, repeat if necessary } cout << "Enter the output file name: "; //prompt for output file cin >> output; //record output file cout << output << endl; //echo print outFile.open(output.c_str()); //open the output file stream while(outFile.fail() ) //if the output file stream is in the fail state { outFile.clear(); //clear the output file stream cout << "==> Error. Invalid output file name. File " << #ifndef P11_H #define P11_H #include<iostream> #include<fstream> #include<string> #include<cstdlib> using namespace std; const int MAX_STUDENTS = 30; const int NUM_EXAMS = 3; const int NUM_QUIZZES = 5; const int NUM_HW = 5; const int NUM_SCORES = NUM_EXAMS + NUM_QUIZZES + NUM_HW; const int MAX_PTS = (NUM_EXAMS-1)*100 + 200 + NUM_QUIZZES*20 + NUM_HW*20; const int NWIDTH = 10; const int FWIDTH = 5; const int FWIDTH2 = 8;
struct Name { string first; string last; }; struct Student { Name name; int scores[NUM_SCORES]; float percent; int numberPoints; }; bool ReadOneScore(string, int&, int& ); void PrintHeading(ofstream&); void OpenFiles( ifstream&, ofstream& ); bool ReadGrades( ifstream& , Student[], int& ); void CalculateGrade(Student[], int); void OutputGrades(ofstream&, const Student[], int); char DetermineLetter(float); #endif
Next file: P11_provided.cpp, the two given function definitions #include <iostream> #include <fstream> #include <string> #include <iomanip> #include "P11.h" bool ReadOneScore(string line, int& index, int& score) { char ch; bool scoreRead = true; string stringScore; ch = line[index]; while (ch == ' ') { index++; ch = line[index]; } if (ch == '-') { index++; score = 0; } else { stringScore = ""; while (ch != 0 && ch != ' ') { stringScore += ch; index++; ch = line[index]; } if (stringScore == "") { scoreRead = false; } else { score = atoi(stringScore.c_str()); } }
return scoreRead; } void PrintHeading(ofstream& out) { string header; char ch; out << left; out << setw(NWIDTH) << "Last" << setw(NWIDTH) << "First"; out << setw(FWIDTH) << "T1" << setw(FWIDTH) << "T2"; out << setw(FWIDTH) << "Fin";
for (int i = 1; i <= NUM_QUIZZES; i++) { header = "Q"; header += ch; out << setw(FWIDTH) << header; } for (int i = 1; i <= NUM_HW; i++) { ch = char(48 + i); header = "HW"; header += ch; out << setw(FWIDTH) << header; } out << setw(FWIDTH) << "Pts"; out << setw(FWIDTH2) << "Percent"; out << setw(FWIDTH) << "Letter"; out << endl; }
The next file P11_main.cpp, the file containing the main function: #include <iostream> #include <fstream> #include <string> #include <iomanip> #include "P11.h" // contains function prototypes, constants and structure declaration
using namespace std; int main() { Student students[MAX_STUDENTS];
ifstream inFile; // file containing the input information ofstream outFile; // file containing the results of the program
int numStudents; // number of students in the class bool fileStatus; // output file stream status after reading data OpenFiles(inFile, outFile); fileStatus = ReadGrades(inFile, students, numStudents); if (fileStatus) { CalculateGrade(students,numStudents); PrintHeading(outFile); OutputGrades(outFile, students, numStudents); } return 0; // Successful termination }
The last file (the one we can modify): Project_11.cpp, we have to write 5 function definitions, I have written all 5, the first 4 I am fairly confident that they work properly, the 5th, which has to work to check if any of the other ones work, does not. #include <fstream> #include <string> #include <iomanip> #include "P11.h" void OpenFiles(ifstream& inFile, ofstream& outFile) { string input, output; cout << "Enter the input file name: "; //prompt for input file cin >> input; //record their answer cout << input << endl; //echo print inFile.open(input.c_str()); //open the input file while(inFile.fail() ) //if the input file stream is in the fail state { inFile.clear(); //clear the input file stream cout << "==> Error. Invalid input file name. File " << input << endl; //error message cout << "==> could not be opened for input. Try again... " << endl; //error message cout << "Enter the input file name: "; //reprompt cin >> input; //record new input file name cout << input << endl; //echo print inFile.open(input.c_str()); //Open the new input file, repeat if necessary } cout << "Enter the output file name: "; //prompt for output file cin >> output; //record output file cout << output << endl; //echo print outFile.open(output.c_str()); //open the output file stream while(outFile.fail() ) //if the output file stream is in the fail state { outFile.clear(); //clear the output file stream cout << "==> Error. Invalid output file name. File " << #include <iostream> #include <fstream> #include <string> #include <iomanip> #include "P11.h" bool ReadOneScore(string line, int& index, int& score) { char ch; bool scoreRead = true; string stringScore; ch = line[index]; while (ch == ' ') { index++; ch = line[index]; } if (ch == '-') { index++; score = 0; } else { stringScore = ""; while (ch != 0 && ch != ' ') { stringScore += ch; index++; ch = line[index]; } if (stringScore == "") { scoreRead = false; } else { score = atoi(stringScore.c_str()); } }
return scoreRead; } void PrintHeading(ofstream& out) { string header; char ch; out << left; out << setw(NWIDTH) << "Last" << setw(NWIDTH) << "First"; out << setw(FWIDTH) << "T1" << setw(FWIDTH) << "T2"; out << setw(FWIDTH) << "Fin";
for (int i = 1; i <= NUM_QUIZZES; i++) { header = "Q"; header += ch; out << setw(FWIDTH) << header; } for (int i = 1; i <= NUM_HW; i++) { ch = char(48 + i); header = "HW"; header += ch; out << setw(FWIDTH) << header; } out << setw(FWIDTH) << "Pts"; out << setw(FWIDTH2) << "Percent"; out << setw(FWIDTH) << "Letter"; out << endl; }
The next file P11_main.cpp, the file containing the main function: #include <iostream> #include <fstream> #include <string> #include <iomanip> #include "P11.h" // contains function prototypes, constants and structure declaration
using namespace std; int main() { Student students[MAX_STUDENTS];
ifstream inFile; // file containing the input information ofstream outFile; // file containing the results of the program
int numStudents; // number of students in the class bool fileStatus; // output file stream status after reading data OpenFiles(inFile, outFile); fileStatus = ReadGrades(inFile, students, numStudents); if (fileStatus) { CalculateGrade(students,numStudents); PrintHeading(outFile); OutputGrades(outFile, students, numStudents); } return 0; // Successful termination }
The last file (the one we can modify): Project_11.cpp, we have to write 5 function definitions, I have written all 5, the first 4 I am fairly confident that they work properly, the 5th, which has to work to check if any of the other ones work, does not. #include <fstream> #include <string> #include <iomanip> #include "P11.h" void OpenFiles(ifstream& inFile, ofstream& outFile) { string input, output; cout << "Enter the input file name: "; //prompt for input file cin >> input; //record their answer cout << input << endl; //echo print inFile.open(input.c_str()); //open the input file while(inFile.fail() ) //if the input file stream is in the fail state { inFile.clear(); //clear the input file stream cout << "==> Error. Invalid input file name. File " << input << endl; //error message cout << "==> could not be opened for input. Try again... " << endl; //error message cout << "Enter the input file name: "; //reprompt cin >> input; //record new input file name cout << input << endl; //echo print inFile.open(input.c_str()); //Open the new input file, repeat if necessary } cout << "Enter the output file name: "; //prompt for output file cin >> output; //record output file cout << output << endl; //echo print outFile.open(output.c_str()); //open the output file stream while(outFile.fail() ) //if the output file stream is in the fail state { outFile.clear(); //clear the output file stream cout << "==> Error. Invalid output file name. File " << #include <iostream> #include <fstream> #include <string> #include <iomanip> #include "P11.h" // contains function prototypes, constants and structure declaration
using namespace std; int main() { Student students[MAX_STUDENTS];
ifstream inFile; // file containing the input information ofstream outFile; // file containing the results of the program
int numStudents; // number of students in the class bool fileStatus; // output file stream status after reading data OpenFiles(inFile, outFile); fileStatus = ReadGrades(inFile, students, numStudents); if (fileStatus) { CalculateGrade(students,numStudents); PrintHeading(outFile); OutputGrades(outFile, students, numStudents); } return 0; // Successful termination }
The last file (the one we can modify): Project_11.cpp, we have to write 5 function definitions, I have written all 5, the first 4 I am fairly confident that they work properly, the 5th, which has to work to check if any of the other ones work, does not. #include <fstream> #include <string> #include <iomanip> #include "P11.h" void OpenFiles(ifstream& inFile, ofstream& outFile) { string input, output; cout << "Enter the input file name: "; //prompt for input file cin >> input; //record their answer cout << input << endl; //echo print inFile.open(input.c_str()); //open the input file while(inFile.fail() ) //if the input file stream is in the fail state { inFile.clear(); //clear the input file stream cout << "==> Error. Invalid input file name. File " << input << endl; //error message cout << "==> could not be opened for input. Try again... " << endl; //error message cout << "Enter the input file name: "; //reprompt cin >> input; //record new input file name cout << input << endl; //echo print inFile.open(input.c_str()); //Open the new input file, repeat if necessary } cout << "Enter the output file name: "; //prompt for output file cin >> output; //record output file cout << output << endl; //echo print outFile.open(output.c_str()); //open the output file stream while(outFile.fail() ) //if the output file stream is in the fail state { outFile.clear(); //clear the output file stream cout << "==> Error. Invalid output file name. File " << #include <fstream> #include <string> #include <iomanip> #include "P11.h" void OpenFiles(ifstream& inFile, ofstream& outFile) { string input, output; cout << "Enter the input file name: "; //prompt for input file cin >> input; //record their answer cout << input << endl; //echo print inFile.open(input.c_str()); //open the input file while(inFile.fail() ) //if the input file stream is in the fail state { inFile.clear(); //clear the input file stream cout << "==> Error. Invalid input file name. File " << input << endl; //error message cout << "==> could not be opened for input. Try again... " << endl; //error message cout << "Enter the input file name: "; //reprompt cin >> input; //record new input file name cout << input << endl; //echo print inFile.open(input.c_str()); //Open the new input file, repeat if necessary } cout << "Enter the output file name: "; //prompt for output file cin >> output; //record output file cout << output << endl; //echo print outFile.open(output.c_str()); //open the output file stream while(outFile.fail() ) //if the output file stream is in the fail state { outFile.clear(); //clear the output file stream cout << "==> Error. Invalid output file name. File " <<
Explanation / Answer
x.Hlor="red">please rate - thanks I don't know how to work with multiple files, so you'll have to reseperate the fires. I think, hope, I marked all the lines of code I changed with //////////////////////////// the only thing that may be a problem is the way the output looks, columns aren't lined up, but then either is your samples #include #include #include #include using namespace std; const int MAX_STUDENTS = 30; const int NUM_EXAMS = 3; const int NUM_QUIZZES = 5; const int NUM_HW = 5; const int NUM_SCORES = NUM_EXAMS + NUM_QUIZZES + NUM_HW; const int MAX_PTS = (NUM_EXAMS-1)*100 + 200 + NUM_QUIZZES*20 + NUM_HW*20; const int NWIDTH = 10; const int FWIDTH = 5; const int FWIDTH2 = 8; struct Name { string first; string last; }; struct Student { Name name; int scores[NUM_SCORES]; float percent; int numberPoints; }; bool ReadOneScore(string, int&, int& ); void PrintHeading(ofstream&); void OpenFiles( ifstream&, ofstream& ); bool ReadGrades( ifstream& , Student[], int& ); void CalculateGrade(Student[], int); void OutputGrades(ofstream&, const Student[], int); char DetermineLetter(float); bool ReadOneScore(string line, int& index, int& score) { char ch; bool scoreRead = true; string stringScore; ch = line[index]; while (ch == ' ') { index++; ch = line[index]; } if (ch == '-') { index++; score = 0; } else { stringScore = ""; while (ch != 0 && ch != ' ') { stringScore += ch; index++; ch = line[index]; } if (stringScore == "") { scoreRead = false; } else { score = atoi(stringScore.c_str()); } } return scoreRead; } void PrintHeading(ofstream& out) { string header; char ch; outRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.