0

I have a problem with my code, I want the code to check if the name is equal to the realname and print what's inside of the if statement, otherwise, to print what's inside of the else statement, at the end of the code it says what it throws.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    char name[5];
    char realname[] = "Mike";

    printf("Please, enter your name: ");
    fgets(name, sizeof(name), stdin);
    scanf(name, realname);

    if (strcmp(name, realname))
    {
        printf("Nice to meet you %s ", name );
        printf("I'm known as robot. ");
        printf("And welcome to starting.com");
    }
    else
    {
        printf("You're not the owner of this account.");
    }

}
//If i type "john" it returns this.
//Please, enter your name: john
//Nice to meet you john I'm known as machinecode. And welcome to starting.com
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 2
    `if (strcmp(name, realname))` --> `if (strcmp(name, realname) == 0)` – Support Ukraine Jun 20 '21 at 05:53
  • 2
    `scanf(name, realname);` hmm... what do you expect this to do? – Support Ukraine Jun 20 '21 at 05:55
  • I think you meant `sscanf` instead of `scanf`. Actually, I'm not sure why you would even want to do any scanf after the previous `fgets`. Seems kinda redundant. – smac89 Jun 20 '21 at 05:56
  • @smac89: Well, ``fscanf`` followed by `sscanf` can be used for some lightweight data validation and parsing.  At a minimum, it could be useful in this program to strip leading and trailing whitespace. But the OP isn’t close to using it correctly. – Scott - Слава Україні Jun 20 '21 at 06:39
  • `char name[5]` is OK if someone is called "Mike", "Bob" or "Eve". But not if they are called "Miranda Veracruz de la Jolla Cardinal". – HAL9000 Jun 20 '21 at 15:06

2 Answers2

3

strcmp returns 0 when both arguments passed to it are equal, and 0 is a "false" value in C. In short, you have your condition reversed. The most readable way to write it, IMHO, is to explicitly compare the result of strcmp to 0:

if (strcmp(name, realname) == 0)
{
    printf("Nice to meet you %s ", name );
    printf("I'm known as robot. ");
    printf("And welcome to starting.com");
}
else
{
    printf("You're not the owner of this account.");
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

You have (at least) 3 bugs/misunderstandings in your code.

  1. strcmp return value

    It seems you expect strcmp to return something like "TRUE" if the strings are equal. But that's not how strcmp works. If two strings are equal it returns zero. Zero in an if controlling expression is like "FALSE", i.e. the substatement won't be executed. You need to compare the return value to zero, like if (strcmp(name, realname) == 0)

  2. scanf usages

    This part scanf(name, realname); is strange. I'm not sure what you expect it to do but as it is, the makes no sense. Delete the line.

  3. fgets usages

    fgets reads the std input including the newline character. So before comparing strings, you need to remove that newline character. However, if the provided buffer is too small to hold the typed input, fgets will only store BUFFER-SIZE-1 characters and there will be no newline. You need to handle that case as well. See this question: Removing trailing newline character from fgets() input

So, your code could be:

int main()
{
    char name[5];
    char realname[] = "Mike";

    printf("Please, enter your name: ");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = 0;

    if (strcmp(name, realname) == 0)
    {
        printf("Nice to meet you %s ", name );
        printf("I'm known as robot. ");
        printf("And welcome to starting.com");
    }
    else
    {
        printf("You're not the owner of this account.");
    }

}
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • 4th bug - name is too short. Needs room for newline and null. – stark Jun 20 '21 at 11:13
  • @stark nice catch but... If the input is "Mike", `fgets` will actually store "Mike\0" and skip the newline. So it's actually possible to get a match even with `char name[5];` – Support Ukraine Jun 20 '21 at 14:19
  • It will not skip the newline. It will still be in the input stream. – stark Jun 21 '21 at 00:27
  • @stark true.. By "skip" I wanted to say that the newline would not be copied into the buffer. Therefore it will be possible to get an input that match `realname`. Anyway - I agree that `name` is too short for any practical use. – Support Ukraine Jun 21 '21 at 05:52