在 while 循环中输入密钥
Enter key in a while loop
#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;
}
在此代码中:如果我输入除 hello 之外的任何内容,循环将继续。不过输入回车键不会再循环,只会加一行。
我在某处读到,使用 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
格式字符串中的 space。 %s
格式说明符已经忽略前导白色 space。所以不用 " %s"
使用 "%s"
.
主要问题是word != "hello"
。这不是比较字符串的方式。你实际做的是比较 word
的地址和字符串常量 "hello"
的地址。要进行字符串比较,请使用 strcmp
。如果它 returns 0,字符串是相同的,所以你的 while
循环应该检查 non-zero:
while (strcmp(word,"hello")) {
务必#include <string.h>
获取strcmp
的声明。
最后,您需要初始化 word
以便初始字符串比较不会通过读取未初始化的数据调用未定义的行为:
char word[100] = {0};
很好地回答了 OP 最初的问题。
OP 现在使用 fgets(word, 100, stdin);
并输入 h e l l o 输入。 word[]
然后用 "hello\n"
填充,这不会通过 strcmp(word, "hello") != 0
。
解决方案:剥离最终 '\n'
。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 100
int main() {
char word[BUFFER_SIZE] = { 0 };
while (strcmp(word, "hello") != 0) {
system("clear");
printf("\nSay hello to me : ");
// insure buffered output is flushed
fflush(stdout);
// Avoid magic numbers, use `sizeof word`
// Test if input was received
if (fgets(word, sizeof word, stdin) == NULL) {
fprintf(stderr, "\nInput closed\n");
return 1;
}
// lop off potential trailing \n
word[strcspn(word, "\n")] = '[=10=]';
}
printf("congrats, you made it !\n");
return 0;
}
#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;
}
在此代码中:如果我输入除 hello 之外的任何内容,循环将继续。不过输入回车键不会再循环,只会加一行。
我在某处读到,使用 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
格式字符串中的 space。 %s
格式说明符已经忽略前导白色 space。所以不用 " %s"
使用 "%s"
.
主要问题是word != "hello"
。这不是比较字符串的方式。你实际做的是比较 word
的地址和字符串常量 "hello"
的地址。要进行字符串比较,请使用 strcmp
。如果它 returns 0,字符串是相同的,所以你的 while
循环应该检查 non-zero:
while (strcmp(word,"hello")) {
务必#include <string.h>
获取strcmp
的声明。
最后,您需要初始化 word
以便初始字符串比较不会通过读取未初始化的数据调用未定义的行为:
char word[100] = {0};
OP 现在使用 fgets(word, 100, stdin);
并输入 h e l l o 输入。 word[]
然后用 "hello\n"
填充,这不会通过 strcmp(word, "hello") != 0
。
解决方案:剥离最终 '\n'
。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 100
int main() {
char word[BUFFER_SIZE] = { 0 };
while (strcmp(word, "hello") != 0) {
system("clear");
printf("\nSay hello to me : ");
// insure buffered output is flushed
fflush(stdout);
// Avoid magic numbers, use `sizeof word`
// Test if input was received
if (fgets(word, sizeof word, stdin) == NULL) {
fprintf(stderr, "\nInput closed\n");
return 1;
}
// lop off potential trailing \n
word[strcspn(word, "\n")] = '[=10=]';
}
printf("congrats, you made it !\n");
return 0;
}