#include <iostream>
usingnamespace std;
int main()
{
int studentcount, students, score;
float totalscores, average;
string studentname;
cout<<"How many students have taken the exam"<<endl;
cin>> students;
totalscores=0.0;
studentcount=0;
while(students>studentcount)
{
cout<<"what is the student name: "<<endl;
cin>>studentname;
cout<<"Please enter scores: ";
cin>> score;
cout<<studentname<< " got a score of "<<score<<endl;
if(score<100 && score>89)
{
cout<<studentname<<"'s letter grade is an A."<<endl;;
}
elseif(score<90 && score>79)
{
cout<<studentname<<"'s letter grade is a B."<<endl;
}
elseif(score<80 && score>69)
{
cout<<studentname<< "'s letter grade is a C."<<endl;
}
else
{
cout<<studentname<<" failed."<<endl;
}
studentcount=studentcount+1;
totalscores=totalscores+score;
}
average=totalscores/students;
cout<<"The class average was :"<<average<<endl;
system("pause");
return 0;
}
Add two variables of int leastscore and highscore;
where you intialized the others intialize thix two as follows
highscore=0;
least score=100; /// if no student can get above 100 or below 0
we intialixed highscore=0 so that we can account for any number greater than 0
, here we will add a statement that checks and see if score is greater than 0 it sets highscore equal to score else highscore remains equal to highscore;
likewise happens to the variable leastscore if score is less than least score leastscore is equated to score;
while(students>studentcount)
{
cout<<"what is the student name: "<<endl;
cin>>studentname;
cout<<"Please enter scores: ";
cin>> score;
cout<<studentname<< " got a score of "<<score<<endl;
if(score>=0&&score<=100)
{
if (score<leastscore)
{
leastscore=score;
}
else if(score>highscore)
{
highscore=score;
}
else if(score<100 && score>89)
{
cout<<studentname<<"'s letter grade is an A."<<endl;;
}
else if(score<90 && score>79)
{
cout<<studentname<<"'s letter grade is a B."<<endl;
}
else if(score<80 && score>69)
{
cout<<studentname<< "'s letter grade is a C."<<endl;
}
else
{
cout<<studentname<<" failed."<<endl;
}
}
else
{
cout<<"score can't be more than 100 or less than 0 , repeate"<<endl;
studentcount=studentcount-1; ///this line will compensate for the invalid score entered
score=0; /// to make sure that all the student correct value were
/// keyed in and score dont make the total seem greater
/// than the correct values;
}
#include <iostream>
usingnamespace std;
int main()
{
int studentcount, students, score;
float totalscores, average, leastscore=100, highscore=0;
string studentname;
cout<<"How many students have taken the exam"<<endl;
cin>> students;
totalscores=0.0;
studentcount=0;
while(students>studentcount)
{
cout<<"what is the student name: "<<endl;
cin>>studentname;
cout<<"Please enter scores: ";
cin>> score;
cout<<studentname<< " got a score of "<<score<<endl;
if(score>=0&&score<=100)
{
if (score<leastscore)
{
leastscore=score;
}
elseif(score>highscore)
{
highscore=score;
}
}
if(score<100 && score>89)
{
cout<<studentname<<"'s letter grade is an A."<<endl;;
}
elseif(score<90 && score>79)
{
cout<<studentname<<"'s letter grade is a B."<<endl;
}
elseif(score<80 && score>69)
{
cout<<studentname<< "'s letter grade is a C."<<endl;
}
else
{
cout<<studentname<<" failed."<<endl;
}
studentcount=studentcount+1;
totalscores=totalscores+score;
}
average=totalscores/students;
cout<<"The class average was :"<<average<<endl;
cout<<"The highscore was :"<<highscore<<endl;
cout<<"The low score was: "<<leastscore<<endl;
system("pause");
return 0;
}
The else on line 32 is an error. It is quite possible that both high and low have to be adjusted by the same score. The first given score, if it is neither 0 or 100, should do so.
Why are only the high and low confined within the line 25 test? Surely, the letter grading should also enjoy about the knowledge that score is within [0..100]. More importantly, what does happen to the average in a class of 10 students, if John Doe has a "failed" score of 9000? andy1992 did try to do it differently, but not neatly and you did not use that.
You could do:
1 2 3 4 5 6 7 8 9
if ( score < 0 || 100 < score )
{
cout << "Invalid score. Discarded.\n";
}
else
{
// test min, max, letter grade
// add count and total
}
However, this is within a loop and therefore, we have one more keyword:
1 2 3 4 5 6 7 8
if ( score < 0 || 100 < score )
{
cout << "Invalid score. Discarded.\n";
continue;
}
// test min, max, letter grade
// add count and total
PS. What are the valid scores? In your score the grade for 100 is "failed"? Is that correct?