| rm214 (5) | |
|
Hello, I'm a first time poster and new to c++. The code that I am working on is from my programming class. I am not looking for anyone to fix it but I do need help figuring out what I am doing wrong. I think I am writing the functions the wrong way... or something with the functions. The project idea is to take information from an input file which includes the name, Id, homework/participation scores, test scores, and total class score(which equals the previous 2 together) and then write this information to an output file. There are a lot of specifics but when I compile the program I cannot get it to run. I keep getting errors, some which are in the 130 line area. If I could at least get the program to run, then I can work out the specifics later. Thanks for any advice. #include <iostream> #include <fstream> #include <cstring> using namespace std; //hw scores and the class participation score, maximum 50. float compute_hw_participation(istream& infile) { float hw, part, hwp; float total=0; cout << " Scores are: "; for(int i=1; i <= 11; i++) { infile >> hw; infile >> part; hwp = hw + part; cout << hwp << endl; } return total; } //score on the tests, maximum 50. float compute_tests(float tests, istream& infile) { infile >> tests; cout << " tests are " << tests << endl; return 0.0; } float hwp, tests; //homework and participation + tests, maximum 100. float compute_totalscore(float totalscore) { totalscore = hwp + tests; cout << " Total score is " << totalscore << endl; return 0.0; } if (totalscore >= 90) { cout << "A" << endl; } else { if (totalscore >= 80) { cout << "B" << endl; } else { if (totalscore >= 70) { cout << "C" << endl; } else { if (totalscore >= 60) { cout << "D" << endl; } else { if (totalscore < 60) { cout << "F" << endl; } } } } } void printRecord (char name[20], char Id[8], float hwp, float tests, float totalscore, float grade, ostream& outfile) { outfile << "Name: "<< name << endl << "Id: "<< Id << endl << "Homework/Participation: " << hwp << endl << "Tests: " << tests << endl << "Total course score: " << totalscore << endl << "Letter Grade: " << grade << endl <<"-------------------------------------------------"<<endl<< endl; } int main() { ofstream outfile; ifstream infile; char file_name[21], name[20], Id[20]; float hwp, tests, totalscore; int deductions; cout << "Please enter name of input file: "; cin >> file_name; infile.open(file_name); if ( !infile) { // abandons operation with error mesg cout << "Could not open input file \n"; return 0; } outfile.open("ScoreResult"); if ( !outfile) { cout << "Could not open output file \n"; return 0; } infile >> name; while(!infile.eof()) { infile >> Id; cout << name << " " << Id << endl; hwp = compute_hw_participation(infile); tests = compute_tests(tests, infile); totalscore = compute_totalscore(totalscore, infile); // grade printRecord(name, Id, hwp, tests, totalscore, outfile); infile >> name; } return 0; } | |
|
|
|
| BrentSpinor (26) | |||
|
The problem is, around line 130, you call the function "compute_totalscore" and pass two parameters into it (totalscore and infile). The function definition earlier in the code only takes one parameter. Either change the definition to accept "totalscore" and "infile", or make the call to the function with just "totalscore". All of the "if" statements between lines 40 and 80 need to be part of a function. I've named the function "GetGrade" for obvious reasons. Instead of outputting the grade, it returns a char. Also, when you make the function call to "printRecord" around line 140, you need to pass the "grade" parameter between "totalscore" and "outfile". After making those changes, it complies for me. Obviously, I don't have the input file, so who knows if it actually works.
| |||
|
Last edited on
|
|||
| rm214 (5) | |
|
Thanks Brent for your help. I will take another look tonight and let you know how it goes. Ryan | |
|
|
|