1

I am using c to create a very basic programme. I using scanf to get input from the console but when I use it with a char it just seems to 'skip' it.

Here is the code:

#include <stdio.h>
#include <string.h>


int main(){

    char name[20];
    char yn;

    printf("Welcome to Benjamin's first C programme! \n\n");


     do{

         printf("What is your name? \t");
         scanf("%s", name);
         printf("\n");

         printf("Is your name %s [y/n]?", name);
         scanf("%c", &yn);
         printf("\n");

     } while(yn != 'y');
}
  • 1
    This question has been asked about a zillion times this week. And the week before. And the week before that. The answer is in the docs. The answer is already somewhere on Stackoverflow. Just sayin' – Vorsprung Nov 26 '13 at 19:55
  • ***Cannot*** see a reason for down vote. Question meets criteria, well stated problem, code showing problem, stated expected results, and where it is not working. – ryyker Nov 26 '13 at 19:59
  • @ryyker Although I did not downvote this time, I can completely agree with Vorsprung. I remember making all these beginner mistakes, yet you don't see me having asked questions like this -- Google and the site search helped me already. –  Nov 26 '13 at 20:17
  • @H2CO3 - agreed and point taken. I do not recall having asked anything I could figure out myself either. But then, when I _first_ started programming, _Google_ was still just a big very number :). It was all books and compiler errors. Now, books (or online tutorials) are under used, and people have not learned that compiler errors are really their friend. _(always appreciate your perspective, thanks)_ – ryyker Nov 26 '13 at 20:33

3 Answers3

4

Add space at the begining of the string format in the second scanf()

scanf(" %c", &yn);

This space will catch the newline that you type after the first input

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
3

Your apparent skipping is because %s specifier matches a string of characters, until a whitespace or a newline character. In this case, it is a newline. The problem is that it leaves it in the input stream, and the next reading matches it. So, in your yn variable, there will always be the newline character. In order to prevent this, insert a space before %c specifier, to skill all blanks (whitespace, newline, tab):

scanf(" %c", &yn);

Also note that for the same reason, your program will not work if I insert my full name (i.e. I insert spaces).

Paul92
  • 8,827
  • 1
  • 23
  • 37
1

As an alternative, try using getchar()

Replace:

scanf("%c", &yn);  

With:

     getchar();//eat the '\n' from <return>
yn = getchar();  

For the record I like the other answers better, just offering this as an alternative...

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • This is how it ought to be done. –  Nov 26 '13 at 20:06
  • @H2CO3 - thanks, but, can you expand? I kind of thought the others had merit because of their brevity. – ryyker Nov 26 '13 at 20:08
  • 1
    `scanf()` is a horrible function. Its usage is counter-intuitive, it doesn't work like one would expect, it is conceptually wrong because it tries to do two inherently distinct things at the same time (parsing a string and performing I/O), etc. `scanf()` is almost never an appropriate function to use. If one needs a character-by-character input, `getchar()` is the right function to use. If one needs line-by-line input, `fgets()` is the good choice. –  Nov 26 '13 at 20:11
  • @H2CO3 - Well, as expected, I am not disappointed in expecting that you had definite thoughts about this. Thanks for sharing them. And I will also keep that in mind for future coding approaches. – ryyker Nov 26 '13 at 20:23