I am a total beginner with this, started a couple days ago. I have no idea on how to get numbers in order, I have to have a user enter 3 numbers and put them in order, help is appreciated

#include <iostream>

using std::endl;
using std::cin;
using std::cout;

int main()
{
	//declare variables
	bool Number_1 = 0;
	bool Number_2 = 0;
	bool Number_3 = 0;

	//enter input items
	cout << "Please enter the first number: ";
	cin >> Number_1;
	cout << "Please enter the second number: ";
	cin >> Number_2;
	cout << "Please enter the third number: ";
	cin >> Number_3;

	if (Number_1 <= Number_2 <= Number_3)
		cout << "The first number is: " << Number_1 << endl;
		cout << "The second number is: " << Number_2 << endl;
		cout << "The third number is: " << Number_3 endl;
	else if	Number_1 <= Number_3 <= Number_2)
		cout << "The first number is: " << Number_1 << endl;
		cout << "The second number is: " << Number_3 << endl;
		cout << "The third number is: " << Number_2 endl;
	else if ( Number_2 <= Number_1 <= Number_3)
		cout << "The first number is: " << Number_2 << endl;
		cout << "The second number is: " << Number_1 << endl;
		cout << "The third number is: " << Number_3 << endl;
	else if ( Number_2 <= Number_3 <= Number_1)
		cout << "The first number is: " << Number_2 << endl;
		cout << "The second number is: " << Number_3 << endl;
		cout << "The third number is: " << Number_1 << endl;
	else if ( Number_3 <= Number_2 <= Number_1)
		cout << "The first number is: " << Number_3 << endl;
		cout << "The second number is: " << Number_2 << endl;
		cout << "The third number is: " << Number_1 << endl;
	else if ( Number_3 <= Number_1 <= Number_2)
		cout << "The first number is: " << Number_3 << endl;
		cout << "The second number is: " << Number_1 << endl;
		cout << "The third number is: " << Number_2 << endl;
	//end ifs
	
	return 0;
}	//end of main function

Recommended Answers

All 5 Replies

I understand that I cannot use else if unless the if is the same right?

Think in similar manner. Its not complete.

#include<iostream>

using namespace std;
 
int main()
{ 	
	int X = 0;
	int Y = 0;
	int Z = 0;

	cin >> X >> Y >> Z;

	if(X < Y)   
	{
		if( X < Z ) 
		{
			cout << X << " ";
			if( Z < Y)
			{
				cout << Z << " ";
				cout << Y << " ";
			}
			else 
			{
				cout << Y << " ";
				cout << Z << " ";
			}
		}

		else{
			cout << Z << " ";
			cout << X << " ";
			cout << Y << " ";
		}
		
	}


	
	return 0;

}

Uh good. But I think you realised your limitation.
For sorting integers, there are many algorithm available.
But before that, you need to build a common program to try those. This program includes code to input N numbers into an array. Then it sorts the array using one of the algorithm and finally prints the result( that is the sorted array itself).
So go and try each of the algorithm and post back if you find any difficulties.

Tip: start out with insertion sort.

Uh good. But I think you realised your limitation.
For sorting integers, there are many algorithm available.
But before that, you need to build a common program to try those. This program includes code to input N numbers into an array. Then it sorts the array using one of the algorithm and finally prints the result( that is the sorted array itself).
So go and try each of the algorithm and post back if you find any difficulties.

Tip: start out with insertion sort.

I'm not sure He knows about arrays yet.

@OP this code :

//declare variables
	bool Number_1 = 0;
	bool Number_2 = 0;
	bool Number_3 = 0;

Type bool should be changed to int at least.
bool can either be 0 or 1.
int can be anyNumber within its range, −32,768 to +32,767

@ OP this code, and similar :

if (Number_1 <= Number_2 <= Number_3)

Is not doing what you think its doing.
The compiler first evaluates the first term , Number_1 <= Number_2.
This return either 0 or 1, or true or false.
The the true of false is evaluated with the right hand side.
0 <= Number_3 or 1 <= Number_3 which is the same as
false <= Number_3 or true <= Number_3.

As you can see its not comparing the number2 to number 3,
rather, its comparing the result from number_1<=number_2.

I have not used arrays yet,
i've been working on this for a couple of hours
heres what i have got, it is similar to a response on here i think

#include<iostream>


using std::cin;
using std::cout;
using std::endl;

int main()
{	
	//declare variables
	double num1 = 0.0; 
	double num2 = 0.0; 
	double num3 = 0.0;

	//enter inputs
	cout<<"Please enter three numbers: ";
	cin >> num1 >> num2 >> num3;
	cout<<"Lowest to Highest: ";

	//main algorithm
	if(num1 < num2)
	{
	if(num1 < num3)
	{
		cout << num1 << " ";
            if (num3 < num2)
            cout << num3 << " " << num2 << endl;
            else cout << num2 << " " << num3 << endl;
	}
        else
		{
			cout << num3 << " ";
			if (num1 < num2) cout << num1 << " " <<num2 << endl;
            else cout << num2 << " " << num1 << endl;
	}
    }
    else
	{
		if(num2 < num3)
		{
		cout << num2 << " ";
        if(num1 < num3) cout << num1 << " " << num3 << endl;
            else cout << num3 << " " << num1 << endl;
        }
        else
		{
			cout << num3 << " ";
			if(num1 < num2) cout << num1 << " " << num3 << endl;
				else cout << num2 << " " << num1 << endl;
		}
	}
    return 0;
} //end of main function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.