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.
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
[edit:] And for future reference, please post code between [code] and [/code] tags, which will automatically number them and maintain indentation. Thanks
Last edited by Infarction; May 3rd, 2007 at 8:40 pm.
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
[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[i] 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!
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.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.