472,955 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,955 software developers and data experts.

need help with a loop for least and greatest number

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.
  7. #include <iostream?
  8. using namespace std;
  10. int main()
  12. {
  13.       //enter up to 5 integers
  14.     int least, int greatest;        //determine least and greatest
  17. double num1 = 0;
  18. double num2 = 0;
  19. double num3 = 0;
  20. double num4 = 0;
  21. double num5 = 0;
  24.         cout << " Please enter up to 5 integers:\n ";
  25.         cin >> num1,  num2,  num3, num4, num5;
  27.     cout << " The integer with the least value is: \n";
  28.     cin >>  least;
  30.         cout << " Of the integers you choose the one with the greatest value is: \n"
  31.         cin >>  greatest;
  33. if ( num1 < num2 )
  34.     least = num1;
  36.     else if ( num2 < num3)
  37.         least = num2;
  39.     else if ( num3 < num4)
  40.         least = num3;
  42.     else if ( num4 < num5)
  43.         least = num3;
  45.     else if ( num 5 < num1 )
  46.             least = num5
  48. if ( num1 > num2 )
  49.     greatest = num1;
  51.     else if ( num2 > num3)
  52.         greatest = num2;
  54.     else if ( num3 > num4)
  55.         greatest = num3;
  57.     else if ( num4 > num5)
  58.         greatest = num3;
  60.     else if ( num 5 > num1 )
  61.             greatest = num5
  64. if( user input == -99 ) break;
  66. return 0;
  68. }
Sep 26 '07 #1
3 5645
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!!!

Sep 26 '07 #2
671 Expert 512MB
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
9,208 Expert Mod 8TB
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

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

Similar topics

by: Johnny Knoxville | last post by:
I've added a favicon to my site (http://lazyape.filetap.com/) which works fine if you add the site to favourites the normal way, but I have some JavaScript code on a couple of pages with a link,...
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd...
by: ash | last post by:
Hey I am new, but I don't have time to intruduce myself yet. I am intro to C++ and this is a programme I have to write. all the direction are here, It will be very nice of someone to figure this...
by: Amadej | last post by:
Hello everyone, I need some advice on which class to use when working with an XML file in a specific way. Basically I am writing a program, that needs to save it's state into a file, so it can...
by: Mike Nolan | last post by:
As far as I can tell, Postgres has no equivalent to greatest and least functions in Oracle. Yes, you can do the same thing with a case statement, but at the expense of writing MUCH longer SQL...
by: rami | last post by:
please everybody ,can anyone tell me how to do an infinite loop in C
by: daveyand | last post by:
Hey Guys, I've stumped. I created a function that does various things to select boxes. Namely Get All selected indexes, populate array with these values
by: preethamkumark | last post by:
- The program first creates a shared memory buffer containing an array of 20 integers. - Each slot of the buffer can have either 0 or 1, where 0 represents an empty slot, and 1 represents an...
by: mturner64 | last post by:
Good day. I am working on a program that will allow a user to enter an undefined series of numbers and enter -99 when finished. The program should output the Greatest and Least of the series...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.