Input Numbers and Sort them ascending

Hello everyone! I am new with C++ and I am trying to write 10 numbers via the "cin" command and then to sort them ascending (e.g if I wrote 32, 23, 65, 45, 22, 12, 3, 5, 8, 1, 10; the Command Prompt should sort it so it'll look like this: 1, 3, 5, 8, 10, 12, 22, 23, 32, 45, 65). 'Til yet I managed only to write the numbers because the Command Prompt closes after I write them all.

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.h>
#include<conio.h>
using namespace std;

main()
{
int number[10],i,a,b,c,d,t;
cout<<"Enter the elements of the array:"<<endl;
for(int i=0;i<10;i++)
{
cout<<"Enter number for cell "<<i<<endl;
cin>>number[i];
}
cout<<endl<<"Thank You! The data entry is over."<<endl;
cout<<"\a\a";
for(int j=0; j<10; j++)
{
for(int k=0; k<9; k++)
{
if (number[k]>number[k+1])
{
a=number[k];
number[k]=number[k+1];
number[k+1]=a;
}
}
}
cout<<"The list in ascending order is ";
cout<<endl;
for(i=0;i<10;i++)
cout<<number[i]<<"\t"<<endl;
}


Any possible help? Command prompt just closes after I write the 10 numbers.
I just had to update the iostream header and return int from main, but otherwise your code ran for me. What sorting algorithm are you using?

Edit: If the console window is just closing quickly, you might want to check out the thread stickied at the top of this section.

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
#include<iostream>
using namespace std;

int main()
{
    int number[10],i,a,b,c,d,t;
    cout<<"Enter the elements of the array:"<<endl;
    for(int i=0;i<10;i++)
    {
        cout<<"Enter number for cell "<<i<<endl;
        cin>>number[i];
    }
    cout<<endl<<"Thank You! The data entry is over."<<endl;
    cout<<"\a\a";
    for(int j=0; j<10; j++)
    {
        for(int k=0; k<9; k++)
        {
            if (number[k]>number[k+1])
            {
                a=number[k];
                number[k]=number[k+1];
                number[k+1]=a;
            }
        }
    }
    cout<<"The list in ascending order is ";
    cout<<endl;
    for(i=0;i<10;i++)
        cout<<number[i]<<"\t"<<endl;
}


Enter the elements of the array: 
Enter number for cell 0 
32 
Enter number for cell 1 
23 
Enter number for cell 2 
65 
Enter number for cell 3 
45 
Enter number for cell 4 
22 
Enter number for cell 5 
12 
Enter number for cell 6 
3 
Enter number for cell 7 
5 
Enter number for cell 8 
8 
Enter number for cell 9 
1 
Thank You! The data entry is over. 
The list in ascending order is 
1 
3 
5 
8 
12 
22 
23 
32 
45 
65 
Last edited on
Topic archived. No new replies allowed.