What is the error with this code?

#include <iostream>
using namespace std;

void main()
{
int array[6];
int temp=0;
for (int i=0; i<5; i++)
cin>>array[i];
for (int i=0; i<6; i++)
{
for (int j=0; j<5; j++)
{
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
for (int i=0;i<5; i++)
cout<<array[i];

}
Last edited on
This is code the above in code tags! Yay!

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

void main()
{
     int array[6];
     int temp=0;
     for (int i=0; i<5; i++)
          cin>>array[i];
     for (int i=0; i<6; i++)
     {
          for (int j=0; j<5; j++)
          {
               if(array[j]>array[j+1])
               {
                    temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
               }
          }
     }

     for (int i=0;i<5; i++)
          cout<<array[i];
}


Your main function must return an int. Like so:

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

int main()
{
     int array[6];
     int temp=0;
     for (int i=0; i<5; i++)
          cin>>array[i];
     for (int i=0; i<6; i++)
     {
          for (int j=0; j<5; j++)
          {
               if(array[j]>array[j+1])
               {
                    temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
               }
          }
     }

     for (int i=0;i<5; i++)
          cout<<array[i];

     return 0;
}
Last edited on
1
2
3
void main()
// should be
int main()


1
2
3
for (int i=0; i<5; i++)
    cin>>array[i];
// i never gets to 5! 


1
2
3
4
5
6
7
8
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;

// not a problem, but why not use std::swap() instead?
#include <algorithm>

swap(array[j], array[j+1]);


1
2
3
for (int i=0;i<5; i++)
    cout<<array[i];
// i never gets to 5! 

Topic archived. No new replies allowed.