Thread: simple If statement not working

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    8

    simple If statement not working

    I am trying to get the user to enter one of four letters, either D, B, A, or O. Then I am using an if statement to check if the user entered an invalid letter. If the user enters one of the four letters above, then nothing should happen. If they enter something else, then it should ask them to "enter a valid letter". I tried to code this, and the problem is no matter what I enter it pops up with "enter a valid letter". Can anyone tell me what I'm doing wrong? here is my code..

    Code:
     
    
    int main() {
    
    
    char letter;
    
    
    
    
    printf("\n\nEnter a letter ('D','A','B',or 'O'): ");
    scanf("%c", &letter);
    
    
    if (letter != 'D' || letter != 'A' || letter != 'B' || letter != 'O') {              
            printf("Please enter a valid letter: \n"); 
            scanf("%c", &letter);  
     }
    
    
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    %c, unlike his friend %d, does not eat the newline character (because it as a character).
    As a result when you press the key F, then you hit enter too.
    So, the 1st scanf will read the F and the 2nd one the enter.
    The solution is to put a space just before %c. This makes scanf to eat the newline automatically.

    I suggest you use a do - while loop, since no matter what you will ask for an input at first.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char input;
        do{
              printf("Please input\n");
              scanf(" %c", &input);
        }while(....);
      
        return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    if (letter != 'D' || letter != 'A' || letter != 'B' || letter != 'O')
    Your logic is flawed, you need to use && instead of || in your if statement.

  4. #4
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    yeahh camelman was right u must using if && what it means AND,
    AND means that the if variable integrated
    || means OR
    OR means that the if variable is choice, think about that logic

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by loserone+_+ View Post
    yeahh camelman was right u must using if && what it means AND,
    AND means that the if variable integrated
    || means OR
    OR means that the if variable is choice, think about that logic
    Hmm... I always pictured it as AND being multiplication and OR being addition (testing for a non-zero result)

    i.e.
    All variables need to be 1 (or true) for the result to be non-zero in the following
    a*b*c
    or
    a && b && c

    And, only one of the variables need to be non-zero for the following to be non-zero
    a+b+c
    or
    a || b || c

    I don't know what you mean by "variable integrated"
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Hmm... I always pictured it as AND being multiplication and OR being addition (testing for a non-zero result)
    Indeed. The boolean symbol for OR is a plus, and for AND is a dot (which is one way of writing multiplication), which supports this notion.
    List of logic symbols - Wikipedia, the free encyclopedia

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    This is just the way I picture it, so that it makes sense to me.

    Please note that Boolean algebra is very different to conventional algebra and "AND" shouldn't be taken as literately multiplication, nor "OR" as literally addition.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if statement not working Properly
    By noname88 in forum C Programming
    Replies: 11
    Last Post: 09-29-2011, 08:44 PM
  2. The following if statement is not working!...
    By darkmagic in forum C Programming
    Replies: 5
    Last Post: 06-23-2010, 08:11 AM
  3. If/else statement not working
    By zenovy in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2006, 08:26 PM
  4. If statement isnt working
    By Extropian in forum C Programming
    Replies: 4
    Last Post: 08-08-2005, 09:21 AM
  5. If statement not working!
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 03-02-2002, 02:45 AM