Thread: Help With If/Else If Statement

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

    Help With If/Else If Statement

    Well, I'm new to C, previously a programmer in PHP. Sorry if I'm making any obvious mistakes.

    Anyways, I'm writing a simple CLI program that answers to Y/N.

    If I type in Y, everything works as expected. However, when I type in N, it executes both the else if, and the else conditionals.

    (Y/N)?: N
    Wrong answer...
    (Y/N)?: Please type Y or N.
    (Y/N)?:
    When I type in something random, the else is executed for every character inputted. No idea why:

    (Y/N)?: asdf
    Please type Y or N.
    (Y/N)?: Please type Y or N.
    (Y/N)?: Please type Y or N.
    (Y/N)?: Please type Y or N.
    (Y/N)?: Please type Y or N.
    (Y/N)?:
    Here is the code:

    Code:
    
    
    #include <stdio.h>
    
    
    void yesno() {
    
    
        char choice;
    printf("(Y/N)?: ");
        scanf("%c", &choice);
    
        if (choice == 'Y') {
            printf("<333");
        }
        else if (choice == 'N') {
    printf("Wrong answer...\n");
            yesno();
        }
        else {
    printf("Please type Y or N.\n");
            yesno();
        }
    }
    
    
    int main()
    {
    //ask_question();
    
        yesno();
    
        fflush(stdin);
        getchar();
    
        return 0;
    }
    Again, sorry if I did something really stupid or obvious.

    Thanks tons!

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    scanf(" %c".....)<----- add the white space before the %c when scanning for a character. You are using recursion in your code anytime you call a function within itself. If you want to keep displaying a certain prompt why not put it in a loop instead? Reason for your code outputting multiple lines after you enter something like "asdf" is because "asdf" gets put in the buffer. Anytime you scan for a character after that, scanf will look into the buffer and grab the next character that was left in it.
    Last edited by camel-man; 11-04-2012 at 10:29 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    fflush(stdin);
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What else could I use instead of this IF statement?
    By dsured in forum C Programming
    Replies: 7
    Last Post: 02-01-2011, 11:10 PM
  2. OR statement in C
    By Keylac in forum C Programming
    Replies: 8
    Last Post: 11-26-2009, 09:09 AM
  3. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  4. what does this statement do?
    By timhxf in forum C Programming
    Replies: 8
    Last Post: 04-29-2007, 02:51 AM
  5. if statement
    By Joe100 in forum Game Programming
    Replies: 1
    Last Post: 12-07-2003, 05:57 AM