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

need help with a loop for least and greatest number

P: 1
I dont know where I am going wrong so I dont know which part to post, this is what I have, can anyone help me figure out where I am going wrong? THanks for any and all help.

Expand|Select|Wrap|Line Numbers
  1. // into to c++
  2. // This program should use a loop that lets the user enter a series of integers.
  3. // The user should enter -99 to signal the end of the series.
  4. // After all the numbers have been entered, 
  5. // the program should display the largest and smallest numbers entered.
  6.  
  7. #include <iostream?
  8. using namespace std;
  9.  
  10. int main()
  11.  
  12. {
  13.       //enter up to 5 integers
  14.     int least, int greatest;        //determine least and greatest
  15.  
  16.  
  17. double num1 = 0;
  18. double num2 = 0;
  19. double num3 = 0;
  20. double num4 = 0;
  21. double num5 = 0;
  22.  
  23.  
  24.         cout << " Please enter up to 5 integers:\n ";
  25.         cin >> num1,  num2,  num3, num4, num5;
  26.  
  27.     cout << " The integer with the least value is: \n";
  28.     cin >>  least;
  29.  
  30.         cout << " Of the integers you choose the one with the greatest value is: \n"
  31.         cin >>  greatest;
  32.  
  33. if ( num1 < num2 )
  34.     least = num1;
  35.  
  36.     else if ( num2 < num3)
  37.         least = num2;
  38.  
  39.     else if ( num3 < num4)
  40.         least = num3;
  41.  
  42.     else if ( num4 < num5)
  43.         least = num3;
  44.  
  45.     else if ( num 5 < num1 )
  46.             least = num5
  47.  
  48. if ( num1 > num2 )
  49.     greatest = num1;
  50.  
  51.     else if ( num2 > num3)
  52.         greatest = num2;
  53.  
  54.     else if ( num3 > num4)
  55.         greatest = num3;
  56.  
  57.     else if ( num4 > num5)
  58.         greatest = num3;
  59.  
  60.     else if ( num 5 > num1 )
  61.             greatest = num5
  62.  
  63.  
  64. if( user input == -99 ) break;
  65.  
  66. return 0;
  67.  
  68. }
  69.  
Sep 26 '07 #1
Share this Question
Share on Google+
3 Replies


P: 5
Check out if elseif

suppose you have 10, 12, 1, 2, 3 as your numbers input
cause 10<12, so that num1<num2 is satisfied, and then num1(10) is your least

so that you won't go any further of the ifelse condition. but this is not right.

You might want to do than pairwise, like say

num1 compare with num2 , you get a min and max
num3 compare with num4 , you get a min and max

and then you compare the two mins together with num5 to determin the min
compare the two max together with num5 to determin the max as well,

Remember if from math perpective, in order to compare 5 numbers , you need at least 10 comparisions, apparently you don't, that is why your code doesn't work.

Hope it helps!!!

JJ
Sep 26 '07 #2

Expert 100+
P: 671
Sounds like our OP needs to open up his C++ book and do some reading.

Delete your entire program. Now start from scratch. Do just step one. That is, write a loop that allows the user to enter a series of integers. A loop.

If you don't know how to write a loop or an array, that's a sign you don't know the material at all. And we can't help if your issue is not knowing the material.
Sep 26 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Also, you cannot use operators line <, >, ==, !=, etc with floating point.

The rounding of floating point causes these operators to report something is true when the numbers may not be in that exact relation but only close in value.

Try a Google on Floating Point Arithmetic and you will see what I mean.
Sep 26 '07 #4

Post your reply

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