Thread: New to C++

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    New to C++

    hello, im new to C++ and I have a question about a problem I am having. I am useing the If statements to ask questions and the question is

    How old are you?
    I got the coding right and it all works but I am having a problem.

    I have
    Code:
    if ( Age < 50 ) {
             cout<<"Well your not over the hill yet haha.\n";
        }
        else if ( Age == 50  ) {
             cout<<"Hmm, congratulations on making it this far without being shot.\n";
        }
        else if ( Age > 50 ) {
             cout<<"well your over the hill and falling.....\n";
        }
        if ( Age >= 100 ) {
             cout<<"OH MY GOD!\n";
        }
        cin.get();
    It all works..... Except the OH MY GOD part of it.... I want to put in the coding "Equal to or Greater than 100"

    so what I have here is if you are under 50 you get the first responce. If your equal to 50 you get the second... and if you are above 50 you get the 3rd...

    If you are over OR equal to 100 or over I want it to say the 4th responce. how do i fix this?

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    How about something like this:

    Code:
    if ( Age < 50 ) {
             cout<<"Well your not over the hill yet haha.\n";
        }
        else if ( Age == 50  ) {
             cout<<"Hmm, congratulations on making it this far without being shot.\n";
        }
        else if ( Age > 50 && age < 100 ) {
             cout<<"well your over the hill and falling.....\n";
        }
        if ( Age >= 100 ) {
             cout<<"OH MY GOD!\n";
        }
        cin.get();

    This logic will ensure that 'age' will only qualify for a single case.

    suggestion:
    Not sure if you have looked into switch/case structure yet, but swtich/case is a good way to handle a bunch if if/else statements.
    Last edited by The Brain; 04-12-2005 at 09:44 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Better still:

    Code:
    if (age < 50 ) {
             cout<<"Well you're not over the hill yet haha.\n";
        }
        else if ( age == 50  ) {
             cout<<"Hmm, congratulations on making it this far without being shot.\n";
        }
        else if (age < 100 ) {
             // No need to test if age > 50 as it must be if this gets executed.
             cout<<"Well you're over the hill and falling.....\n";
        }
        else {
             cout<<"OH MY GOD!\n";
        }
        cin.get();
    And I even corrected your spelling
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

Popular pages Recent additions subscribe to a feed