#include <stdlib.h>
#include <stdio.h>
int main()
{
char word[100];
while (word != "hello") {
system("clear");
printf("\nSay hello to me : ");
scanf(" %s", word);
}
printf("congrats, you made it !");
return 0;
}
在此代碼中:如果我輸入了任何內容,但是你好,循環繼續。但是,輸入ENTER鍵不會再循環,只會添加一行。在while循環中輸入鍵
我讀了一些使用getchar()可能會有所幫助的地方,但我對C開發有點新鮮,而且我在這裏搜索了幾個小時如何使它工作。
編輯0:
刪除
while (word != "hello")
char word[100];
scanf(" %s", word);
新增
#include <string.h>
while (strcmp(word, "hello") != 0)
char word[100] = {0};
fgets(word, 6, stdin);
編輯1:
我想在我的代碼類似,包括那
fgets(word, 6, NULL);
但它給我一個分段錯誤。
**編輯2:**
正確的工作輸入:
fgets(word, 6, stdin);
所以它的工作,但增加超過6個字符的問題,如:
Say hello to me : hello from the inside
將會打印:
Say hello to me :
Say hello to me :
所以我只是修改這樣的功能:
fgets(word, 100, stdin);
但現在不會得到我任何輸入工作
每行樣式的一行再次:)我想知道它從哪裏來,如果它是如此醜陋? –
它看起來不像這個代碼將任何東西放入「單詞」中。你的scanf不應該使用word而不是mot嗎? mot也沒有定義。 – bruceg
scanf希望閱讀leas中的一個非空白字符。 – nsilent22