Thread: something wrong with code

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    something wrong with code

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        cout << "Hi! How old are you?\n";
        int age;
        cin>> age;
        
           
        if (13>age>19) {cout << "Well, your' not a teenager, thats for sure.\n";}
        else if (13<=age<=19)
        
        { cout << "You are a teenager.\n";} 
        
        cin.ignore();
        cin.get();
    
        return 0;
    }
    Whats wrong? I get the message "your a teenager" no matter what number I type.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    if (13>age>19){
    This syntax doesn't work as you thing it does. First it does this 13>age, which returns true if the number is under 13. Then this will be done true>19 or false>19. Because true equals to 1 and false to 0, then the whole thing will always return false.
    And the same way the second comprision always returns true.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use the boolean && to combine expressions in an if.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Code:
    if (13>age && age>19) {cout << "Well, your' not a teenager, thats for sure.\n";}
        else if (13<=age && age<=19)
    Obviously this is wrong cause it didnt work but whats wrong.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Think about what you wrote. I'll type it in english: If 13 is greater than the age and the age is greater than 19 then print out "Not a teenager."

    In that case maybe && isn't appropriate? The other option is || (hopefully you have learned that && means "and" and || means "or").

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Ohhhh now I see Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM