Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

In the GDB editor have to press <Enter> to input data, why is that?

+1 vote
asked Sep 25, 2020 by Patrick Noonan (130 points)
I am trying to make this program continuously loop to check left '1' or right '2', but the GDB online editor is forcing the user to hit <Enter> to actually process the data.  This is for the C compiler.  Is there a way to avoid having to hit <Enter> to feed input into the stdin interface?

---> Program Below

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

int
main ()
{

  char a;
  char c;

  int i = 0;
  printf
    ("Press '1' for left and '2' for right - <space> to quit\n");
  do
    {
      a = getchar ();
      do
    {
      c = getchar ();
    }
      while (c != '\n' && c != EOF);

      switch (a)
    {
    case 49:
      printf ("left\n");
      break;

    case 50:
      printf ("right\n");
      break;

    default:
      printf ("nothing\n");
      break;
    }

    }
  while (a != 32);

  printf ("User has ended program\n");
  return 0;
}

1 Answer

+1 vote
answered Sep 28, 2020 by Peter Minarik (86,040 points)

getchar()Get character from stdin

So, it does not monitor your key presses, it monitors the standard input. The standard input is committed when you press ENTER. Before that you can edit it (BACKSPACE, DELETE, move in the line with HOME, END, and arrows).

That's why you have to press an ENTER.

If you're after a key pressed functionality, there's no platform independent one. For OnlineGDB (and linux) you have to check ncurses.

Have a look at this.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...