2015-05-29 56 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