Thread: Noob question

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    2

    Noob question

    I know this is a very simple problem but I cant figure it out. I have been doing the tutorials and trying to learn but I got stuck on the IF... I know, kind of sad.
    This is my code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int age;
    
      cout<<"How old are you? ";
      cin>> age;
      cin.ignore();
      if ( age < 50 ) {
           cout<<"You are under 50! I know, I'm amazing!\n";
           }
      else if ( age == 14 || 15) {
           cout<<"Blah/n";
           }  
      else if ( age == 100 ) {
           cout<<"You're older than my grandmother!\n";
           }
      else {
        cout<<"You're really old\n"; 
        }
      cin.get();
    }
    Everything works except if age equals 15 or 14. If age equals 15 or 14 it still executes if ( age < 50 ) {....
    Can you tell me whats wrong. Please.

    -Thx

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    When you are entering 14 or 15 it is still less than 50 so it executes that.

    Now find a way to fix that. =)

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    else if ( age == 14 || 15) {
    
    //should be...
    else if ( age == 14 || age == 15) {
    A better way to write it.
    Code:
      if( age < 15 ) {
          cout<<"Blah/n";
      }
      else if( age < 50 ) {
        cout<<"You are under 50! I know, I'm amazing!\n";
      }  
      else if ( age == 100 ) {
        cout<<"You're older than my grandmother!\n";
      }
      else {
        cout<<"You're really old\n"; 
      }
    Last edited by sand_man; 05-01-2005 at 07:43 PM.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    2
    Quote Originally Posted by sand_man
    Code:
    else if ( age == 14 || 15) {
    
    //should be...
    else if ( age == 14 || age == 15) {
    A better way to write it.
    Code:
      if( age < 15 ) {
          cout<<"Blah/n";
      }
    Thank you for the help. That worked. But for what I was doing < 15 was to broad of a range.

  5. #5
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51
    To fix that, put your else-ifs in numerical order..

    (This is pseudocode cause im too lazy to type it in real code)
    instead of:

    if age < 40
    if age < 20
    if age < 30

    do:

    if age <20
    if age<30
    if age <40

    With the first way, if age is 2 it will execute the code for if the age is less than 40 and then skip the others. With the second, if it's 2, it will execute the 20 and then skip the others (desired result)
    nerds unite!

    I'm using windows XP.
    I'm using dev-C++ by bloodshed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. Very noob question :(.
    By GamerProduction in forum Tech Board
    Replies: 4
    Last Post: 04-14-2007, 05:40 AM
  5. Noob question ( little dos program )
    By Demon1s in forum C++ Programming
    Replies: 13
    Last Post: 04-04-2003, 09:28 PM