Thread: Need help with my simple If statement & Boolean (Noob sos)

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    20

    Need help with my simple If statement & Boolean (Noob sos)

    Hi guys, this is my first time posting and I recently purchased jumping into C++ and need help on the first practice problem of Chapter 4.

    Q: Ask the user for two user's ages, and include who is older; behave differently if both are over 100.

    So this is what I have coded:

    Code:
    #include <iostream>
    #include <string>
    
    
    usingnamespacestd;
    
    
    int main ()
    
    
    {
        string name; // x
        string name_2; // y
        int x;
        int y;
    
    cout << "Hello User.. This program intend's to ask for the age and name of 2 people\n";
    
    cout << "Please enter the first name:\n";
        getline(cin, name, '\n');
    
    cout << "Good.. now enter the age of " << name << ":\n";
        cin >> x;
    
    cout << "Next I need you to enter the name of person number 2:\n";
        getline(cin, name_2, '\n'); // This line doesn't work when I run it.
    
    cout << "Alright.. now finally please enter the age of " << name_2 << ":\n";
        cin >> y;
    
        if (x > y)
            cout << name << " is older than " << name_2 << "\n";
        else if (y > x)
            cout << name_2 << " is older than " << name << "\n";
        else if ((x && y) > 100)
    cout << "Your both crazy old!!!\n";
        else
            return 0;
    
    
    
    
    
    }
    


    Below is what has ran:

    [Program]


    Hello User.. This program intend's to ask for the age and name of 2 people
    Please enter the first name:
    Jonathan
    Good.. now enter the age of Jonathan:
    21
    Next I need you to enter the name of person number 2:
    Alright.. now finally please enter the age of :
    (Did it skip?)
    Jonathan is older than
    Program ended with exit code: 0

    [/Program]

    2 Problems come from this:
    I can't get the second "getline" to show input for the user.
    I can't get the last statement if/else statement to work.

    Anyone know what I'm doing wrong? Also is their anything like a downloadable PDF on this site that shows the answer key to the problem sets of the book? I can learn so much faster if I can compare it myself so I don't always have to bother people about that much in forums.

    Any help will be greately appreciated! Thanks! : )

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> 2 Problems come from this:
    >> I can't get the second "getline" to show input for the user.

    Code:
    cout << "Good.. now enter the age of " << name << ":\n";
    cin >> x;
    Suppose someone enters in 35<enter>. In cin's internal buffer that will look like "35\n", so extracting x will grab the 35 and leave the residual character(s) in the internal buffer. You need to discard this character using your favorite method, like cin.ignore(). See SourceForge.net: Clearing the input buffer - cpwiki

    >> I can't get the last statement if/else statement to work.
    The expression being tested is:
    (x && y) > 100
    (x && y) is true if both x and y are nonzero, so that becomes 1 > 100, which is never true.
    You probably meant x > 100 && y > 100, where each side of the expression is tested, and both must be true.

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Code:
    cout << "Your both crazy old!!!\n";
    You spelt "you're" incorrectly. It's especially important to get your spelling correct when you're typing something like that.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Indent style - Wikipedia, the free encyclopedia

    usingnamespacestd; - 2 spaces missing

    ((x && y) > 100) - you need to read about logical operators and how they work

    why your main returns something only in particular cases? return statement should be unconditional
    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
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by whiteflags View Post
    >> I can't get the last statement if/else statement to work.
    The expression being tested is:
    (x && y) > 100
    (x && y) is true if both x and y are nonzero, so that becomes 1 > 100, which is never true.
    You probably meant x > 100 && y > 100, where each side of the expression is tested, and both must be true.
    The other aspect is that if/else if/else if are evaluated sequentially. if x is 200 and y is 300, the code will only report that one is older than the other (no mention of "crazy old" which might also be intended).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by vart View Post
    why your main returns something only in particular cases? return statement should be unconditional
    The "should be" is open to interpretation but, in C++, falling off the end of main() is equivalent to return 0. So it makes no real difference in this example, but would be broken in a function other than main().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    20
    Hey guys thanks for all the feedback.. I have rewritten the code and it looks like this now:

    Code:
    #include <iostream>
    #include <string>
    
    
    usingnamespacestd;
    
    
    int main ()
    
    
    {
        string name; // x
        string name_2; // y
        int x;
        int y;
    
    cout << "Hello User.. This program intend's to ask for the age and name of 2 people\n";
    
    cout << "Please enter the first name:\n";
        getline(cin, name, '\n');
    
    cout << "Good.. now enter the age of " << name << ":\n";
        cin >> x;
        cin.ignore();
    
    cout << "Next I need you to enter the name of person number 2:\n";
        getline(cin, name_2, '\n');
    
    cout << "Alright.. now finally please enter the age of " << name_2 << ":\n";
        cin >> y;
    
        if (x > y)
            cout << name << " is older than " << name_2 << "\n";
        else if (y > x)
            cout << name_2 << " is older than " << name << "\n";
        else
            return 0;
    
        if ( x > 100 && y > 100)
    cout << "You're both crazy old!!!\n";
    
    
    
    
    
    }
    


    Program runs like this:

    [Program]
    Hello User.. This program intend's to ask for the age and name of 2 people
    Please enter the first name:
    Jonathan
    Good.. now enter the age of Jonathan:
    155
    Next I need you to enter the name of person number 2:
    Mike
    Alright.. now finally please enter the age of Mike:
    141
    Jonathan is older than Mike
    You're both crazy old!!!
    Program ended with exit code: 0
    [/Program]

    Wow this stuff really pounds logic to my head. Never even imagined that the <enter> key also inputs a "\n" as well.

    I also had been able to fix the order of operations for (x && y > 100) and realized that was only half the problem with the "If/else" statement only choosing one possibility while it was possible to have 2 at the same time. (being old and choosing who is older)

    I hope this new code I wrote is working for all the reasons I think it should and not by accident! ^^

    //Side-note: I have changed the spelling of "Your" to "You're" & "usingnamespacestd" was a typo (I know I need 2 spaces.. ^^ )
    Last edited by Jonathan002; 10-04-2013 at 12:44 AM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try entering both ages as 102, and see what happens. You won't get much output.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Oct 2013
    Posts
    20

    Oic!

    Quote Originally Posted by grumpy View Post
    Try entering both ages as 102, and see what happens. You won't get much output.
    Thanks for pointing that out. : ) For some reason I had the impression that the "if/else" statement needed to be ended with "else" so I used "return 0." After running some test I found out that I could omit the "else" statement entirely.

    This is now my new code:
    Code:
    #include <iostream>
    #include <string>
    
    
    usingnamespacestd;
    
    
    int main ()
    
    
    {
        string name; // x
        string name_2; // y
        int x;
        int y;
    
    cout << "Hello User.. This program intend's to ask for the age and name of 2 people\n";
    
    cout << "Please enter the first name:\n";
        getline(cin, name, '\n');
    
    cout << "Good.. now enter the age of " << name << ":\n";
        cin >> x;
        cin.ignore();
    
    cout << "Next I need you to enter the name of person number 2:\n";
        getline(cin, name_2, '\n');
    
    cout << "Alright.. now finally please enter the age of " << name_2 << ":\n";
        cin >> y;
    
        if (x > y)
            cout << name << " is older than " << name_2 << "\n";
        else if (y > x)
            cout << name_2 << " is older than " << name << "\n";
        else if (x == y)
    cout << "Your both the same age!!\n";
    
    
        if (( x > 100 && y > 100) && x == y)
    cout << "And You're both crazy old!!\n";
        else if ( x > 100 && y > 100)
    cout << "You're both crazy old!!!\n";
    
    
    }
    


    [Test 1]
    Hello User.. This program intend's to ask for the age and name of 2 people
    Please enter the first name:
    Jonathan
    Good.. now enter the age of Jonathan:
    102
    Next I need you to enter the name of person number 2:
    Mike
    Alright.. now finally please enter the age of Mike:
    102
    Your both the same age!!
    And You're both crazy old!!
    Program ended with exit code: 0
    [/Test 1]

    [Test 2]
    Hello User.. This program intend's to ask for the age and name of 2 people
    Please enter the first name:
    Jonathan
    Good.. now enter the age of Jonathan:
    21
    Next I need you to enter the name of person number 2:
    Mike
    Alright.. now finally please enter the age of Mike:
    21
    Your both the same age!!
    Program ended with exit code: 0
    [/Test 2]



    I really appreciate the learning value I get from your help!
    Last edited by Jonathan002; 10-04-2013 at 02:50 AM.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should pass your code as text

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
        string name; // x
        string name_2; // y
        int x;
        int y;
    
        cout << "Hello User.. This program intend's to ask for the age and name of 2 people\n";
    
        cout << "Please enter the first name:\n";
        getline(cin, name, '\n');
    
        cout << "Good.. now enter the age of " << name << ":\n";
        cin >> x;
        cin.ignore();
    
        cout << "Next I need you to enter the name of person number 2:\n";
        getline(cin, name_2, '\n');
    
        cout << "Alright.. now finally please enter the age of " << name_2 << ":\n";
        cin >> y;
    
        if (x > y)
            cout << name << " is older than " << name_2 << "\n";
        else if (y > x)
            cout << name_2 << " is older than " << name << "\n";
        else if (x == y)
            cout << "Your both the same age!!\n";
    
    
        if (( x > 100 && y > 100) && x == y)
            cout << "And You're both crazy old!!\n";
        else if ( x > 100 && y > 100)
            cout << "You're both crazy old!!!\n";
    }
    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

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by SirPrattlepod View Post
    Code:
    cout << "Your both crazy old!!!\n";
    You spelt "you're" incorrectly. It's especially important to get your spelling correct when you're typing something like that.
    What?! You mean that is still the case? I thought they changed this after I was out of school?

  12. #12
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by DaveH View Post
    What?! You mean that is still the case? I thought they changed this after I was out of school?
    I like that you fixed it. But now there is a problem when the people are the same age: Your both the same age!

  13. #13
    Registered User
    Join Date
    Oct 2013
    Posts
    20
    Version Update!

    What's new in this version:
    - Bug fixes to spelling
    - Posting code as text now (or at least trying too..)

    Code:
    [Text]
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    
    {
        string name; // x
        string name_2; // y
        int x;
        int y;
        
        cout << "Hello User.. This program intend's to ask for the age and name of 2 people\n";
        
        cout << "Please enter the first name:\n";
        getline(cin, name, '\n');
        
        cout << "Good.. now enter the age of " << name << ":\n";
        cin >> x;
        cin.ignore();
        
        cout << "Next I need you to enter the name of person number 2:\n";
        getline(cin, name_2, '\n');
        
        cout << "Alright.. now finally please enter the age of " << name_2 << ":\n";
        cin >> y;
        
        if (x > y)
            cout << name << " is older than " << name_2 << "\n";
        elseif (y > x)
            cout << name_2 << " is older than " << name << "\n";
        elseif (x == y)
            cout << "You're both the same age!!\n";
     
        
        if (( x > 100 && y > 100) && x == y)
            cout << "And You're both crazy old!!\n";
        elseif ( x > 100 && y > 100)
            cout << "You're both crazy old!!!\n";
    
    }
    [/Text]
    Wait how do you pass them out as text? All I know how to do to make it neater is [Code:] command.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Your IDE is adding crap to colorize the text, and for some reason, it chooses to make the code display in Times New Roman font as well. I can't guess how to turn that feature off, but I suggest that you copy the code and paste it in notepad.exe first. Then the code from notepad will be plain text.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apart from the formatting concern, your code (as posted) isn't valid, as "elseif" is not a keyword.

    That aside, try simplifying the two if/else blocks. Both have a lot of duplicated parts. Removing the duplication will make the logic simpler.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a simple question (Noob)
    By MishaMish in forum C++ Programming
    Replies: 2
    Last Post: 01-26-2011, 01:24 AM
  2. Replies: 2
    Last Post: 04-25-2010, 10:07 AM
  3. Replies: 3
    Last Post: 09-12-2005, 09:08 AM
  4. Noob here...a simple question.
    By The_Mayor in forum C Programming
    Replies: 4
    Last Post: 06-08-2005, 12:48 AM
  5. Simple boolean problem
    By larry in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2001, 08:49 AM

Tags for this Thread