By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,613 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,613 IT Pros & Developers. It's quick & easy.

How to calculate highest and lowest score using a while loop

P: 16
the in input file is like
2 80 97
5 69 79 89 99 58
7 60 70 80 90 100 0 59
[
Oct 10 '10 #1

✓ answered by Geocool

Expand|Select|Wrap|Line Numbers
  1. if ( score > highscore )
  2.     highscore = score;
  3. else if (score < lowscore )
  4.     lowscore = score;
Add the above code after "cin >> score;"

You have declared lowerscore as 1 so if you enter 20 is bigger than your already given(1) lower score so it doesn't take it.

I suggest to set first input as lowscore.
like:

Expand|Select|Wrap|Line Numbers
  1. bool isfirstinput(true);
  2. while(!cin.eof()){
  3. ....
  4. ...
  5. cin >> score;
  6. if(isfirstinput==true)
  7. {
  8. lowscore=score;
  9. isfirstinput = false; // so it won't pass to lowscore again
  10. }
  11. ...
I hope this helps... ;)

Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
I don't see you referencing highscore or lowscore inside your while loop.
Oct 11 '10 #2

P: 2
Expand|Select|Wrap|Line Numbers
  1. if ( score > highscore )
  2.     highscore = score;
  3. else if (score < lowscore )
  4.     lowscore = score;
Add the above code after "cin >> score;"

You have declared lowerscore as 1 so if you enter 20 is bigger than your already given(1) lower score so it doesn't take it.

I suggest to set first input as lowscore.
like:

Expand|Select|Wrap|Line Numbers
  1. bool isfirstinput(true);
  2. while(!cin.eof()){
  3. ....
  4. ...
  5. cin >> score;
  6. if(isfirstinput==true)
  7. {
  8. lowscore=score;
  9. isfirstinput = false; // so it won't pass to lowscore again
  10. }
  11. ...
I hope this helps... ;)
Oct 11 '10 #3

P: 16
I set lowscore equal to 100 and it works for the first one:
Oct 11 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.