-1

I need to use scanf to get a character and a string which would store the user's answer. (yes/no)
The code below skips scanf("%c", &elem).

while ( !strcmp ("yes", option))
{
    printf("enter the elements \n\n");
    elem=getchar();
    printf("you have entered %c\n",elem);
    enqueue(st, elem);
    printf("please enter yes or no ");
    scanf("%s[^\n]",option);
}
./out
enter the elements
a
you have entered a
enqueue elem= a
please enter yes or no yes
enter the elements

you have entered

enqueue elem=
Soft Waffle
  • 356
  • 1
  • 12
Ankur Saran
  • 99
  • 1
  • 9

3 Answers3

2

When you are pressing Enter/Return key to enter the element then a \n character is also passed to the buffer along with the element. This \n is read by your getchar on next call.

To consume this \n place this line after the getchar();

int ch;
while((ch = getchar()) != EOF && ch != '\n'); 
haccks
  • 104,019
  • 25
  • 176
  • 264
2

You don't have any scanf("%c", &elem) in your code... btw the problem is with the enter for scanf. When you get an input by scanf, an enter character stays in the input buffer which will be read by your getchar() function in the second round. one simple way to solve it is to add a dummy getchar after your scanf line:

while ( !strcmp ("yes",option))
{
        printf("enter the elements \n\n");
        elem=getchar();
        printf("you have entered %c\n",elem);
        enqueue(st,elem);
        printf("please enter yes or no ");
        scanf("%s[^\n]",option);
        getchar();
}

You can find more information about how to clear your input buffer here: How to clear input buffer in C?

I can recommend you consider two things:

  1. For getting only a character, I personally found it much more easier to use getch and getche function in Windows, and equivalent of them for GCC-compatible environments. You can find samples of it online or on this line [What is Equivalent to getch() & getche() in Linux?
  2. Always flush the input buffer after you read your input to prevent any similar problems to happen.

The input functions check the input buffer, which you can find at 0xb8000000, and check the first input there. If the buffer is empty, they wait for the user to enter the input, otherwise, they check the first element in the buffer and then examine that to what they expect to read. If they succeed, they read it and remove it from buffer. Otherwise, they fail to give you your input and depending on the function, the result is different.

For Example, consider the following line:

scanf("%d %d %f", &a, &b &c);

and give the input as: a 2 4 The scanf will return 0, which means it reads zero inputs so 'a', 2, and 4 remains in your buffer. So your buffer looks like: [a, 2, 4]. As a result if you add the following line: scanf("%c", &ch); scanf will try to get a character from the buffer, and it reads character 'a' and put it in variable ch. So it doesn't get any input from user. And you end up with having 2 and 4 on your buffer again.

Community
  • 1
  • 1
manman
  • 4,743
  • 3
  • 30
  • 42
  • 1
    If you notice the following line in his output, the second yes is his input. `please enter yes or no yes` I tested the app before posting my answer just to make sure. – manman Jan 04 '14 at 02:11
  • This will not work if a user input element as `abcd`. – haccks Jan 04 '14 at 02:13
  • `getchar` is not a good function for just getting an integer. Better solution for windows users is `getche` and for unix/linux, I write my own `getch` and `getche` methods according http://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux – manman Jan 04 '14 at 02:24
  • @haccks tried to give a more complete answer and possible solutions according to what I have done. Feel free to update the answer to write your suggestions and idea – manman Jan 04 '14 at 02:41
  • Good. You explained well :). +1 – haccks Jan 04 '14 at 02:45
0

Take care mixing scanf() format specifiers "%c", "%s" and "%[]".

Correct usage of "%[^\n]": there is no s. If leading whitespace in not wanted to be saved, include a leading space as in " %[^\n]"

char option[100];
// scanf("%s[^\n]", option);
scanf(" %[^\n]", option);
// or better
scanf(" %99[^\n]", option);
// or pedantic
switch (scanf(" %99[^\n]", option)) {
   case EOF: HandleEOForIOError(); break;
   case 0: HandleNoData(); break;  // might not be possible here.
   case 1: HandleSuccess();

Correct usage of "%c". If leading whitespace in not wanted to be save, include a leading space as in " %c". This may be the case in OP's code so the preceding inputs Enter or '\n' is consumed.

char elem;
scanf(" %c", &elem);

Correct usage of "%s". Leading whitespace is not saved with or without a leading space.

char option[100];
scanf("%99s", option);
// Following works the same.
scanf(" %99s", option);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256