gpt4 book ai didi

无法正确输入

转载 作者:太空宇宙 更新时间:2023-11-04 02:26:27 26 4
gpt4 key购买 nike

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

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

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

最佳答案

C 中的经典错误。

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

最后一个=应该是==

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

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

|| 应该是&&

还有一件事:由于您的逻辑,您正在失去您的第一个角色。我会把它留给你解决。 :-)

关于无法正确输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50392040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com