[英]Why does my code break at the first if statement?
#include <stdio.h>
#include <ctype.h>
#include<string.h>
int main (void)
{
char *names;
int capacity = 0;
int size = 0;
char name[100];
printf ("Enter number 4 is you want to stop inputting names.\n");
while (1)
{
printf ("Input:\n");
fgets(name, sizeof(name),stdin);
printf ("%s", name);
if (strcmp(name, "end")!= 0)
{
printf ("hello");
}
if (strcmp (name, "end")== 0)
{
printf("bye ");
}
}
}
I am trying to keep looping to get the user input and break out of the loop when the user enter a certain character or word.我试图保持循环以获取用户输入并在用户输入某个字符或单词时跳出循环。 But when I input "end" I expected the output to be "bye" but the output is "hello".
但是当我输入“end”时,我希望输出是“bye”,但输出是“hello”。
The problem is subtle.问题很微妙。 Read the documentation for
fgets
.阅读
fgets
的文档。 Then look at the value of name
in the debugger after the call to fgets
.然后在调用
fgets
后查看调试器中name
的值。
fgets
ends the input when it sees a newline character, but it includes the newline character in the result. fgets
在看到换行符时结束输入,但它在结果中包含换行符。 So name
ends up with the character string "end\\n"
;所以
name
以字符串"end\\n"
; comparing that to "end"
will fail.将其与
"end"
进行比较将失败。
If you're going to use fgets
you have to allow for that newline character, and compare with "end\\n"
.如果要使用
fgets
,则必须允许使用该换行符,并与"end\\n"
进行比较。
If this is C code, use scanf
.如果这是 C 代码,请使用
scanf
。 If this is C++ code, use std::cin
with a stream extractor.如果这是 C++ 代码,
std::cin
与流提取器一起使用。
That's caused by your input function fgets
.这是由您的输入函数
fgets
引起的。 Use cin
instead and you will get your expected output " bye " once you input " end ".改用
cin
,一旦输入“ end ”,您将获得预期的输出“ bye ”。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
using namespace std;
int main (void) {
char *names;
int capacity = 0;
int size = 0;
char name[100];
printf ("Enter number 4 is you want to stop inputting names.\n");
while (1)
{
printf ("Input:\n");
cin >> name;
printf ("%s", name);
if (strcmp(name, "end")!= 0)
{
printf ("hello");
}
else if (strcmp (name, "end")== 0)
{
printf("bye ");
}
}
}
Instead of strcmp
use strncmp
.而不是
strcmp
使用strncmp
。 strcmp
will compare all characters in your strings until a terminating null-character is reached or characters differ. strcmp
将比较字符串中的所有字符,直到达到终止空字符或字符不同。 strncmp
will only compare the first n values in both strings. strncmp
只会比较两个字符串中的前 n 个值。 Also you should add break
after printf("bye")
to leave the loop.您还应该在
printf("bye")
之后添加break
以离开循环。
#include <stdio.h>
#include <ctype.h>
#include<string.h>
int main (void)
{
char *names;
int capacity = 0;
int size = 0;
char name[100];
printf ("Enter number 4 is you want to stop inputting names.\n");
while (1)
{
printf ("Input:\n");
fgets(name, sizeof(name),stdin);
printf ("%s", name);
if (strncmp(name, "end", 3)!= 0)
{
printf ("hello");
}
if (strncmp (name, "end", 3)== 0)
{
printf("bye ");
break;
}
}
}
As others already mention: fgets()
stores the end-of-line character \\n
, too.正如其他人已经提到的:
fgets()
存储行尾字符\\n
。 If you compare with "end\\n"
it will work.如果您与
"end\\n"
进行比较,它将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.