0

I try to make a C console program ask user enter Y (means yes) or N (means no) by using if and while,the user only can enter Y or N,otherwise the program will tell the user input error,no matter they input what the characters, how many strings.The fuction just like terminal ask user “Are you sure?Input Y or N".

But my program will give me more than one error feedback when i test.I relly want it just give me one feedback.Hope you can help me perfect my progrem.

Here are my code:

#include <stdio.h>
int main(void)
{
    char yon = '\0';
    do
    {
        if (yon != '\0')
            printf("\nSorry,Please try again.\n");
        printf("\nEnter Y/N?:");
    } 
    while ((yon = getchar()) != 'y'&&yon != 'Y'&&yon != 'n'&&yon != 'N'); 
    return 0;
}

My English is not good,If you do not know what i mean,Please tell me to edit.

Thanks.

1 Answers1

1

I just now edit my code,it runs very nice without bugs.

Here are my code:

#include <stdio.h>
int main(void)
{
    char yon = '\0';
    do
    {
        fflush(stdin);
        if (yon != '\0')
            printf("\nSorry,Please try again.\n");
        printf("\nEnter Y/N?:");
        scanf("%1s",&yon);
    } 
    while ( yon!= 'y'&&yon != 'Y'&&yon != 'n'&&yon != 'N');
    return 0;
}

Thanks.

  • Be Careful!! `fflush(stdin);` is NOT doing what you think it is. In fact `fflush(stdin);` is *undefined* on most systems. `fflush` is only guaranteed to be defined for input streams associated with seekable files (not pipes or terminals) – David C. Rankin Feb 06 '17 at 06:10
  • @DavidC.Rankin Do you mean this `fflush(stdin);` fuction can not work on every computer?I do not know my humble understanding is right or wrong?Please tell me more? – xiazhanjian Feb 06 '17 at 06:27
  • `fflush (stdin)` will NOT work on every computer. It is only defined for *seekable* files (e.g. disk files), not for `stdin` (see *man fflush*). You are better server by manually flushing the input buffer if you need to (e.g. `int c; while ((c = getchar()) != '\n' && c != EOF) {}`) That will read and discard all remaining characters in `stdin` without undefined behavior. – David C. Rankin Feb 06 '17 at 06:30
  • @DavidC.Rankin Thanks!Thank for your advice for my code,I will learn more about computer science.By the way,could you think i need edit my answer and delete this fuction `fflush(stdin);`?Or could you give me a better answer about my question? – xiazhanjian Feb 06 '17 at 06:59
  • I would be happy to, but your question was put on hold. Try to reword it by explaining a little more clearly how your output is not what you expect based on the code you posted (personally I think it is clear enough, but there are apparently others who can't quite understand). There are a couple of issues you need to consider. What happens if the user cancels input? (e.g. generates a manual `EOF`) You should always handle that condition when taking user input. Otherwise, you just need to improve the logic of your code a bit and make it clear you only want the first character of input. – David C. Rankin Feb 06 '17 at 07:12