2015-05-29 54 views
-2

我减速字符从用户获得输入的字符串后,输出包括某些其它字符(多个)

char strs[3][200] = {'\0'}; 

的矩阵,然后尝试插入串只对于第一行

gets(strs[0]); 

然后尝试打印所有行

printf("1) "); 
puts(strs[0]); 
printf("2) "); 
puts(strs[1]); 
printf("3) "); 
puts(strs[2]); 

结果是

1) ☺me input from the user 
2) ☺ 
3) ☺ 

为什么结果中会出现“笑脸”?

+1

'gets()'是危险的。改用'fgets()'。 –

+2

尝试'char strs [3] [200] = {{'\ 0'}};'。如果这不起作用,则提供[MCVE](http://stackoverflow.com/help/mcve)。顺便说一句,你的输入和结果不匹配。 –

+0

从哪里得到第四个输出?它看起来你的源文件包含一些奇怪的字符。 –

回答

-1

看起来有些东西在初始化时出错了。

试试这个

char options[2][100]; 

    options[0][0]='t'; 
    options[0][1]='e';  
    options[0][2]='s'; 
    options[0][3]='t'; 
    options[0][4]='1'; 
    options[0][5]='\0'; 

     printf("1) "); 
     puts(options[0]); 

出认沽将是:

1) test1 
1

这工作

#include <stdio.h> 

int main() 
{ 
    char str[3][200]={{'\0'},{'\0'},{'\0'}}; 

    fgets(str[0], 200, stdin); 
    fgets(str[1], 200, stdin); 
    fgets(str[2], 200, stdin); 

    fputs(str[0], stdout); 
    fputs(str[1], stdout); 
    fputs(str[2], stdout); 


    return 0; 
} 

在你的代码哟你只初始化第一个元素/字符串。然后当你的字符串中有垃圾时。

+0

是的。我的错。在开始我的大脑之前需要咖啡:) – LPs

+0

没关系。它发生;-) –

+0

@Jongware我编辑 – LPs