简体   繁体   English

为什么我的代码在第一个 if 语句处中断?

[英]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.

相关问题 为什么内联我的访问器会破坏我的代码? - Why does inlining my accessors break my code? 可重现:为什么在C中传递此对象会破坏我的代码? - Reproducible: Why does passing this object in C break my code? 为什么break语句会终止程序? - Why does break statement terminate program? 动态规划:当我将 if 语句分成两行时,为什么代码会失败? - Dynamic Programming: Why does the code fail when I break the if statement up into 2 lines? 为什么我的第一个if语句起作用,但是我的其他if语句不起作用 - Why does my first if statement work, but my else if statement after it doesn't work 为什么我的代码不运行 switch 语句? (链表) - Why does my code do not run the switch statement? (Linked List) 为什么我的代码首先执行后面的cout? - Why does my code perform the later cout first? 为什么虚拟功能会中断我的工作? - Why does a virtual function break my casting? 与组合相比,为什么私有继承会增加有人破坏我的代码的可能性? - Why does private inheritance increase the probability, as compared to composition, that someone will break my code? 为什么我的代码总是跳过我的 c++ 的 else 语句? - Why does my code keep skipping my else statement for c++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM