Hi, I posted about a week ago and have since nearly finished my code but it keeps printing the grade 'Z'. Why! Been trying to figure out forever and I can't seem to beet it, thanks.
//hw scores and the class participation score, maximum 50.
float compute_hw_participation(istream& infile)
{
bool error= false;
int hw, part, min = 100; float total = 0;
//cout << " Homework scores are: ";
for(int i=1; i <= 10; i++) //Loop that runs the 10 homework scores.
{
infile >> hw; //Takes first homework score in.
if ((hw < 0) || (hw > 100))
error= true;
if(hw < min) //Drops lowest score.
min = hw; //Assigns min to hw value.
total = total + hw; //Adds hw value to total which is 0.
}
//cout << hw << " "; //Prints the first 10 hw values.
infile >> part; //Takes in #11 score being participation score.
if ((part < 0) || (part > 100))
error= true;
total = total + part; //Adds part. to total score (the score should now be over 1100 if all scores).
total = total - min; //Subtracts the ,min from the total so the max score could only be 1000.
total = total * .05; //Scales the total for a max of 50.
cout << "Homework scores are: " << total << endl;
if (error)
return -1.0;
return total;
}
//score on the tests, maximum 50.
float compute_tests(istream& infile)
{
bool error= false; float tests; int final; float testtotal = 0; float finaltotal = 0;
for(int i=1; i <= 3; i++) //Loop that runs the first 3 test scores.
{
infile >> tests; //Takes first test score in.
if ((tests < 0) || (tests > 100))
error= true;
testtotal = testtotal + tests; //Adds total test score to tests.
}
testtotal = testtotal * .1;
infile >> final;
if ((final < 0) || (final > 100))
error= true;
finaltotal = finaltotal + final; //Takes the final test score and compute and adds input score to 0.
finaltotal = finaltotal * .2; //Scales final score to twice the weight of the midterm scores.
testtotal = testtotal + finaltotal; //Adds the midterm test and the twice weighted final test for a max of 50.
cout << " Test scores are " << testtotal << endl;
if(error)
return -1.0;
return testtotal;
}
//homework and participation + tests, maximum 100.
//Input file.
cout << "Please enter name of input file: ";
cin >> file_name;
infile.open(file_name);
if ( !infile)
{
//Abandons operation with error message.
cout << "Could not open input file \n";
return 0;
}
//Output file.
cout << "Please enter name of output file: ";
cin >> file_name;
outfile.open(file_name);
if ( !outfile)
{
cout << "Could not open output file \n";
return 0;
}
infile >> name;
while(!infile.eof())//End of file FLAG.
{
infile >> Id, total, testtotal;
cout << name << " " << Id << endl;
total = compute_hw_participation(infile); //Infile when taking in a value, variable when there is no infile in function.
testtotal = compute_tests(infile);
totalscore = compute_totalscore(total, testtotal);
grade = GetGrade(totalscore);
printRecord(name, Id, total, testtotal, totalscore, grade, outfile);
infile >> name;//Triggers flag if at end of file.
Your code is quite hard to follow as it's not clearly formatted. Not such an issue if it was a small fragment, but that not's the case here. Please go back and edit your initial post to add code tags (sorting out the spacing and indenting that got lost when you cut and pasted...).
Because of this line: if (totalscore = -1). You're assigning totalscore to -1 which will always succeed, and evaluate as true. Change it to if (totalscore == -1) to compare instead, which is what you want. Simple mistake.
Remember, one equals sign '=' is assignment, two equals signs '==' are comparison.