CODE
else cout << "Unable to open file";
{
Here is the first problem I found... I think you mean:
CODE
else { cout << "Unable to open file"; }
either way, the open bracket is never closed, so either put the cout inside of it, or delete it.
When someone posts code and says: IT just dosen't compile. The first ting I do is reformat it with indentions for each open bracket. Most IDE's have a neat feature where you hilight a blobk of code and press the tab button and they indent for you. This reveals two of the most common beginner mistakes, non-matching brackets, and improper if-else structures.
if you get into the habit of ALWAYS using
if (condition) { statement; } rather than trying to omit the (unrequired) brackets it will save a world of touble.
Here is code that will compile (no idea if it works)
CODE
#include <iostream>
#include <fstream>
#include <string>
//Had to add this definition
#define ELEMENTS 6
using namespace std;
void insertion_sort(int x[],int length)
{
int key,i;
for(int j=1;j<length;j++)
{
key=x[j];
i=j-1;
while(x[i]>key && i>=0)
{
x[i+1]=x[i];
i--;
}
x[i+1]=key;
}
}
int main ()
{
string line;
ifstream myfile ("inputfile.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
//Had to put this in brackets...
else {cout << "Unable to open file"; }
//had to uncomment this line (as A was undefined).
int A[ELEMENTS]={5,2,4,6,1,3};
int x;
cout<<"NON SORTED LIST:"<<endl;
for(x=0;x<ELEMENTS;x++)
{
cout<<A[x]<<endl;
}
insertion_sort(A,ELEMENTS);
cout<<endl<<"SORTED LIST"<<endl;
for(x=0;x<ELEMENTS;x++)
{
cout<<A[x]<<endl;
}
return 0;
}