| | |
average array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 11
Reputation:
Solved Threads: 0
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.
The program I wrote doesn't work properly, and I can't figure out what my problem is.
C++ Syntax (Toggle Plain Text)
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. }
Last edited by cscgal; May 13th, 2007 at 2:12 pm. Reason: Added code tags
•
•
•
•
>while(grade[i] < 0.0 || grade[i] > 4.0) Change to while( grade[i] < 0.0 && grade[i] > 4.0)
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 linecin>>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
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
•
•
Join Date: Apr 2007
Posts: 11
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#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:
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:
C++ Syntax (Toggle Plain Text)
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 }
Last edited by John A; May 3rd, 2007 at 10:45 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
why not better do it less complicated:
c Syntax (Toggle Plain Text)
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)); }
Last edited by Nichito; May 3rd, 2007 at 11:34 pm.
-->sometimes i wanna take my toaster in a bath<-- ![]() |
Similar Threads
- Errors in counting number of elements of the array greater and lesser than average (C)
- help...one-dimensional array averaging program (C++)
- C++ : Calculating the average value of an array (C++)
- Functions and Array help (C++)
- How do I create a program using an Array ? (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ Reading from a text file
- Next Thread: reading text into 2 dimensional array
Views: 3133 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






