0
#include <stdio.h>

int main()
{

    char apple[]="Apple";
    char banana[]="Banana";
    char orange[]="Orange";

    printf("Choose one of the below options\n\n");
    printf("Which fruit do you like the most: Apple, Banana, Orange\n\n");
    scanf("%s",&apple,&banana,&orange);
    if("%s", apple)
    {
        printf("You chose Apple.\n");
    }
    if("%s",banana)
    {
        printf("You chose banana.\n");
    }
}

// I want the code to simply print on screen the choice i chose. But when i run the code it prints both Apple and Banana. If i type Apple i do not want it to print Banana. Do i need to use else statement with this? or what else am i missing? Thank you i am very new to c programming.

3
  • 1
    Please format your question correctly.. Commented Aug 19, 2016 at 11:28
  • So many things wrong here. "I am very new" does not mean you are automatically excused from learning the very basics first. Commented Aug 19, 2016 at 13:04
  • And what should a person do if it is the very basics that a person needs help with? Does the person has no right to seek help until the basics are solid? Commented Aug 19, 2016 at 16:47

1 Answer 1

1

You need to use strcmp() to compare between strings. see the following code.

strcmp() will return 0 if the contents of both strings are equal.

strcmp() will return <0 if the first character that does not match has a lower value in ptr1 than in ptr2.

strcmp() will return >0 if the first character that does not match has a greater value in ptr1 than in ptr2.

reference

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

int main()
{

    char apple[]="Apple";
    char banana[]="Banana";
    char orange[]="Orange";
    char input[100];

    printf("Choose one of the below options\n\n");
    printf("Which fruit do you like the most: Apple, Banana, Orange\n\n");
    scanf("%s",input);
    if(strcmp(input,apple)==0)
    {
        printf("You chose Apple.\n");
    }
    if(strcmp(input,banana)==0)
    {
        printf("You chose banana.\n");
    }
    if(strcmp(input,orange)==0)
    {
        printf("You chose orange.\n");
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for you reply, the strcmp is very new to me but i will study more into it.
You can check out the reference ! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.