944,161 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3835
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 3rd, 2007
0

average array

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. 1. #include <iostream>
  2. 2. #include <iomanip>
  3. 3. using namespace std;
  4. 4. int main()
  5. 5. {
  6. 6. int grade[6];
  7. 7. int i, total = 0;
  8. 8. double average;
  9. 9.
  10. 10. cout.setf(ios::fixed);
  11. 11. cout.setf(ios::showpoint);
  12. 12. cout.precision(2);
  13. 13.
  14. 14. for (i = 0; i <= 5; i++){
  15. 15. cout << "Enter grade " << i << " (on a scale of 0.0 to 4.0): ";
  16. 16. cin >> grade[i];
  17. 17. {
  18. 18. while(grade[i] < 0.0 || grade[i] > 4.0)
  19. 19. {
  20. 20. cout << "Enter a grade on a scale of 0.0 to 4.0: ";
  21. 21. }
  22. 22. cin >> grade[i];
  23. 23. }
  24. 24. }
  25. 25.
  26. 26. for(i = 0; i <= 5; i++)
  27. 27. total = total + grade[i];
  28. 29. average = total/6.0;
  29. 30. }
Last edited by cscgal; May 13th, 2007 at 2:12 pm. Reason: Added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathgirl is offline Offline
11 posts
since Apr 2007
May 3rd, 2007
0

Re: average array

>while(grade[i] < 0.0 || grade[i] > 4.0) Change to while( grade[i] < 0.0 && grade[i] > 4.0)
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
May 3rd, 2007
0

Re: average array

Click to Expand / Collapse  Quote originally posted by Aia ...
>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

[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.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
May 3rd, 2007
0

Re: average array

Click to Expand / Collapse  Quote originally posted by Infarction ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
May 3rd, 2007
0

Re: average array

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathgirl is offline Offline
11 posts
since Apr 2007
May 3rd, 2007
0

Re: average array

post your new code and ill help you out.
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
May 3rd, 2007
0

Re: average array

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. {
  17. while(grade[i] < 0.0 || grade[i] > 4.0)
  18. cin >> grade[i];
  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];
  28. average = total/6.0;
  29. cout << average << endl;
  30. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mathgirl is offline Offline
11 posts
since Apr 2007
May 3rd, 2007
0

Re: average array

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:
C++ Syntax (Toggle Plain Text)
  1. for (i = 0; i <= 5; i++){
  2. cout << "Enter grade " << i << " (on a scale of 0.0 to 4.0): ";
  3. // { <- don't need these braces
  4. cin >> grade[i];
  5.  
  6. while(grade[i] < 0.0 || grade[i] > 4.0)
  7. {
  8. cout << "Enter a grade on a scale of 0.0 to 4.0: ";
  9. cin >> grade[i];
  10. }
  11. // } <- don't need this
  12. }
Last edited by John A; May 3rd, 2007 at 10:45 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
May 3rd, 2007
0

Re: average array

why not better do it less complicated:
  1. for (i=0;i<=5;i++){
  2. do{
  3. cout<<"Enter grade "<<i<<" on a scale from 0.0 to 4.0: ";
  4. cin>>grade[i];
  5. }while ((grade[i]<0.00)||(grade[i]>4.00));
  6. }
Last edited by Nichito; May 3rd, 2007 at 11:34 pm.
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
May 4th, 2007
0

Re: average array

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!!!
Gel
Reputation Points: 14
Solved Threads: 3
Newbie Poster
Gel is offline Offline
19 posts
since Dec 2006

This thread is more than three months old

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.
Message:
Previous Thread in C++ Forum Timeline: C++ Reading from a text file
Next Thread in C++ Forum Timeline: reading text into 2 dimensional array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC