简体   繁体   中英

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".

The problem is subtle. Read the documentation for fgets . Then look at the value of name in the debugger after the call to fgets .

fgets ends the input when it sees a newline character, but it includes the newline character in the result. So name ends up with the character string "end\\n" ; comparing that to "end" will fail.

If you're going to use fgets you have to allow for that newline character, and compare with "end\\n" .

If this is C code, use scanf . If this is C++ code, use std::cin with a stream extractor.

That's caused by your input function fgets . Use cin instead and you will get your expected output " bye " once you input " end ".

  #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 will compare all characters in your strings until a terminating null-character is reached or characters differ. strncmp will only compare the first n values in both strings. Also you should add break after printf("bye") to leave the loop.

#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. If you compare with "end\\n" it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM