### Good programming practice/approach?

closed account (4izT0pDG)
New here, hello.

Recently had a quiz with this problem: "Write a program that continuously requests a grade to be entered.If the grade is less than 0 or greater than 100, your program should print an appropriate message informing the user that an invalid grade has been entered; else, the grade should be added to a total. When a grade of 999 is entered, the program should exit the repetition loop and compute and display the average of the valid grades entered."

I couldn't answer it on the quiz, but I managed to solve it on my free time. It works, but my concern is that my approach doesn't seem very "right". I've been taught that using the continue statement should be avoided. Any input on how else I could have done it?

Code:
 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include using namespace std; int main() { double grade,total; int count; count=1; total=0; cout<<"enter a grade"<0) { cin>>grade; if (grade<0 || (grade>100 && grade!=999)) { cout<<"Invalid Grade"<
I don't get question well enough.From what I got I think when total grade =999 program should exit from loop.If so then,
If I enter 999,then I go to else part where grade is added then subtracted.count subtracted but later added.So result is nil.but loop ends.

In if use

 ``12345`` ``````if (grade<0 || grade>100) { cout<<"Invalid Grade"<

else part
 ``12345678910`` ``````else { total+=grade; if (total>=999) { break; } count++; }``````

and do not use count++; outside.
There is nothing wrong with what you did. I do have some suggestions though. You don't need count. Your while statement can simply check `while (grade != 999)` but then you have an issue, grade will still be added to total. You have a few options here. The one I suggest is to use the following code:
 ``123456789101112131415161718`` ``````while (grade != 999) { // Accepts a grade cin>>grade; // Makes sure grade is not valid if (grade<0 || (grade>100 && grade!=999)) // Inform user it was an invalid grade cout<<"Invalid Grade"<

That should be all you need. A little simpler and shorter as well.

Edit: Implemented count variable.
Last edited on
he need to calculate average.From where will he get number of grades entered?Its from count
Fixed.
closed account (4izT0pDG)
ahh brilliant! Thank you my friend. Your suggestion is so much cleaner. I over-thought this way too much.
I would suggest reading from cin in the while test, like this:

 ``123456789101112131415`` ``````cout << "Please enter a grade: "; while(cin >> grade && grade != 999) { if(grade < 0 || grade > 100) cout << "Invalid grade. "; else { total += grade; ++count; } cout << "Enter the next grade: "; } cin.clear(); //in case of cin failure cin.ignore(numeric_limits::max(),'\n'); //eat the remainder character(s) ``````

Why? Consider the following input:
100
78
55
84
c

The 'c' will cause cin to fail in both examples.

However, the loop posted by Volatile Pulse would cause the following issues:
1) the program would enter an infinite loop (grade stays at 84),
2) for each loop through, 84 would be added to total, and the count would increase, despite no / invalid input.

In my loop, the program is protected from an infinite loop, and data integrity is protected.

Last edited on
Yeh! I would definitely test if the input is not a digit. I have completed this project so I won't put up the completed code Yet.

 ``12345678910111213141516`` ``````sudocode: do enter grade; if grade is a digit -> if grade is within given range (101 to 998)-> calculate average; increment the number of grades entered; else if grade is out of range (0 to 100) or is a character -> output error message; while not exit code (999); if number of grades is > 0 -> output grade average; else output No grades were entered; ``````

Have a look at this http://www.cplusplus.com/reference/std/locale/isdigit/
closed account (4izT0pDG)
Why does putting cin in the while condition provide data protection?
The cin object converts to a boolean depending on the success of a read operation. If valid, the loop continues; in the case of bad input, the loop terminates, giving the rest of the code no chance to alter the other variables.
Topic archived. No new replies allowed.