I am trying to write a program that will prompt the user for six grades to be entered (one at a time), read in each grade entered by the user, and store them in an array of six elements. Grades should be on a 0 to 4.0 (inclusive) scale, and the program should accept only grades within that range. I.e. whenever the user enters a grade less than 0 or greater than 4.0, the program will prompt the user to re-enter that grade within the correct range (and do so repeatedly, as long as he/she continues to enter an invalid grade).
The program I wrote doesn't work properly, and I can't figure out what my problem is.
1. #include <iostream>
2. #include <iomanip>
3. using namespace std;
4. int main()
5. {
6. int grade[6];
7. int i, total = 0;
8. double average;
9.
10. cout.setf(ios::fixed);
11. cout.setf(ios::showpoint);
12. cout.precision(2);
13.
14. for (i = 0; i <= 5; i++){
15. cout << "Enter grade " << i << " (on a scale of 0.0 to 4.0): ";
16. cin >> grade[i];
17. {
18. while(grade[i] < 0.0 || grade[i] > 4.0)
19. {
20. cout << "Enter a grade on a scale of 0.0 to 4.0: ";
21. }
22. cin >> grade[i];
23. }
24. }
25.
26. for(i = 0; i <= 5; i++)
27. total = total + grade[i];
29. average = total/6.0;
30. }