# User input for student grades is too verbose

I need some help with this beginner C++ program (it's an assignment). How can I use a single variable and make the program get the six inputs I need instead of using a variable for each input?

#include <iostream>
using namespace std;

int main()

{

cout << "program to calculate grade" << endl << endl;
<< endl << endl;

cout << "grade is: >> " << totalGrade << endl << endl;
cout << "you have approved + " << endl;
}
cout << "grade is >> " << totalGrade << endl << endl;
cout << "you have approved -" << endl;
}
else if (totalGrade < 9) {
cout << "grade is: >> " << totalGrade << endl << endl;
cout << "you have reproved  "<< endl;

}

return 0;
}

• This isn't eligible for code review; it's a homework assignment. Ask yourself how you'd still make this program work if you only had a single variable at your disposal, then go from there. Hint: You can add to an existing value. Oct 15, 2014 at 19:12
• @LynnCrumbling: Just because it happens to be homework does not mean we should not review it. We should just not do the work for them (like answering the question he asks explicitly). Oct 15, 2014 at 19:55
• How about using a loop to get the six grades. Oct 15, 2014 at 19:56

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;


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.

An array is what you're looking for. With an array and a for loop, you would be able to cycle through and ask the user for each grade and save yourself a bit of typing. Check around on this site on the syntax ('cause, hey, I wouldn't want to make your assignment too easy, right?).

Along with that:

grade1 = grade1 * 0.125


is equivalent to:

grade1 *= 0.125


It's a nifty little shortcut that will multiply grade1 by 0.125 and then assign the new value back to grade1.

I'm sure someone else mentioned this, but this:

cout << "hi please enter the six grades ";
<< endl << endl;


Is going to get you into trouble. Not sure your compiler will even accept that. I'd recommend putting all of that onto the same line:

cout << "hi please enter the six grades " << endl << endl;