473,666 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help with a loop for least and greatest number

1 New Member
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 5690
junjun
5 New Member
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 Recognized Expert Contributor
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 Recognized Expert Moderator Expert
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
4219
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, which when you click it bookmarks the site (much easier). The favicon is never saved if the site is bookmarked this way. Does anyone have any ideas how to fix this?? This is the code: <script language="JavaScript">
7
7387
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 integer is prime if it is not divisible by an odd integer less than or equal to the square root of the number.) I was given this assignment in my c++ class and I'm having trouble with it. I've looked at the professors lab solutions but I just...
3
3807
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 out. note: I only in intro C++ which is about to be finished by the next two weeks. The topic which I am on in my book is "POINTERS" Text from the book:
5
1735
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 be read when it starts up (based on where it ended). It's just 3 elements, with a short text and a number associated to it. Something like this: <element><name>something</name><number>3</number></element>
8
7735
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 statements. Is this something that is on or can be added to the 'to do' list? I could write a series of user-defined functions to do specific comparisons (such as comparing several dates and returning the greatest one) but there doesn't appear to...
59
79737
by: rami | last post by:
please everybody ,can anyone tell me how to do an infinite loop in C
6
1752
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
2010
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 occupied one. - Initially, the buffer is empty. Thus, all the slots are initialized with 0.
5
6789
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 entered. The instructor reccommends using a while loop and the loop is where I am getting lost. Any help is appreciated. Thanks... My code is below. #include <iostream> #include <iomanip> using namespace std; int main() { int numbers;...
0
8443
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7385
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6192
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.