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. }

>while(grade < 0.0 || grade > 4.0) Change to while( grade < 0.0 && grade > 4.0)

No, don't, it would always be false. A number can't be less than 0 and greater than 4. Instead, try moving your second line cin>>grad[i]; into the while loop. Right now it just outputs the warning message and loops, since grade never changes :icon_wink:

[edit:] And for future reference, please post code between [code] and [/code] tags, which will automatically number them and maintain indentation. Thanks :)

No, don't, it would always be false. A number can't be less than 0 and greater than 4. Instead, try moving your second line cin>>grad[i]; into the while loop. Right now it just outputs the warning message and loops, since grade never changes :icon_wink:

[edit:] And for future reference, please post code between [code] and [/code] tags, which will automatically number them and maintain indentation. Thanks :)

My apologies. You are correct. I don't know what I was thinking.

I moved the cin >> grade into the while loop. I am having a problem because when I enter wrong numbers and it prompts me to enter a new number it includes it as a number in the array and prompts for less numbers.
Thanks!

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int grade[6];
    int i, total = 0;
    double average;
         
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);  
         
    for (i = 0; i <= 5; i++){
        cout << "Enter grade " << i << " (on a scale of 0.0 to 4.0): ";
        {
        while(grade[i] < 0.0 || grade[i] > 4.0)
        cin >> grade[i];
        {
   cout << "Enter a grade on a scale of 0.0 to 4.0: ";
        }
        cin >> grade[i];
        }
    }
        
    for(i = 0; i <= 5; i++)
        total = total + grade[i];
    average = total/6.0;
    cout << average << endl;
}

Did you even change anything?

Look at it this way: you need to grab the user's input before the while loop, so that you can see if it was invalid or not. Then if it's invalid, the while loop kicks in, which keeps grabbing input until it's valid. Inside the braces { and } of the while loop, you need to have that cin statement.

Here's how it would look:

for (i = 0; i <= 5; i++){
            cout << "Enter grade " << i << " (on a scale of 0.0 to 4.0): ";
//        { <- don't need these braces
            cin >> grade[i];
            
            while(grade[i] < 0.0 || grade[i] > 4.0)
            {
                cout << "Enter a grade on a scale of 0.0 to 4.0: ";
                cin >> grade[i];
            }
//        } <- don't need this
    }

why not better do it less complicated:

for (i=0;i<=5;i++){
   do{
      cout<<"Enter grade "<<i<<" on a scale from 0.0 to 4.0: ";
      cin>>grade[i];
   }while ((grade[i]<0.00)||(grade[i]>4.00));
}

do you know wat???

if you want the code to be in between 0 and 4.0,its should be
while(grade>0&&grade<4.0)

also int won't read floating point nos.,

sorry if i'm wrong!!!

try the program now and let me know!!!

if you want the code to be in between 0 and 4.0,its should be
while(grade>0&&grade<4.0)

No. If she replaces her line (in Joe's code) with yours it would continue to say ""Enter a grade on a scale of 0.0 to 4.0: " while the input would be correct..

also int won't read floating point nos.

Assuming you mean "numbers" (not the gas that makes cars go fast), you are right. Grade should be declared as double/float grade[6];

Edited 6 Years Ago by Nick Evan: n/a

>why not better do it less complicated
Usually the first method is used in validating user input because it allows for a custom error message. In this case it doesn't really matter, however.

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
     
double stdev(double a[], int n)
    {assert(n > 1);
    double sum = 0;
    for (int i = 0; i < n; i++)
        sum += a[i];
    double average = sum/n;
    sum = 0;
    double deviation;
    for (int i = 0; i < n; i++)
    {
    deviation = a[i] - average;
    sum += deviation * deviation;
    }
    return sqrt(sum/ (n - 1));
}
    
int main()
{
    double grade[6];
    int i, total = 0;
    double average;
        
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
   cout.precision(2);
    
    for (i=0;i<=5;i++)
    {
        do{
            cout<<"Enter grade "<<i<<" on a scale from 0.0 to 4.0: ";
            cin>>grade[i];
        }while((grade[i]<0.00)||(grade[i]>4.00));
    }
    for(i = 0; i <= 5; i++)
       total = total + grade[i];
    average = total/6.0;
    cout << "The average grade is " << average << endl;
     
    string finalGrade;
    if (average > 3.2 && average <= 4.0)
        finalGrade = "A"; 
    if(average > 2.4 && average <= 3.2)
        finalGrade = "B";
    if (average > 1.6 && average <= 2.4)
     finalGrade = "C";
    if (average > 0.8 && average <= 1.6)
        finalGrade = "D";
    if (average >= 0 && average <= 0.8)
        finalGrade = "F";
    cout << "The final letter grade is " << finalGrade << endl;
        
    cout << "The standard deviation is "<< stdev(x[6], 6)<<endl;
        
}

I need to write a program that will prompt the user for 6 grades, one at a time, and then read in and store in an array of six elements. Grades should be on a scale of 0 to 4.0, and the program should accept only grades within that range.
It should compute the average grade and determine the equivalent letter grade.
It should then compute the standard deviation from the six individual grades from the average.
My main problem is with the standard deviation. I posted what I have, can somebody please help ASAP?

my guess is your problem is in the call for the function: stdev(x[6],6) you haven't any variable x, so, there you should change 'x' for 'grade'...

>> }while((grade[i]<0.00)||(grade[i]>4.00)); you should rather include 0.0 and 4.0, so it should be while((grade[i]<=0.0)||(grade[i]>=4.0)); >> total = total + grade[i]; i'd rather use total+=grade[i]; which is the same thing...

This article has been dead for over six months. Start a new discussion instead.