473,387 Members | 1,575 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,387 software developers and data experts.

How Can I Compare More Than Two Values in an 'if' Function?

Expand|Select|Wrap|Line Numbers
  1. // This program will read three (3) integers than display its numerical sequence.
  2.  
  3. #include "std_lib_facilities.h"
  4.  
  5. int main()
  6. {
  7.     cout << "This program will read three (3) integers of your choice and than\n";
  8.     cout << "display them in its numerical sequence.\n";
  9.     cout << "Please enter three (3) integers:\n";
  10.     int val1;
  11.     int val2;
  12.     int val3;
  13.     while (cin >> val1 >> val2 >> val3){
  14.         if (val1 <= val2 <= val3)
  15.             cout << val1<<", "<<val2<<", "<<val3<<'\n';
  16.         else if (val1 <= val3 <= val2)
  17.                 cout << val1<<", "<<val3<<", "<<val2<<'\n';
  18.             else if (val2 <= val1 <= val3)
  19.                     cout << val2<<", "<<val1<<", "<<val3<<'\n';
  20.                 else if (val2 <= val3 <= val1)
  21.                         cout << val2<<", "<<val3<<", "<<val1<<'\n';
  22.                     else if (val3 <= val1 <= val2)
  23.                             cout << val3<<", "<<val1<<", "<<val2<<'\n';
  24.                         else if (val3 <= val2 <= val1)
  25.                                 cout << val3<<", "<<val2<<", "<<val1<<'\n';
  26.     }
  27.     keep_window_open();
  28.     return 0;
  29. }
I can't seem to get this exercise working properly. When I run it there are no errors, although it doesn't output the values in chronological order. Which is what I'd like it to do.

Any help would be much appreciated.

Jeremy C.
Nov 13 '10 #1
2 1845
newb16
687 512MB
because, say, 3 <= 1 <= 4 evaluates to
(3<=1) <= 4 then to
0 <= 4 that is true. Use || and && for multiple conditions.
Nov 13 '10 #2
Ah, thank you. For some reason I wasn't able to find both of those operators. I guess I didn't look hard enough.

Thanks again newb16
Nov 13 '10 #3

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

Similar topics

1
by: David Isaac | last post by:
Alan Isaac wrote: > Default parameter values are > evaluated once when the function definition is > executed. Where are they stored? ... Where is this documented? Forgive any poor phrasing: I'm...
1
by: roy_it | last post by:
During the validation of a xml, with my xsd file, can I compare the value of two attributes? Thank U Nella validazione di un xml, con un file xsd, posso confrontare i valori di due...
5
by: Jim H | last post by:
I will have many 3 byte Byte arrays. Is there a way to compare the values without iterating through and comparing each element in the code? Example: byte lTest1 = new byte {0x1, 0x2, 0x3};...
11
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
2
by: rbt | last post by:
What's a good way to compare values in dictionaries? I want to find values that have changed. I look for new keys by doing this: new = if new == : print new, "No new files." else: print new,...
2
by: kw_uh97 | last post by:
Hello everone, I am a newbie and I have a recordset that returns the values XXXXMain XXXX1 XXXX2 XXXX3 XXXX4 XXXX5 XXXX6 YYYY1
5
by: PerlPhi | last post by:
hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show...
3
by: mcolson | last post by:
I am trying to compare the last two values in the same column of a table. First of all, I have a column titled Row_Index that uses an index which starts at 1 and increments by 1. What I am trying...
10
by: grego9 | last post by:
I am trying to add in a simple IF function that applies only if the cell contains a certain value. Does anyone know of a formula type that would return this - or what syntax I should use with the...
1
by: =?Utf-8?B?bGlhbnF0bGl0?= | last post by:
Is using a jump statement more faster than using if statement with a jump statement example for(int i=0; i < 10; i++) { if(a == null) {
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.