c - getchar() 不返回或继续

标签 c getchar

我有一部分函数声明如下:

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

无论出于何种原因,我都无法继续进入 switch 语句。输出如下所示:

(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

后面的字符是我尝试输入的。 gdb 说 No symbol "keystroke"in current context 所以我猜它没有被记录是出于某种原因?知道为什么吗?

编辑:

这是有问题的函数。它有点长且复杂,并且使用了一些全局变量。

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;
       }
     }
   }

最佳答案

  1. 跳出while(1)循环后,输入缓冲区中有一些匹配失败的输入,需要在进入while ( !end) 循环。您可以通过以下方式执行此操作:

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

    如果您没有使用它们,while(!end) 中的 getchar() 将从它们读取。

  2. while(!end)循环中,如果输入am,需要输入两个整数,没有任何提示,你应该添加一些。

关于c - getchar() 不返回或继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21982688/

相关文章:

c - 是否有类似 "man"的命令来列出结构成员?

c - 如何在循环中连续打印变量并在 linux 下的 C 中按一下转义键终止?

c - 如何读取文本文件的内容并返回我指定的两个字符串之间的字符串?

c - 写入相同值的竞争条件是否安全?

c++ - 这可以用 OpenGL 完成吗?

c - 文件路径长度有多重要?

C 用户输入验证 - 只需要一个转换为 int 的字符

c - 这个C函数有什么问题? (printf() 和 getchar())

c - 为什么我的第二个 printf 没有按预期调用?

c - 为什么getchar让单字符输出函数显示字符串?