My program is designed to sort records in a text file, but my main issue is getting the main function to ask the user if he would like to run the program again to read and write another file. My program right now, when the user enters 1 to run again, skips the first question to enter the program to read. Why is that? I appreciate the help! Here is my main function only: The program compiles only during the first run.
int main()
{
int fieldCount = 0;
int lineCount = 0;
char file[STR_LEN];
char filepath[STR_LEN];
char** fields = NULL;
char** lines = NULL;
int recCount = 0;
Person **sortedRecs = NULL;
int x;
do {
printf("Enter path of the file to read: ");
gets(filepath);
printf("Enter path to copy to: ");
gets(file);
fields = readFieldsDynamic(filepath, &fieldCount);
lines = readLinesDynamic(filepath, &lineCount);
recCount = getPersons(fields, fieldCount, &sortedRecs);
if (recCount && lines && sortedRecs && (recCount <= lineCount)) {
writeRecsToFile(file, sortedRecs, recCount, lines, lineCount);
printf("Sorted records are written in %s\n", file);
}
if (fields) {
freePointerToChars(fields, fieldCount);
}
if (lines) {
freePointerToChars(lines, lineCount);
}
if (sortedRecs) {
freeRecs(sortedRecs, recCount);
}
printf("Enter 1 to run program again: ");
scanf("%d%*c", &x);
} while (x == 1);
return 0;
}