>while(grade[i] < 0.0 || grade[i] > 4.0) Change to while( grade[i] < 0.0 && grade[i] > 4.0)
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
>while(grade[i] < 0.0 || grade[i] > 4.0) Change to while( grade[i] < 0.0 && grade[i] > 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[i] 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 :)
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
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[i] 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.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
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
}
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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));
}
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
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];
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
>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.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
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...
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57