Putting three numbers in ascending order

My assignment is to write a program to put three numbers in ascending order. Here is what I've got:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>

using namespace std;

int main()

{
	int number1;
	int number2;
	int number3;
	int exit;

	cout << "This program will put three numbers in ascending order." << endl;
	cout << " " << endl;
	cout << "Please type the first number and press Enter." << endl;
	cin >> number1;
	cout << " " << endl;
	cout << "Please type the second number and press Enter." << endl;
	cin >> number2;
	cout << " " << endl;
	cout << "Please type the third number and press Enter." << endl;
	cin >> number3;
	cout << " " << endl;
	
	if (number3< number2< number1)
		cout << "The numbers in ascending order are " << number3 << ", " << number2 << ", " << number1 << "." << endl;
	if (number2< number3< number1)
		cout << "The numbers in ascending order are " << number2 << ", " << number3 << ", " << number1 << "." << endl;
	if (number3< number1< number2)
		cout << "The numbers in ascending order are " << number3 << ", " << number1 << ", " << number2 << "." << endl;
	if (number1< number3< number2)
		cout << "The numbers in ascending order are " << number1 << ", " << number3 << ", " << number2 << "." << endl;
	if (number1< number2< number3)
		cout << "The numbers in ascending order are " << number1 << ", " << number2 << ", " << number3 << "." << endl;
	if (number2< number1< number3)
		cout << "The numbers in ascending order are " << number2 << ", " << number1 << ", " << number3 << "." << endl;
		
	cout << " " << endl;
	cout << "Type 'exit' to exit." << endl;
	cin >> exit;
}


The program will run but when I put in the three numbers I get five different responses instead of the one correct response. What's wrong?

You can't use the < and > like you are there. You have to do something like this:

if(number3 < number2 && number2 < number 1)

Anyway, the reason it isn't working it because you probably meant to have those statements be else ifs, not normal ifs.
Ah, the && did the trick. Thanks so much.

What is the difference in "if" and "else", when should I use which, and why should I use "else" here? (I left the "ifs" in there, by the way, and as far as I can tell it worked perfectly)
1
2
3
4
5
6
7
8
9
10
11
12
13
int a;
cin >> a;

if (a == 1)
cout << "a is 1";

else if (a == 2)
cout << "a is 2";

else if (a == 3)
cout << "a is 3";

//etc... etc... 


That is how you should test several conditions.
If you need more than one if for the same purpose (such as checking what number the user typed in) only the first if should actually be "if", and the rest being "else if".

However, to get the most out of understanding this, consider this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int a;
cin >> a;

if (a == 1)
cout << "a is 1";

else if (a == 2)
{
if (a < 3 && a > 1) //this is a subcondition of the else if.
cout << "a is 2";
}

else if (a == 3)
{
if (a != 2 && a != 4) // this is a subcondition of the else if. 
cout << "a is 3";
}
Last edited on
That will be very helpful on my next assignment. Thank you both.
Angel: i don't reallly understand what the float statment is used for i have already checked forums in english and russian and i don't get any info that im looking for and i do speak a little russian but the books were also translated in some parts where i couldn't even understand and as far as i can tell float = ((rootbeer + icecream) * blender)^2 in other words i want two rootbeer floats lol lame joke sorry. O_o
Besides the fact that i have no idea whatsoever about what you're jabbering about - The float was a misstake, changed it to integer now.
Topic archived. No new replies allowed.