average array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 11
Reputation: mathgirl is an unknown quantity at this point 
Solved Threads: 0
mathgirl mathgirl is offline Offline
Newbie Poster

average array

 
0
  #1
May 3rd, 2007
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. 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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: average array

 
0
  #2
May 3rd, 2007
>while(grade[i] < 0.0 || grade[i] > 4.0) Change to while( grade[i] < 0.0 && grade[i] > 4.0)
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: average array

 
0
  #3
May 3rd, 2007
Originally Posted by Aia View Post
>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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: average array

 
0
  #4
May 3rd, 2007
Originally Posted by Infarction View Post
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.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 11
Reputation: mathgirl is an unknown quantity at this point 
Solved Threads: 0
mathgirl mathgirl is offline Offline
Newbie Poster

Re: average array

 
0
  #5
May 3rd, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: average array

 
0
  #6
May 3rd, 2007
post your new code and ill help you out.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 11
Reputation: mathgirl is an unknown quantity at this point 
Solved Threads: 0
mathgirl mathgirl is offline Offline
Newbie Poster

Re: average array

 
0
  #7
May 3rd, 2007
  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: average array

 
0
  #8
May 3rd, 2007
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:
  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.
"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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: average array

 
0
  #9
May 3rd, 2007
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.
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 8
Reputation: Gel is an unknown quantity at this point 
Solved Threads: 2
Gel's Avatar
Gel Gel is offline Offline
Newbie Poster

Re: average array

 
0
  #10
May 4th, 2007
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!!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3133 | Replies: 13
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC