help me please

I'm get stuck when I try to write a C++ program that will input 3 integer values in any order and output them in order of lowest to highest
The following is the code I have created. Thank you all.

#include <iostream>
using namespace std;

int main ( )

{
int num1, num2, num3;
String In_order;
cout << "Enter first value:\n";
cin >> num1;
cout << "Enter second value:\n";
cin >> num2;
cout << "Enter third value:\n";
cin >> num3;

if (num1 > num2) && (num1 > num3)
{
if (num2 > num3)
{
In_order = num1, num2, num3;

cout << "The order from lowest to highest is: "
<< In_order << endl;
}
else (num2 < num3)
{
In_order = num1, num3, num2;
cout << "The order from lowest to highest is: "
<< In_order <<endl;
}
}
if (num2 > num1) && (num2 > num3)

{
if (num1 > num3)
{
In_order = num2, num1, num3;
cout << "The order from lowest to highest is: "
<< In_order <<endl;
}
else (num1 < num3)
{
In_order = num2, num3, num1;
cout << "The order from lowest to highest is: "
<< In_order <<endl;
}
}

else (num3 > num1) && (num3 > num2)
{
if (num1 > num2)
{
In_order = num3 num1, num2;
cout << "The order from lowest to highest is: "
<< In_order <<endl;
}
else (num1 < num2)
{
In_order = num3, num2, num1;
cout << "The order from lowest to highest is: "
<< In_order <<endl;
}

}
return 0;
}

first thing you need to do is copy your code and place it in between the tags (source code tags)

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

#include<iostream>

using namespace std;

int main()
{
    int num1, num2;
    
    cout << "? num1";
    cin >> num1;
    cout << "? num2";
    cin >> num2;
    
    if(num1 > num2)
        cout <<num1;
    else
        cout << num2;
    
    /*you have three variables so you cannot use this same
    method as i have this is an example of how to insert code*/



    system("PAUSE");
    return 0;
}
Hi,
Its better to use vectors with sort() function to make things very easy. The method you are trying can get really complicated when you are working with many numbers. If you use sort() there isn't a need for comparing values.

Here is a version of your code using vectors and sort().

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
#include <iostream>
#include <vector> //For vector
#include <algorithm> //For sort()

using namespace std;

int main()
{
    //Declare a vector of type int
    vector<int> numbers;
    //Declare the integers
    int num1,num2,num3;
    
    //Get the numbers
    cout << "Enter num 1: ";
    cin >> num1;
    cout << "Enter num 2: ";
    cin >> num2;
    cout << "Enter num 3: ";
    cin >> num3;
    
    //Put the numbers in the numbers vector
    numbers.push_back(num1);
    numbers.push_back(num2);
    numbers.push_back(num3);
    
    sort(numbers.begin(), numbers.end()); //This sorts the vector in ascending order
    
    cout << "The numbers from smallest to biggest is: " << endl;
    for(int i = 0; i < numbers.size(); i++)
    {
        cout << numbers[i] << " "; //Vectors are just like arrays and an element can be accessed by vectorname[index]
    }
    
    return 0;
}


To make things more simple:

(Here to increase/decrease the number of user inputs just change the limit and the code will still work flawlessly.)

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
#include <iostream>
#include <vector> //For vector
#include <algorithm> //For sort()

using namespace std;

int main()
{
    //Declare a vector of type int
    vector<int> numbers;
    //Declare one int
    int num;
    const int limit = 3; //The total numbers to get
    
    //Get the numbers
    for(int index = 0; index < limit; index++)
    {
        cout << "Enter number " << index+1 << ": ";
        cin >> num; //Get number
        numbers.push_back(num); //Put it in the vector
    }
    
    sort(numbers.begin(), numbers.end()); //This sorts the vector in ascending order
    
    cout << "The numbers from smallest to biggest is: " << endl;
    for(int i = 0; i < numbers.size(); i++)
    {
        cout << numbers[i] << " "; //Vectors are just like arrays and an element can be accessed by vectorname[index]
    }
    
    return 0;
}
Last edited on
I'm very new for the C++ , can you all help me with the simple code?
I haven't study about vector and array yet. Do you please have another code that not using vector and array? Thank you very much.
Topic archived. No new replies allowed.