0

So far I've got the following:

#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <string.h>

int main (void)
{
    printf("Loading BES Command Prompt...\n");
    sleep(5);
    printf("> ");
    char *input;
    scanf("%s", input);
    printf("\n");
    if (input == "exit")
    {
        printf("Terminating BEScmd.c...\n");
        sleep(2);
        return 100;
    }
}

However I need help because it won't use printf("Terminating BEScmd.c...\n"); and the compiler says it aborts with the Exit Code "0", not "100" which is what I want it to stop with.

Can you please help me here?

  • Use [`strcmp()`](https://man7.org/linux/man-pages/man3/strcmp.3.html) to check for exact match. Use [`strstr()`](https://man7.org/linux/man-pages/man3/strstr.3.html) to check if the string contains the thing to check. – MikeCAT Jun 10 '21 at 20:49
  • You never assign `input` a value, so you pass garbage to `scanf`. Then you compare `input` to a pointer to a string literal constant. Two pointers will only be equal if they point to the same thing, so this tests if `input` points to a string literal constant, which it can't possibly do if it points to the input. – David Schwartz Jun 10 '21 at 21:05

1 Answers1

0

You can use strcmp(a,b); from string.h library.

Like this:

string a = "words";
string b = "words";

if (strcmp(a, b) == 0)
{
    printf("a and b match");
    // strcmp returns 0 if both strings match
}

else
{
    printf("a and b don't match");
    // strcmp returns anything else if the strings dont match
}