I'm writing a program that adds line numbers to C files. I get the filenames as command line arguments but I wanted the user to have a chance to enter them if they forget to when they run the program. I ask the user to if they want to enter filenames and then they answer 'y' or 'n'. They are given five tries to answer correctly if an invalid character is entered but after five tries the program prints an error message and terminates. If the user enters an invalid character I have it print '[y/n]?' to the screen to prompt the user for those letters. If an invalid character is entered though it goes through the loop twice and prints them out side by side. Why does this happen?
Compiler.c file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lineNumAdderHeader.h"
#include "miscellaneousHeader.h"
#include "errorCheckedFunctionsHeader.h"
int main(int argc, char *argv[]){
int i = 1;
char ch;
int answerTries = 0;
char *seperatedFilenames[argc - 1];
if (argc < 2){
fprintf(stderr, "No files were entered for compiling.\n");
answer: do{
if (answerTries == 0)
printf("Would you like to enter files for compiling [y/n]? ");
else if (!(answerTries < 5))
fatal("in main(). An invalid character was entered too many times.");
else
printf("[y/n]? ");
ch = getchar();
if (ch == 'n' || ch == 'N')
exit(0);
answerTries++;
} while (ch != 'y' && ch != 'Y');
}
else{
while (i < argc){
seperatedFilenames[i - 1] = argv[i];
i++;
}
}
i = 0;
while (i < (argc - 1)){
lineNumAdder(seperatedFilenames[i]);
i++;
}
}
Fatal Funciton:
/*Displays a fatal error*/
void fatal(char *errorMessage){
/*Holds the errorMessage*/
char completedErrorMessage[strlen(errorMessage) + 17];
/*Copies the error message into completedErrorMessage*/
strcpy(completedErrorMessage, "[!!] Fatal Error ");
strcat(completedErrorMessage, errorMessage);
/*Prints the error message to the screen*/
fprintf(stderr, "%s\n", completedErrorMessage);
/*Exit the program in failure*/
exit(-1);
}