sort 3 numbers

Hello, I've just begun programming, I'm trying to solve this problem but I think I've made a mess of it, could you please help, and explain where I've gone wrong

Problem /* ask the user to input 3 numbers, and then sort them from the lowest to the highest */

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdlib.h>
#include <cstdio>
#include <iostream>

using namespace std;

int main ()

{
    
    /* ask the user to input 3 numbers, and then sort them from the lowest to the highest */
    
int a,b,c,printa,printb,printc;

cout<<"1st number\n";

cin>>a;

cout<<"2nd number\n";

cin>>b;

cout<<"3rd number\n";

cin>>c;

for (int cont=0;cont<3;cont++) {
    if (a < b && < c) {
    printa=a;
    cout<<"The first number is"<<printa;
    else if ( b < a && < c)
    printa=b;
    cout<<"The first number is"<<printa;
    else 
    printa=c;
    cout<<"The first number is"<<printa;
}
if (b > a && < c) {
    printb=b;
    cout<<"The second number is"printb;
    else if (a > b && < c) 
    printb=a;
    cout<<"The second number is"printb;
    else 
    printb=c;
    cout<<"The second number is"<<printb;
    }
    
    if (a > c && > b) {
    printc=a;
    cout<<"The third number is"<<printc;
    else if (b > a && > c) 
    printc=a;
    cout<<"The third number is"<<printc;
    else 
    printc=c;
    cout<<"The third number is"<<printc;
    }
system("Pause");
return 0;
}

Last edited on
Does it work?
what is the output?

if (a > a && > b)
a can never be larger than a, this expression is allways FALSE

also: what does this loop accomplish?
You do the exactly same thing 3 times
1
2
3
4
5
6
7
8
9
10
11
for (int cont=0;cont<3;cont++) {
    if (a < b && < c) {
    printa=a;
    cout<<"The first number is"<<printa;
    else if ( b < a && < c)
    printa=b;
    cout<<"The first number is"<<printa;
    else 
    printa=c;
    cout<<"The first number is"<<printa;
}
Last edited on
made a mistake there , should be
if (a > c && > b) corrected.

technically, I'm expecting the program to check which number is the lowest at every loop, to then proceed to the next else if loop
Well, that's not what's happening here :)
at the moment you just print 3 times "The first number is x" because it checks all if/elseif/else parts in each iteration

edit:
also: this is an invalid syntax && < c
you have to explicitly write the variable anew
if ( a < b && a < c)
Last edited on
Topic archived. No new replies allowed.