質問

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);
}
役に立ちましたか?

解決

Your getchar() call returns one character from the standard input. When a users enters an answer, he then hits an enter/return key which translates to new line character which is part of the line that is then sent to standard input.

What you should probably do is to only check first character returned by getchar and then, in loop, read and discard all characters until you get new line character (\n). Only then, you can proceed to ask the question another time.

You should use this loop because your user may enter several characters at once. For example he may enter "yes" which will count as 4 characters.

他のヒント

Try flushing the stream before completing the loop, with

fflush(stdin);

If you don't want to use this function, you could try this,

do{
    if(answerTries!=0)  //it should clear your newline char input
         getchar();
    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');

hope it helped..

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top