3

I am having a problem with the Enter key or character in the stdin stream messing up following input calls.

let's say I have one input call, so I enter in the stuff. but then takes the Enter key as input for the next input call.

I think in c++ there is cin.ignore() to do the trick.

I just can't find the C version.

The input methods are getchar() and gets().

Sorry if this is a duplicate. I couldn't find the question that matches mine. thanks for any help!

        printf("Do you want to view the lines? ");
    int choice = getchar();
    while (choice == 'y')
    {
            char line[80];
            printf("What line do you want to see? ");
            gets(line);
            if (line != "all")
            {
                    n = atoi(line);
                    printf("Line %d: %s\n",n,list[n]);
            }
            else
                    for (int i = 0; i<size; i++)
                            printf("%s \n",list[i]);
            printf("Any more lines? ");
            choice = getchar();
    }

I admit that this is extremely basic, but still learning .

Akshay Patil
  • 954
  • 1
  • 12
  • 28
Alex
  • 503
  • 2
  • 6
  • 14
  • Can you write a short example program that demonstrates the problem, and add it to your question? – steveha Feb 04 '13 at 05:49
  • i have edited it steveha – Alex Feb 04 '13 at 05:59
  • possible duplicate of [Program doesn't wait for user input with scanf("%c",&yn);](http://stackoverflow.com/questions/8464620/program-doesnt-wait-for-user-input-with-scanfc-yn) – Alok Save Feb 04 '13 at 06:08
  • 2
    Alok Save, you shouldnt flag peoples questions without reading it. I dont have the scanf function anywhere in the code. – Alex Feb 04 '13 at 07:01

1 Answers1

4

You simply need to keep calling getchar to consume the characters you don't want from the stream. If you know there's always a single additional character then it is as simple as making one additional call to getchar.

If you want to remove multiple characters from the stream or deal with situations where the input may actually contain something you really need you can do something like the code below instead of your choice = getchar().

do
{
  choice = getchar();
} while(choice=='\n'); // Add any other characters you may want to skip

This will keep removing characters (in this case only if they are newline) but leave choice set to the first non-removed character.

By the way, you can't compare strings like that (!= "all") use the C string compare functions, e.g. strcmp().

More generically the C++ istream ignore can be roughly written in C as something like the code below; call with stdin to skip characters from the standard input:

int ignore(FILE *stream, int n, int delim)
{
  int c;
  while(n>0)
  {
    c = getc(stream);
    if(c==EOF || c==delim)
    {
      return c;
    }
    n--;
  }
  return(c);
} 
Guy Sirton
  • 8,331
  • 2
  • 26
  • 36
  • what is the delim variable for? – Alex Feb 04 '13 at 07:03
  • @Alex: It's the same as in the C++ `ignore` function you mentioned ( http://www.cplusplus.com/reference/istream/istream/ignore/ ). `ignore` by default skips 1 character and delim is EOF. The tl;dr of all this is keep calling getchar() if you get something you don't want. If you simply call getchar() you will pull a single character from the input stream. – Guy Sirton Feb 04 '13 at 07:06
  • @Alex: I've added some more explanations at a level that I hope is suitable for a beginner. Let me know if there's something that isn't clear to you. – Guy Sirton Feb 04 '13 at 07:19