1 error between program fail and success

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.



#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;


//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.

float compute_totalscore(float total, float testtotal)
{
bool error= false;
float totalscore;

totalscore = total + testtotal;
if ((totalscore < 0) || (totalscore > 100))
error= true;
cout << " Total course score is " << totalscore << endl;

if(error)
return -1.0;

return totalscore;
}


//Letter grade function.
char GetGrade(float totalscore)
{

char grade;
if (totalscore = -1)
{

grade = 'Z';}
else
{

if (totalscore >= 90)
{

grade = 'A';}
else
{

if (totalscore >= 80)
{

grade = 'B';}
else
{

if (totalscore >= 70)
{

grade = 'C';}
else
{

if (totalscore >= 60)
{

grade = 'D';}
else
{

if (totalscore < 60)
{

grade = 'F';}
}
}
}
}
}


cout << "Letter grade: " << grade << endl;
return grade;
}

//Print output function.
void printRecord(char name[20], char Id[20], float total, float testtotal, float totalscore, char grade, ostream& outfile)
{
outfile << name << " " << Id << " " << total << " " << testtotal << " " << totalscore << " " << grade << endl
<< "-------------------------------------------------" <<endl << endl;
}

int main()

{
ofstream outfile;
ifstream infile;


char file_name[20], name[20], Id[20];
char grade;
float total, testtotal, totalscore,;

//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.

}

return 0;
}
Hi

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...).

1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main() {
    cout << ":-)" << endl;
    return 0;
}


This article explains how they work
How to use tags
http://www.cplusplus.com/articles/z13hAqkS/

Thanks

Andy
Last edited on
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.
Last edited on
Andy - I apologize for the format, I threw it up quick and continued to work on the assignment at the time. I will remember this for next time.

Moeljbcp - Thanks for the reply and info. I later found this and corrected and it seems to work good.

Ryan
Topic archived. No new replies allowed.