How to print error message when input is incorrect, then repeat prompt for input

int GetUserInput(char *userStart,char *userDestination){
char firstLetter,secondLetter;
while(1){
    printf("\nEnter starting position: ");
    scanf(" %c",&firstLetter);
    if (firstLetter == 'x' || firstLetter=='X'){
        return 1;
    }
    printf("Enter destination position: ");
    scanf(" %c",&secondLetter);
    if (secondLetter == 'x' || secondLetter=='X'){
        return 1;
    }

    if (!((firstLetter<'a' && firstLetter>'O') || (firstLetter<'A' && firstLetter>'O'))){
        if (!((secondLetter<'a' && secondLetter>'o') || (secondLetter<'A' && secondLetter>'O'))){
            break;
        }else{
            PrintErrorMessage(2);
            printf("second\n");
        }
    }else{
        PrintErrorMessage(1);
        printf("first\n");
    }
}
*(userStart) = firstLetter;
*(userDestination) = secondLetter;
return 0;


}

I entered some incorrect input, however, the error message is not display, it only repeats the prompt for input again.

Output:

Enter starting position: n
destination position: v

Enter starting position:

Expected Output:

Enter starting position: n
Enter destination position: v
"Error Message"
Enter starting position:

Upvotes: 0

Views: 86

Answers (1)

bruceg
bruceg

Reputation: 2513

Usually you will do something like this:

do {
    get_input(&a, &b);
while (validate_input(a,b));

Then, when validate_input returns 0, you are done. If it returns something other than 0, you call get_input again.

Upvotes: 1

Related Questions