473,385 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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.
  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
3 5668
junjun
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
oler1s
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
weaknessforcats
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

8
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,...
7
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...
3
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...
5
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...
8
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...
59
by: rami | last post by:
please everybody ,can anyone tell me how to do an infinite loop in C
6
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
10
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...
5
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.