0

So I've testes a few things and I've noticed that with the following code if the user input is Y/y it won't come out of the loop (I call getchar() again to get rid of the \n in the queue so it'll be ready for the next input from the user) :

#include <stdio.h>
#include <stdbool.h>

int main(int argc, char* argv[]) {
    for(int i = 0; i < argc; i++)
        printf("argv[%d] = %s\n", i, argv[i]);
    char name[20];
    printf("What's your name ? ");
    gets(name);
    char lastname[20];
    printf("%s what's your last name ? ", name);
    fgets(lastname, 20, stdin);
    int age;
    printf("%s %s what's your age? ", name, lastname);
    scanf("%d", &age);
    bool exit = false;
    char c;
    while (!exit) {
        printf("Do you wish to exit the program ? (Y/N) ");
        c = getchar();
        getchar();
        if (c == 'Y' || c == 'y')
            exit = true;
    }
    printf("Have a nice day %s %s.\n", name, lastname);
    return 0;
}

Can someone enlighten me what is the problem ?

AALC
  • 93
  • 1
  • 2
  • 6

1 Answers1

0

There is already a newline char in the input stream when you enter the loop. This causes your first getchar to return \n. The second getchar reads the Y. To fix this you need a getchar call before you enter the loop.

Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
  • When I learned about the getchar function I've been told that it holds the input inside of a queue, so does it means that this queue isn't exclusive to the getchar function but to all inputs function ? Also, where did the \n character came from ? I would assume it came from the last input function that was called which was scanf but as far as I know it chops the \n character that it returns, but maybe it just doesn't return it and it get left in the queue ? I would be thankful if you could explain me this matter. – AALC Aug 28 '14 at 18:18
  • All input with the `scanf` and `getchar` family of functions comes from the `stdin` stream, so yes, they all share the same input queue. Your problem here is that `scanf` is leaving the newline in the queue because you specified `%d` as the input format. Had you specified `%d\n` then `scanf` would have swallowed the newline for you. So you can solve the problem that way, or just by adding a `getchar` call before the loop. – Carey Gregory Aug 28 '14 at 18:24
  • Ok I think I understand better now. One last question when you say that if I were to specify %d\n instead of %d then the \n character wouldn't been popped out of the queue. Why is that ? – AALC Aug 28 '14 at 19:03
  • 1
    @CareyGregory `\n` in `scanf` means to consume all whitespace up to the next non-whitespace character, so `scanf("%d\n"` will block for input after pressing Enter until another line is entered. – M.M Aug 28 '14 at 20:24
  • @MattMcNabb Good point. I knew somewhere in the back of my head that adding `\n` to `scanf` tended to cause problems, and now I remember why. So the OP should take this into account and use `getchar` as I recommended in my answer. – Carey Gregory Aug 28 '14 at 21:06
  • @AALC To really understand how `scanf` processes format specifiers, you need to look up the standard for `scanf`. It's a pretty complex function. – Carey Gregory Aug 28 '14 at 21:06