Thread: If else statements

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    10

    If else statements

    I am currently coding a small program to comment on your age, and I am having problems with if else statements.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
     int age;
    
     cout<<"Please enter your age: \n";
     cin>> age;
     cin.ignore();
    
     if ( age <= 1 ) {
        cout<<"You are a baby!\n";
        }
    
     else if ( age <= 3 and > 1 ) {
          cout<<"You are a toddler.\n";
          }
    
     else else if ( age <= 12 and > 3 ) {
          cout<<"You are a child.\n";
          }
    
     else else else if ( age < 18 and > 12 ) {
          cout<<"You are a teenager.\n";
          }
    
     else else else else if ( age >= 18 and < 45 ) {
          cout<<"You are an adult.\n";
          }
    
     else else else else else if ( age >= 45 and <= 55) {
          cout<<"You are middle aged.\n";
          }
    
     else {
          cout<<"You are old!\n";
          {
    
     cin.get();
    }
    As you can see, i add and extra else everytime. This doesn't work, so i was wondering what i need to do to make this work.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    not "and"
    age >1 && age <= 3

    and don't use several else else else... just one is possible

    if ..
    else if ...
    else if ...
    else if ...
    ...
    else ...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    is that all id need to change?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Timbo8
    is that all id need to change?
    You should also check that the age is >= 0 - otherwise discard the user's input
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    you would need a loop to do that, wouldnt you?

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can start with printing out the error message asking to enter the age in the specified range and exit...

    When the program will work as desired you can add the loop to process till the correct value is entered (or opposite, till the first negative value is entered)...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> not "and"

    Actually, "and" is legal. However, it would probably be wise to use "&&" instead so that programmers who are not familiar with that information are not confused by the code.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Due to the nature of the if-else structure, you don't actually need to check
    "else If age is between 1 and 3" ...
    "else if age is between 3 and 12"...
    "else if age is between 12 and 18"... etc

    the following would work in exactly the same way, and looks a bit neater:

    "if age is less than 1"...
    "else if age is less than 3"...
    "else if age is less than 12"...
    "else if age is less than 18"...

    The reason for this - All the conditions are evaluated in order. "else..." does not get executed when "if" yeilds true, so, the entire if/else block terminates as soon as one of the conditions is matched.

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    so they are done in order? But also, how would i check to see if the entered age is below 0, and dispose of it if it is?

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Put if age < 0 as the first in the if-else chain

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    and then if it is less than 0 go back to where it asks for the age?

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Either that or print an error message then exit the program. But if you are going with the first choice i many be better to use a loop. Keep asking as lond as the expression is true and terminmate the loop at a sentinal value entry, usually -1
    Double Helix STL

  13. #13
    Registered User
    Join Date
    Dec 2006
    Posts
    12
    'and" is not recognised by the compiler. you use &&(ampersand) or ||(or) .. && means the word and in english and || is or.. so to make it possible use || because && is only limited it the 1st condition is true then the second it false it is false...

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    joseph that information is solely dependent on your compiler, as my compiler recognizes the words and, or, not, but also uses &&, ||, !. Although I would reccomend using the symbols not the words 1) so other compilers are sure to be able to use it, 2) it isnt as confusing

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Actually, as I already mentioned "and" is legal and all standards compliant compilers must recognize it as an alternate keyword for &&.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 08:40 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM