can some help me change this to descending

at the min the descending is ascending, could you help me descend it

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Number Sorter
#include <iostream>

#include <algorithm> 
#include <functional> 


using namespace std;

char menu()
{
    char choice;
    cout<< "\nNumber Sorter - Created By Ben Tickle";
    cout<< "\n\n**************************************\n";
    cout<< "\n\n Please choose one of the following:\n";
    cout<< "\n\n 1 - Ascending order ";
    cout<< "\n 2 - Descending order ";
    cout<< "\n 3 - Exit";
    cout<< "\n\n\n**************************************\n";
    cout<< "\n\n Enter you choice and press return: ";
    cin >> choice;
    return choice;
}

int main()
{
    int a[5];
    int b[5];
    int const size = 5; 
    
    char choice;
    
    do
    {
        choice = menu();
        
        switch (choice)
        {
            case '1':
                cout << "\n\nAscending order"
                     << "\n\n\nPlease enter 5 numbers include spaces:";
                
                cin >> a[0]; 
                cin >> a[1]; 
                cin >> a[2]; 
                cin >> a[3]; 
                cin >> a[4]; 
{ 
    
    

    std::sort(a, a+size, std::greater<int>()); 

    for(int i = 0; i<size; ++i) 
        std::cout << a[i] << " "; 
    std::cout << std::endl; 
    
} 
                // Pause for user
                system("pause");
            break;             
            case '2':
                cout<< "\n\nDescending order"
                    << "\n\n\nPlease enter 5 numbers include spaces:";
                    
                cin >> b[0]; 
                cin >> b[1]; 
                cin >> b[2]; 
                cin >> b[3]; 
                cin >> b[4];
{ 
    
    

    std::sort(b, b+size, std::greater<int>()); 

    for(int i = 0; i<size; ++i) 
        std::cout << b[i] << " "; 
    std::cout << std::endl; 
    
}
                
                
                // Pause for user
                system("pause");
            break;
            case '3':
                cout<< "Exit"; 
                break;
            default:
                cout<< "\nNot a valid choice.";
        }
    } while (choice != '3');

    return 0;
} 
You've done the same thing both times and you expect different results :)

For ascending order, use "less" instead of "greater".

Change std::sort(a, a+size, std::greater<int>()); to std::sort(a, a+size, std::less<int>());
i have just tried that but it still sends the number out ascending?
In line 52 change greater by less. And it will work, I promise :)
it really doesnt work, try it, any other suggestions

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Number Sorter
#include <iostream>

#include <algorithm> 
#include <functional> 


using namespace std;

char menu()
{
    char choice;
    cout<< "\nNumber Sorter - Created By Ben Tickle";
    cout<< "\n\n**************************************\n";
    cout<< "\n\n Please choose one of the following:\n";
    cout<< "\n\n 1 - Ascending order ";
    cout<< "\n 2 - Descending order ";
    cout<< "\n 3 - Exit";
    cout<< "\n\n\n**************************************\n";
    cout<< "\n\n Enter you choice and press return: ";
    cin >> choice;
    return choice;
}

int main()
{
    int a[5];
    int b[5];
    int const size = 5; 
    
    char choice;
    
    do
    {
        choice = menu();
        
        switch (choice)
        {
            case '1':
                cout << "\n\nAscending order"
                     << "\n\n\nPlease enter 5 numbers include spaces:";
                
                cin >> a[0]; 
                cin >> a[1]; 
                cin >> a[2]; 
                cin >> a[3]; 
                cin >> a[4]; 
{ 
    
    

    std::sort(a, a+size, std::greater<int>()); 

    for(int i = 0; i<size; ++i) 
        std::cout << a[i] << " "; 
    std::cout << std::endl; 
    
} 
                // Pause for user
                system("pause");
            break;             
            case '2':
                cout<< "\n\nDescending order"
                    << "\n\n\nPlease enter 5 numbers include spaces:";
                    
                cin >> b[0]; 
                cin >> b[1]; 
                cin >> b[2]; 
                cin >> b[3]; 
                cin >> b[4];
{ 
    
    

    std::sort(b, b+size, std::less<int>()); 

    for(int i = 0; i<size; ++i) 
        std::cout << b[i] << " "; 
    std::cout << std::endl; 
    
}
                
                
                // Pause for user
                system("pause");
            break;
            case '3':
                cout<< "Exit"; 
                break;
            default:
                cout<< "\nNot a valid choice.";
        }
    } while (choice != '3');

    return 0;
} 
Topic archived. No new replies allowed.