I am brand new to stack overflow here so thank you all for your patience in helping me with my issue. I am writing a program in C++ that implements the insertion sort by sorting numbers from a .txt file. It accepts a file, shows the contents and then asks the user if they want to sort the numbers. When I key "y" it is supposed to initiate the insertion sort algorithm in my code. However right now all it does is finish compiling. Any advice is greatly appreciated.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Read file input and display contents
ifstream& readfile(ifstream& file)
{
string line;
if (getline(file, line))
{
cout << line;
}
return file;
}
int main()
{
string fileName, line, r;
int n, i, j, k, temp;
int a[n];
ifstream fs;
cout << "enter file: ";
cin >> fileName;
ifstream file(fileName);
if (file.is_open())
{
while (readfile(file))
{
}
}
cout << endl << endl << "Sort File? y or n? ";
cin >> r;
if (r == "y")
{
for (i = 0; i < n; i++)
{
cin >> a[i];
}
for (i = 1; i < n; i++)
{
for (j = i; j >= 1; j--)
{
if (a[j] < a[j - 1])
{
temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
}
else
{
break;
}
}
}
for (k = 0; k < n; k++)
{
cout << a[k] << endl;
}
}
else
{
cout << endl << "error" << endl;
}
cin.get();
return 0;
}