Search code examples
cgetchar

getchar() not returning or continuing


I have part of a function declared like this:

char keystroke;
printf("\n(a)dd, (d)elete, or (m)odify (use - and + to navigate left and right)");
keystroke = getchar();
switch (keystroke){...

for whatever reason, I cannot continue into the switch statement. The output looks like this:

(a)dd, (d)elete, or (m)odify (use - and + to navigate left and right)eginning at (5,6)
s
s
s
a
a
a
f

where the chars after are my attempted input. gdb says No symbol "keystroke" in current context so I guess it is not being recorded for some reason? Any idea why?

Edit:

Here is the function in question. It's a bit long and involved, and uses a few global variables.

struct inputlist{
 int coords[2];
 struct inputlist *next;
};
input *head;

typedef struct inputlist input;

void inpt()
{
  input *temp=head;
  input *temp2;
  char keystroke;
  int cursor = 0;
  int counter = 0;
  int i;
  int impx[2]= {0};
  int impy[2]= {0};
  int end = 0;

  if(temp)
    while(temp->next)
      temp = temp->next;

  while(1)
  {
    printf("Please enter the x coordinate or s to stop.");
    if(!scanf("%d",impx))
      break;
    if((!impx[0]<=0)&&(!impx[0]>=7))
    {
      printf("Please enter a number: [0,7]");
      continue;
    }
    printf("Please enter the y coordinate or s to stop.");
    if(!scanf("%d",impy))
      break;
    if((!impy[0]<=0)&&(!impy[0]>=7))
    {
      printf("Please enter a number: [0,7]");
      continue;
    }

    temp2 = malloc(sizeof(input));
    if(!temp2)
      exit(2);
    if(!head)
      head = temp2;
    temp2->next = NULL;
    if(temp)
      temp->next = temp2;
    else
      head = temp = temp2;
    temp2->coords[0] = impx[0];
    temp2->coords[1] = impy[0];
    }
    if(!head)
     exit(0);
    temp = head;


    while(!end)
    {
      int is_correct = 0;
      counter = 0;
//    printf("\033[");
      while(temp->next)
      {
        if(counter++==cursor)
          printf("*");
        else
          printf(" ");
        printf("(%d,%d)",temp->coords[0],temp->coords[1]);
        temp = temp->next;
      }

      printf("\n(a)dd, (d)elete, or (m)odify (use - and + to navigate left and right)");
      keystroke = getchar();
      switch (keystroke)
      {
        case 'a':
          while(temp)
          temp=temp->next;
          do
          {
            scanf(" %d",impx);
            scanf(" %d",impy);
            if ((impx[0]<=7)&&(impx[0]>=0)&&(impy[0]<=7)&&(impy>=0))
            {
              temp->next = malloc(sizeof(input));
              temp = temp->next;
              temp->next = NULL;
              temp->coords[0] = impx[0];
              temp->coords[1] = impy[0];
              is_correct = 1;
             }
           } while(!is_correct);
           break;
         case 'd':
           temp = head;
           for(i = 0; i<cursor-1;i++)
           temp = temp->next;
           temp2= temp->next;
           temp->next = temp2->next;
           free(temp2);
           break;
         case 'm':
         temp = head;
         for(i=0; i<cursor;i++)
           temp = temp->next;
         do
         {
           scanf(" %d",impx);
           scanf(" %d",impy);
           if ((impx[0]<=7)&&(impx[0]>=0)&&(impy[0]<=7)&&(impy>=0))
           {
             temp->coords[0] = impx[0];
             temp->coords[1] = impy[0];
             is_correct = 1;
           }
         } while(!is_correct);
         break;
         case '+':
         case '='://because who wants to hit shift? not me
         temp = head;
         for(i=0;i<cursor;i++)
           temp = temp->next;
         if(temp->next)
           cursor++;
         break;
         case '-':
         case '_':
         if(cursor)
           cursor--;
         break;
         default: 
           end = 1;
           break;
       }
     }
   }

Solution

    1. After jump out from the while(1) loop, there are some failed-match input in the input buffer, you need to consume them before you enter the while (!end) loop. You can do this by something like:

      while ((keystroke = getchar()) != '\n') ;
      

      If you did not consum them, the getchar() in the while(!end) will read from them.

    2. In the while(!end) loop, if you input a or m, you need to input two intergers, without any prompt, you should add some.