Thread: IF else statement help

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    1

    IF else statement help

    I need help with the following program
    Something is wrong and whatever i type, the output is always "You answer is incorrect!"

    Code:
    #include <stdio.h>int main()
    
    
    {
        /*quiz*/
        char ANS[100];
    
    
        printf("What is the name of the President of the United States?\n");
        scanf("%s", ANS);
    
    
        if(ANS == "Donald")
        {
            printf("Your answer is correct!");
        }
        else
        {
            printf("Your answer is incorrect!");
        }
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You need to use strcmp() to compare strings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    2
    Your mistake is that ;
    You cant use ANS = "String" with this way . You must try like that
    if (ANS[0] == 'D' && Donald[0] == 'o' && .... )

    or you should use strcompare .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  2. [Help]Or statement
    By Mikri Maus in forum C Programming
    Replies: 3
    Last Post: 10-13-2013, 05:35 AM
  3. Help With If/Else If Statement
    By ThePhoenixRisin in forum C Programming
    Replies: 2
    Last Post: 11-05-2012, 04:40 AM
  4. While statement
    By Mentallic in forum C Programming
    Replies: 5
    Last Post: 10-12-2012, 06:26 AM
  5. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM

Tags for this Thread