0

When I execute and put in the response "YES" or "NO.", the program always outputs "Not a valid response." from the else{} statement.

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

int main()
{
    int response[3];
    char password[15];
    printf("Insert Password with 8 characters:");
    gets(password);
    printf("Your current password is:'%s',do you want to keep it?(YES or NO.):",password);
    gets(response);
    if (response == "YES") {
        printf("password stored 'not actually lol'\n");
    }
    else if (response == "NO.") {
        printf("we dont know what else you want to do.\n");
    }
    else {
        printf("Not a valid response.\n");
    }
    return 0;
}

It doesnt even work when the program doesnt take input from the user, I think there is a problem with the if statement but I'm not sure

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

int main()
{
    int response = "NO.";
    char password[15];
    printf("Insert Password with 8 characters:");
    gets(password);
    printf("Your current password is:'%s',do you want to keep it?(YES or NO.)\n",password);
    //gets(response);
    if (response == "YES") {
        printf("password stored 'not actually lol'\n");
    }
    else if (response == "NO.") {
        printf("we dont know what else you want to do.\n");
    }
    else {
        printf("Not a valid response.\n");
    }
    return 0;
}
yonx
  • 5
  • 2
  • 1
    `int response = "NO.";`? The compiler isn't complaining about that? How can you store a string in an integer variable? – Some programmer dude Oct 29 '22 at 21:31
  • 1
    And never ***ever*** use `gets`! It's so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that it was removed from the C language in 2011, and was marked as obsolete in 1999. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. And any learning or teaching that tells you to use `gets` should be viewed as highly suspicious. – Some programmer dude Oct 29 '22 at 21:33
  • 2
    Oh, and `response == "YES"`? That condition will *never* be true (same with the `"NO."` comparison). That's just not how to compare strings in C. What resources *are* you using to learn C, if it teaches you to use both `gets` and invalid string comparisons? – Some programmer dude Oct 29 '22 at 21:34
  • Look up `strcmp` – Ed Heal Oct 29 '22 at 21:35
  • thanks for pointing that out, i seriously need to do a recap. – yonx Oct 29 '22 at 21:40

1 Answers1

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

int main()
{
    // You used response[3] sized response which stores 2 characters the user gives, and one character as NULL, hence, it is always suggested to used required size + 1 for characters
    // Also, "NO." is a char array and not an int array
    char response[4];
    char password[15];
    printf("Insert Password with 8 characters:");
    gets(password);
    printf("Your current password is:'%s',do you want to keep it?(YES or NO.):",password);
    gets(response);
    // String comparison is done using strcmp(s1, s2) and not s1 == s2
    // If two strings are equal, it returns 0, else 1 or -1
    if (strcmp(response, "YES") == 0) {
        printf("password stored 'not actually lol'\n");
    }
    else if (strcmp(response, "NO.") == 0) {
        printf("we dont know what else you want to do.\n");
    }
    else {
        printf("Not a valid response.\n");
    }
    return 0;
}

Also, please read these web pages for a better understanding of why not to use gets() or scanf()

strcmp usage | How to properly take input in C

keidakida
  • 713
  • 4
  • 12