scanf()和for循环不合作

scanf()和for循环不合作,c,for-loop,scanf,C,For Loop,Scanf,我正在编写一个名为GetPattern()的函数,它将在我的main()函数中使用 int main(void) { int attempt=0, option=-1; char pattern[SIZE+1], replacement[SIZE+1]; char name[20]; FILE *in, *out; printf("Enter the pattern to find:"); GetPattern(pattern); out = CreateF

我正在编写一个名为
GetPattern()
的函数,它将在我的
main()
函数中使用

int main(void)
{
  int attempt=0, option=-1;
  char pattern[SIZE+1], replacement[SIZE+1];
  char name[20];    
  FILE *in, *out;

  printf("Enter the pattern to find:");
  GetPattern(pattern);
  out = CreateFile();
  Find(in, pattern, out);
  fclose(in);
  fclose(out);

  return 0;
}
下面是我的
main()
如何使用
GetPattern()
函数的上下文

int main(void)
{
  int attempt=0, option=-1;
  char pattern[SIZE+1], replacement[SIZE+1];
  char name[20];    
  FILE *in, *out;

  printf("Enter the pattern to find:");
  GetPattern(pattern);
  out = CreateFile();
  Find(in, pattern, out);
  fclose(in);
  fclose(out);

  return 0;
}
下面是我的
GetPattern()
函数:

void GetPattern(char *tmp)
{
// prompt the user for the pattern to be found/replaced
// note: any character, including ' ', maybe be part of the pattern
// we assume that the pattern has no more than 20 characters. If the user
// enters more than 20 characters, only the first 20 will be used.

  int i;

  for (i = 0; i < 20; i++) // iterate 20 times
    {
      scanf(" %c", &tmp[i]);
      if (tmp[i] == '\n') // if user hits enter, break the loop
        {
          tmp[i] = '\0'; // insert '\0' at the end of the array
          break;
        }
      else
        tmp[i+1] = '\0'; // insert '\0' at the end of the array
    }

  printf("%s\n", tmp); // see what's in tmp[]

  return;
}
void GetPattern(char*tmp)
{
//提示用户查找/替换图案
//注意:包括“”在内的任何字符都可能是模式的一部分
//我们假设模式不超过20个字符
//输入超过20个字符,仅使用前20个字符。
int i;
对于(i=0;i<20;i++)//迭代20次
{
scanf(“%c”和tmp[i]);
if(tmp[i]='\n')//如果用户点击回车键,则中断循环
{
tmp[i]='\0';//在数组末尾插入'\0'
打破
}
其他的
tmp[i+1]='\0';//在数组末尾插入'\0'
}
printf(“%s\n”,tmp);//查看tmp中的内容[]
返回;
}
GetPattern()
函数本身工作;与
main()
函数分离,但当我将其放入
main
时,它只接受20个字符,不少于20个字符。即使我按ENTER键(即“\n”),循环也不会中断——它会继续运行

你觉得这段代码有什么明显的错误吗?

我认为使用
getc()
会对你有所帮助

for (i = 0; i < 20; i++) // iterate 20 times
{
    //scanf(" %c", &tmp[i]);
    tmp[i]=getc(stdin);/////

    if (tmp[i] == '\n') // if user hits enter, break the loop
    {
        tmp[i] = '\0'; // insert '\0' at the end of the array
        break;
    }
    else
        tmp[i+1] = '\0'; // insert '\0' at the end of the array
}
(i=0;i<20;i++)的
for//迭代20次
{
//scanf(“%c”和tmp[i]);
tmp[i]=getc(stdin)/////
if(tmp[i]='\n')//如果用户点击回车键,则中断循环
{
tmp[i]='\0';//在数组末尾插入'\0'
打破
}
其他的
tmp[i+1]='\0';//在数组末尾插入'\0'
}

“%c”中的空格
表示“跳过空白”。这包括新行。所以
tmp[i]
永远不会是
'\n'
.Ugh。你一定是在开玩笑。用这种语言是不可能获胜的。谢谢@user3386109。当用户单独点击
[Enter]
时,scanf无法读取字符,因此不会进行转换
scanf
坐在那里愉快地等待一个字符,不管按多少次
[Enter]
。输入的结尾用
[ctrl+d]
[ctrl+z]
在windows上)。好吧,不要通过一个半标准函数的动作来判断C语言。我已经用C语言编程30多年了,我从来没有使用过scanf等人,我一直使用fgets、fgetc、strtok、strtol、atoi等等。为什么?好吧,这只是我的观点,但当我第一次看scanf时,我认为它是恶性的:-)。而且,我觉得不使用scanf可以更好地处理更一般的情况。但是,其他人成功地使用了它--YMMV
scanf()。建议使用:fgets(tmp,20,stdin);因为这个命令将处理您想做的一切。但是,强烈建议不要使用“神奇”数字(如20),而是使用#define为该数字指定一个有意义的名称,然后在整个代码中使用该有意义的名称。