This is the first program I have had to do in 5 years so please don't be surprised by some of the mistakes. I am creating a program that will open a file and sort the integers inside that file using Insertion sort. There are 12 files and each as a different number of integers to sort. I am having multiple problems but right now I would like help with writing the contents of the file to the array. I get errors stating I am using two different types, char to int. Here is the code.
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <fstream>
#include <iomanip>
#include <sstream>
using namespace std;
void insertion_sort(int numbers[], int* array[]);
int main()
{
int SIZE;
int array[SIZE]; //array sized for each file input
int numbers;
ifstream infile; //input data file
ifstream fin; // file data opject
char filename[50];
cout << "Enter the name of the file you wish to open. "<<endl;
cin >> filename;
fin.open(filename);
if(infile.bad()) //checks to see if the file will open
{
std::cerr <<"Error. Could not open datafile Num8.txt" <<endl;
exit(8);
}
if(filename == "Num8.txt") //depending on the input file used, the size of the array will change
SIZE = 8;
else if(filename == "Num16.txt")
SIZE = 16;
else if(filename == "Num32.txt")
SIZE = 32;
else if(filename == "Num64.txt")
SIZE = 64;
else if(filename == "Num128.txt")
SIZE = 128;
else if(filename == "Num256.txt")
SIZE = 256;
else if(filename == "Num512.txt")
SIZE = 512;
else if(filename == "Num1024.txt")
SIZE = 1024;
else if(filename == "Num2048.txt")
SIZE = 2048;
else if(filename == "Num4096.txt")
SIZE = 4096;
else if(filename == "Num8192.txt")
SIZE = 8192;
else if(filename == "Num16284.txt")
SIZE = 16284;
else
cout << "You entered a file not accessable by this program"<<endl;
while(!infile.eof()) // the contents of the file are read into the array until the end of file is reached
{
fin.getline(infile,SIZE); //write contents of file to array
insertion_sort(numbers, array); //insertion sort function is called
}
infile.close();
system("PAUSE");
return EXIT_SUCCESS;
}
void insertion_sort(int numbers[], int* array[SIZE])
{
int i;
int j;
int key;
int SIZE;
int count = 0;
for (i=1; i < array; i++)
{
key = numbers[i];
j = i;
while((j > 0) && (numbers[j-i] > key))
{
count = count + 1; //counts the number of times the loop is run
numbers[j] = numbers[j-1];
j = j - 1;
}
numbers[j] = key;
cout >> array; //prints out the new sorted array to the screen
}
}