1

我想从用户那里得到输入,跳过空格,但由于某种原因,这段代码似乎不起作用!

 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #define  MAX 50

 char *gettline();

 int main()
 {
     char *input;
     for( ; ; )
     {/*here i try to check the input and output*/
         printf("#");
         input = gettline();
         printf("%s\n",input);
         free(input);     
      }
    return 0;
 }


char *gettline(){
  int c,i;
  char *input = malloc(sizeof(char)*MAX);

  while((input[0]=c=getchar())==' ' || c=='\t'))
       ;
  input[1]='\0';

  i=0;
  while((c=getchar()) != EOF || c!='\n')
  input[i++] = c;

  input[i]='\0';
  return input;
  }

输出打印'#'并获取我的字符串,但是,
由于某种原因,输出不会从输入打印字符串......任何帮助都会很棒!
提前致谢

4

1 回答 1

2

C中的经典错误。

while((input[0]=c=getchar())==' ' || (input[0]=c='\t'))

最后=应该是==

还有第二个问题(正如您在我研究它时发现的那样)。

while((c=getchar()) != EOF || c!='\n')

||应该&&是。

还有一件事:因为你的逻辑,你正在失去你的第一个角色。我把这个留给你解决。:-)

于 2018-05-17T13:10:04.453 回答