Like Tree4Likes

New to C++ and programming

This is a discussion on New to C++ and programming within the C++ Programming forums, part of the General Programming Boards category; Hello, I am so new with all of this. I am using "Jumping_into C++" to start my learning of C++. ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    5

    New to C++ and programming

    Hello, I am so new with all of this. I am using "Jumping_into C++"
    to start my learning of C++. I am currently stuck in Chapter 4 with this practice problem. Any input as to what I am doing wrong would be very helpful.
    This is what I have so far:


    Code:
    //Program task: Ask the user for two users' ages, and //indicate who is older;
    //behave differently if both are over 100
    
    
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
    // declared variables for user input
    int mauricio_age;
    int heather_age;
    
    
    //Ask the user for the ages of mauricio and heather
    cout << "What is Mauricio age: " << "\n";
    cin >> mauricio_age;
    cout << "What is the Hethers age: " << "\n";
    cin >> heather_age;
    
    
    //  The problem is
    //when I input mauricios age and it's less than 99 and heathers age is over 99
    //it gives me the wrong "cout "WOW that is a lot of years""
    //when it should give me "cout "Mauricio is younger than Heather". Also when I have the
    //same age above 99 for both mauricio and heather it gives the "cout "your age is the same" 
    // when it should say"cout"wow that is a lot of years"
    
    
    if ( mauricio_age == heather_age )
    {
    cout << " Your age is the same\n";
    return 0;
    }
    if ( mauricio_age && heather_age >= 99 )
    {
    cout << "WOW that is alot of years\n";
    } else if ( mauricio_age < heather_age ) { cout << "Mauricio is younger then Heather\n"; } else { cout << "Heather is younger then Mauricio\n"; return 0; }
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,407
    you should probably initialize the two age variables to zero when you declare them. that may resolve some of your unexpected behavior. also, you should call cin.ignore() after receiving the first age, because the '\n' will still be in the buffer, and the next call to cin >> will fail because there is no suitable data available to read into an int variable.

    this line will not do what you apparently think it will.
    Code:
    if ( mauricio_age && heather_age >= 99 )
    Code:
    namespace life
    {
        const bool change = true;
    }

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by Elkvis View Post
    you should probably initialize the two age variables to zero when you declare them. that may resolve some of your unexpected behavior.
    Considering input is written to both variables, this should not be necessary.

    also, you should call cin.ignore() after receiving the first age, because the '\n' will still be in the buffer, and the next call to cin >> will fail because there is no suitable data available to read into an int variable.
    Partly right. std::cin >> ignores whitespace, so it will work. Problem comes when using std::getline after std::cin >>. If you do that, then you need to clear the input buffer with std::cin.get().

    this line will not do what you apparently think it will.
    Code:
    if ( mauricio_age && heather_age >= 99 )
    In C++, you cannot "share" conditions between and and or operators, so this does not equal
    Code:
    if ( mauricio_age >= 99 && heather_age >= 99 )
    as you might think it does. Instead, it actually means:
    Code:
    if ( mauricio_age != 0 && heather_age >= 99 )
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    Quote Originally Posted by Elkvis View Post
    you should probably initialize the two age variables to zero when you declare them.
    Quote Originally Posted by Elysia View Post
    Considering input is written to both variables, this should not be necessary.
    I disagree with both of you. It would be better to check if the input of each variable succeeds or fails.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    5
    I am still confused as to how to write the program. Chapter 4 does not talk about "std::cin". But I will try to use the input given and correct the problem. Thank you for the help.

  6. #6
    Registered User
    Join Date
    Jul 2013
    Posts
    5
    So how would you write this program so it will work?

  7. #7
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by BeginnerC++ View Post
    So how would you write this program so it will work?
    You might want to think about removing those return 0 statements from your if statement blocks.

    return 0 should only be used at the end of your program.

    You can use break points to achieve what you had in mind with return 0.
    (hint: you don't need any break points)

    The return 0 causes your program to terminate

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by CASHOUT View Post
    You might want to think about removing those return 0 statements from your if statement blocks.

    return 0 should only be used at the end of your program.
    No, it should not necessarily be so.
    A function may have multiple exit points. This is not bad practice. You do have to think of whether it's more work to exit through a single point or through multiple points. Ideally, in C++, since we have RAII, multiple exit points may be preferred, but of course, in practice, you may find that it's easier to go through a common exit point nevertheless.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    2,407
    Quote Originally Posted by BeginnerC++ View Post
    Chapter 4 does not talk about "std::cin".
    because you have using "namespace std;" at the top of your program, std::cin can be written simply as cin, which is exactly what you're doing. in any case, you need to clear the whitespace character(s) from the buffer after reading the first value in. this can be done with cin.ignore() or cin.get().

    as grumpy says, you should also be checking if the input succeeds by doing something like this:

    Code:
    if (!(cin >> mauricio_age))
    {
      cout << "Invalid input" << endl;
      return -1;
    }
    you don't have to return, but instead you could loop (do ... while seems appropriate here) back to instruct them to try again.
    Code:
    namespace life
    {
        const bool change = true;
    }

  10. #10
    Registered User
    Join Date
    Jul 2013
    Posts
    5
    I only have one issue with the program now. When the ages for both mauricio and heather are the same and above 99 it ouputs "your age is the same" when I would like it to ouput "WOW that is a lot of years" I have been sitting here for hours trying to figure this out and I can't seem to get it. Here is the new code

    Code:
    //Program task: Ask the user for two users'
    //ages, and indicate who is older;
    //behave differently if both are over 100
    
    
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
        // declared variables for user input
        int mauricio_age;
        int heather_age;
    
    
    
    
        //Ask the user for the ages of mauricio and heather
        cout << "What is Mauricio age: " << "\n";
        cin >> mauricio_age;
        cout << "What is the Hethers age: " << "\n";
        cin >> heather_age;
    
    
        //When mauricios and heathers age are the same 
        //and above 99 it outputs "Your age is the same" 
        //when it needs to be "WOW that is alot of years"
    
    
    
    
        if ( mauricio_age == heather_age )
        {
            cout << " Your age is the same\n";
            return 0;
        }
        if ( mauricio_age >= 99 && heather_age >= 99 )
        {
            cout << "WOW that is alot of years\n";
        }
        else if ( mauricio_age < heather_age )
        {
            cout << "Mauricio is younger then Heather\n";
        }
        else
        {
            cout << "Heather is younger then Mauricio\n";
            return 0;
        }
    }

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Hint: If statements are executed in sequential order. Which if statement matches first, the second, third, etc when inputting different ages? Check your logic!
    stahta01 likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    Quote Originally Posted by Elysia View Post
    No, it should not necessarily be so.
    A function may have multiple exit points. This is not bad practice. You do have to think of whether it's more work to exit through a single point or through multiple points. Ideally, in C++, since we have RAII, multiple exit points may be preferred, but of course, in practice, you may find that it's easier to go through a common exit point nevertheless.
    That has very little to do with the code being discussed.

    If you diagram this, it illustrates how the extra return 0 statements add nothing. It also points out to the naked eye that one if is disconnected from the logic; meaning that one if is sequentially reached instead of connected with an else clause.

    Edit: I admit my diagrams are ........, but Elysia suggests them so often...
    Attached Images Attached Images  
    laserlight likes this.

  13. #13
    Registered User
    Join Date
    Jul 2013
    Posts
    5
    Quote Originally Posted by Elysia View Post
    Hint: If statements are executed in sequential order. Which if statement matches first, the second, third, etc when inputting different ages? Check your logic!
    Why would the "if" statements need to have a specific order if the inputs are random? I know I have it wrong. But I am having a hard time understanding why this won't work at the beginning.

    Code:
     if ( mauricio_age <=99  == heather_age <= 99 )
     {
        cout << " Your age is the same\n";
        return 0;
     }

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    here is what you are doing

    Code:
    bool m = (mauricio_age <=99);
    bool h = (heather_age <= 99 );
    if(m == h)
    {
     ...
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by BeginnerC++ View Post
    Why would the "if" statements need to have a specific order if the inputs are random? I know I have it wrong. But I am having a hard time understanding why this won't work at the beginning.
    Logic is sequential!
    You are asking if the ages are equal, THEN if the ages are both above 99, THEN if one age is less than another, THEN if none of the above hold true.
    The way you've written is, only ONE of these conditions can be executed. If multiple conditions hold true, still only one will be executed, and that is the first that matches.
    Run that scenario through your mind or get a debugger.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2012, 01:03 AM
  2. Replies: 4
    Last Post: 12-11-2011, 03:25 PM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 10:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21