You should move most of the logic out of the main()
function and into its own function or functions. But perhaps you're not there yet in your class.
Similarly, using namespace std
is not recommended. See here. Obviously this won't matter in this program, but it's a bad habit that you may want to avoid starting.
double grade1,grade2,grade3,grade4,grade5,grade6;
Whenever I see something like this, I want to rewrite it as an array:
const int NUMBER_OF_GRADES = 6;
double grades[NUMBER_OF_GRADES];
Personally, I'd also put the grade weights into their own array, but that's up to you. You could also use a switch
statement or an if
/else if
construct to handle the grade weights.
If you use a for
loop to iterate over the entries of the array and calculate the totalGrade
incrementally in each iteration over the loop, I think that you will be able to see how to use a single grade
variable to replace the entries of the array. If your class hasn't done for
loops yet, you can get the same effect by ordering all uses of grade[0]
or grade1
before you do anything with grade[1]
or grade2
. This means that you will have to replace
totalGrade = grade1 + grade2 + grade3 + grade4 + grade5 + grade6;
with six separate statements that have the same effect.
As a matter of policy, you should probably do more error checking when you do a cin
. If you look around this site, you should be able to find several examples of people using while loops with error checking.
When I've seen people put the }
and else
on separate lines, they've always been in the same column. Like this:
if (totalGrade < 9) {
cout << "grade is: >> " << totalGrade << endl << endl;
cout << "you have reproved "<< endl;
}
else if (totalGrade < 9.5) {
// the else makes this implicitly greater than or equal to 9
cout << "grade is >> " << totalGrade << endl << endl;
cout << "you have approved -" << endl;
}
else if (totalGrade <= 10) {
// the else makes this implicitly greater than or equal to 9.5
cout << "grade is: >> " << totalGrade << endl << endl;
cout << "you have approved + " << endl;
}
I personally put them on the same line. Putting them on separate lines leads to people separating them, which can lead to confusion.
Note that I changed the order. This makes no functional difference, but it reduces the number of comparisons that you need to do. Also, I'm not sure that you have to check that totalGrade <= 10
. A simple else
might be enough for the last one.
You might want to think about why I changed <= 9.5
to < 9.5
. Hint: I had to do that to keep the effect the same after I changed the order.
cout << "hi please enter the six grades ";
<< endl << endl;
This looks horribly wrong. It may work but it is fragile. Either remove the ;
from the first line or add a cout
to the beginning of the second line.