Code:
#include <iostream>
#include <iomanip>
#include <string.h>
#include <fstream>
using namespace std;
void insertionsort(struct eRec r[],int n,char fileOutName[]);
void pointinsertionsort(struct eRec r[], int n,char fileOutName[]);
void print(struct eRec p1[], int n, char fileOutName[], int choice);
struct eRec
{
char g[20];
char s[20];
char d[20];
float pr;
char e[16];
};
int main()
{
char fileInName[25];
char fileOutName[25];
char response;
int filecount = 0; //The number of lines in the file.
FILE *inFile; // Logical input file name in program.
FILE *outFile; // Logical output file name in program.
//Gets the file name of input and then checks to see if it exists
printf("What is the name of the input file (max 25 characters)? ");
scanf("%s", &fileInName);
inFile = fopen(fileInName, "r");
if (inFile == NULL)
{
printf("This file does not exist.");
exit(1);
}
//Gets the file out name and then checks to see if it already exists
//If so then it asks for you to choose whether or not to override.
printf("What is the name of the output file (max 25 characters)?");
scanf("%s", &fileOutName);
if((outFile = fopen(fileOutName, "r" )) != NULL )
{
printf("\nA file by the name %s exists.\n", fileOutName);
printf("Do you wish to overwrite (Y or N): ");
//Skip <cr> in buffer with "%*c", i.e., one character
scanf("%*c %c", &response);
if( (response == 'n') || (response == 'N') )
{ // should close file first
printf("\nProgram aborted to prevent overwirte!");
exit(1);
}
}
outFile = fopen( fileOutName, "wt" );
if( outFile == NULL )
{
printf("Could not create the output file! Program terminating.");
exit(1);
}
struct eRec sort[20];
char givname[20];
char surname[20];
char department[20];
char eyecolor[20];
float payrate;
int c = 0;
printf("GivenName \t Surname \t Department \t PayRate \t EyeColor \n");
while((fscanf(inFile,"%s%s%s%f%s ", surname, givname, department, &payrate, eyecolor))!= EOF)
{
strcpy(sort[c].g, givname);
strcpy(sort[c].s, surname);
strcpy(sort[c].d, department);
sort[c].pr = payrate;
strcpy(sort[c].e, eyecolor);
printf("%s \t %s \t %s \t %f \t %s\n", sort[c].g, sort[c].s, sort[c].d, sort[c].pr, sort[c].e);
c++;
}
print(sort, c, fileOutName, 1);
insertionsort(sort,c,fileOutName);
pointinsertionsort(sort,c,fileOutName);
fclose(inFile);
fclose(outFile);
return 0;
}
void print(struct eRec t[], int n, char fileOutName[], int choice)
{
FILE *outFile;
outFile = fopen(fileOutName,"a+");
if (choice == 1)
{
fprintf(outFile, "Unsorted.\n");
}
if (choice == 2)
{
fprintf(outFile, "\nSorted.\n");
}
if (choice == 3)
{
fprintf(outFile, "\nSorted with pointers!\n");
}
fprintf(outFile, "GivenName \t Surname \t Department \t PayRate \t EyeColor \n");
for(int i = 0; i < n; i++)
{
fprintf(outFile,"%s\t %s\t %s\t %f\t %s\t\n", t[i].g, t[i].s, t[i].d, t[i].pr, t[i].e);
}
fclose(outFile);
}
void insertionsort(struct eRec r[], int n, char fileoutname[])
{
int i;
int result;
int looper;
struct eRec temp;
struct eRec m;
for(int j = 1; j < n; j++)
{
m = r[j];
i = j - 1;
temp = r[j];
looper = 1;
while(looper != 0)
{
if((strcmp(m.d, r[i].d)) >= 0)
{
r[i + 1] = temp;
looper = 0;
}
else
{
r[i + 1] = r[i];
i--;
if(i == 0)
{
r[0] = temp;
looper = 0;
}
}
}
}
for(int l = 0; l < n; l++)
{
printf("%d. %s %s, %s\n", l+1, r[l].g, r[l].s, r[l].d);
}
print(r,n,fileoutname,2);
}
void pointinsertionsort(struct eRec r[], int n, char fileoutname[])
{
int i;
int temp2;
int max;
int pntr[n];
for(int t = 0; t < n; t++)
{
pntr[t] = t;
}
for(int t = (n -1); t > 0; t--)
{
max = 0;
for(int j = 1; j <= t; j++)
{
if ( strcmp(r[pntr[j]].d, r[pntr[max]].d) > 0)
{
max = j;
}
}
temp2 = pntr[max];
pntr[max] = pntr[t];
pntr[t] = temp2;
}
//for(int j = 1; j < n; j++)
//{
// m = r[j];
// i = j - 1;
// temp2 = pntr[j];
// looper = 1;
// while(looper != 0)
// {
// temp = r[pntr[i]];
// if((strcmp(m.d, temp.d))>0)
// {
// pntr[i+1] = temp2;
// looper = 0;
// }
// else
// {
// pntr[i + 1] = pntr[i];
// i--;
// if(i == 0)
// {
// pntr[0] = temp2;
// looper = 0;
// }
// }
// }
//}
for(int p = 0; p < n; p++)
{
printf("%d. Pointer: %d\n", p+1, pntr[p]);
}
print(r,n,fileoutname,3);
}
Thank you, any suggestions or help on how to fix this is greatly appreciated.