0

I just started learning programming couple of days ago, and I made this for fun but it doesn't work on if and else with string.. what should I fix?

#include <stdio.h>
#include <cs50.h>

int main(void)
{

    printf("What is your name?\n");
    string name = get_string();

    printf("Sorry, I don't know who the heck %s is. How can I help you?\n",name);
    string request = get_string();

    printf("Well, In order to %s, I need your name.What is your name again?\n",request);
    string nickname = get_string();

    printf("You can do it yourself. You got working arms and legs, %s. Bye. \n", nickname);

    printf("Self Destruction... y/n?\n");
    scanf("%s", "y", "n", "yes", "no");

    if ("y" or "yes")
    printf("Boom\n");

     else if ("n" or "no")
    printf("Wow, you saved me. What a coward.\n");

        else
        printf("Wow, you can't even type.");


}
0

Several basic errors here.

 scanf("%s", "y", "n", "yes", "no");

This doesn't work. The first parameter, "%s" is an input string that basically says to accept a string and store in the second parameter. The %s is the placeholder. Next, the second parameter needs to be a string variable to store the data in. You don't have one there. "y" is simply a string constant. All that follows is more of the same.

If your intent was to validate the input as one of those 4 strings, then you might want to try first storing the string in a var, and compare it to these 4 strings. I'll leave it to you to figure out how. (Think while loop too.)

Also, you can't use or as an operator. The or operator is ||.

Kudos for experimenting and seeing what works! Happy coding!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .