Like Tree1Likes
  • 1 Post By Matticus

[Noob Needing Help] Not too sure I understand!

This is a discussion on [Noob Needing Help] Not too sure I understand! within the C++ Programming forums, part of the General Programming Boards category; Hey all, so I'm currently in my last year in school and want to go down the career path of ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    6

    [Noob Needing Help] Not too sure I understand!

    Hey all, so I'm currently in my last year in school and want to go down the career path of programming. So I decided to begin learning early. I want to learn C++ and have followed lessons 1 and 2 on this site. However when coming to test my skills I'm currently stuck and don't know whether or not I've understood correctly.

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
        int age;
        int current_year = 2012;
    
        cout<<"Please enter your age: "; //Asks for Age
        cin>> age; //Recives user input as their age
        cin.ignore();
    
        //Repeats age then works out when the person was born by taking the age away from 2012 or 2011 incase their birthday hasn't came around yet
        cout<<"Your age is: "<< age <<" which mean you where either born in "<< current_year - age <<" or "<< (current_year - 1) - age<<"\n";
    
        if (age < 10) //Asks if your less than 10 years old, then tells you that you're pretty young
        {
            cout<<"You are pretty young!";
        }
    
        else if (age > 10 && < 20) //Asks if you are between 10 and 20 and then tells you that you're a teen
        {
            cout<<"You are a teen!";
        }
    
        else if (age > 20 && < 50) //Asks if you are between 20 and 50 and then tells you that you're middle-aged
        {
            cout<<"You middle aged";
        }
    
        else if (age > 50) //Asks that if you are over 50, and then tells you that you're quite old
        {
            cout<<"You are quite old!";
        }
    }
    This is the current program I am trying to write, where you enter your age in, it tells you what year you may have been born in the says where your young, old, or middle aged.

    However I am getting a few problems:

    1. When trying to run the program it is saying that:
    Code:
     else if (age > 10 && < 20)
    {
    ...
    }
    
    
    else if (age > 20 && < 50)
    {
    ...
    }
    is incorrect.

    2. That when I press enter it goes through the whole program, where as I want you to enter you name, press enter, it tells you there year, press enter, it tells you young, old, etc.

    Any help that you guys can offer would be a joy.

    Thanks in advance

    // Kegs

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You need to specify what you're comparing against in the second clause of the if statement:
    Code:
    else if (age > 10 && age < 20)

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    Works like a treat, Thanks.

    Do you anyway I can solve my second problem?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    Quote Originally Posted by Kegs View Post
    Do you anyway I can solve my second problem?
    You probably need to write code to do what you intend.

    You want the user to enter a name and press enter. Your code is only reading an age; it is neither prompting for nor reading a name.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    So I've added in,

    Code:
    int name;
    
    cout<<"/nYour name is: "<< name;
    cin<< name;
    cin.ignore();
    However when I press enter it just goes through the whole program without pausing for me to enter my age, then it to repeat that.
    (P.S Sorry for asking so many questions)

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,376
    Code:
    if (age < 10) //Asks if your less than 10 years old, then tells you that you're pretty young
        {
            cout<<"You are pretty young!";
        }
     
        else if (age > 10 && < 20) //Asks if you are between 10 and 20 and then tells you that you're a teen
        {
            cout<<"You are a teen!";
        }
     
        else if (age > 20 && < 50) //Asks if you are between 20 and 50 and then tells you that you're middle-aged
        {
            cout<<"You middle aged";
        }
     
        else if (age > 50) //Asks that if you are over 50, and then tells you that you're quite old
        {
            cout<<"You are quite old!";
        }
    What will happen if you enter 10, 20, or 50?
    memcpy likes this.

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    Quote Originally Posted by Matticus View Post
    What will happen if you enter 10, 20, or 50?
    Didn't think about that, added in '<=' and '>=' where unnecessary. Thanks

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,626
    Quote Originally Posted by Kegs View Post
    So I've added in,

    Code:
    int name;
    
    cout<<"/nYour name is: "<< name;
    cin<< name;
    cin.ignore();
    However when I press enter it just goes through the whole program without pausing for me to enter my age, then it to repeat that.
    (P.S Sorry for asking so many questions)
    So first, a name is a string. Therefore, you should be using std::string, not int (why are you using int?).
    As an advice, when reading a string, it is better to use std::getline instead of cin >> since it stops reading at first whitespace (space, tab, newline).

    Code:
    if (age < 10) //Asks if your less than 10 years old, then tells you that you're pretty young
        {
            cout<<"You are pretty young!";
        }
      
        else if (age > 10 && < 20) //Asks if you are between 10 and 20 and then tells you that you're a teen
        {
            cout<<"You are a teen!";
        }
      
        else if (age > 20 && < 50) //Asks if you are between 20 and 50 and then tells you that you're middle-aged
        {
            cout<<"You middle aged";
        }
      
        else if (age > 50) //Asks that if you are over 50, and then tells you that you're quite old
        {
            cout<<"You are quite old!";
        }
    Now, as for this, you can simplify this to:
    Code:
    if (age < 10)
            cout<<"You are pretty young!";
        else if (age < 20) // Age must be >= 10 here, else first if clause would match.
            cout<<"You are a teen!";
        else if (age < 50) // Age must be >= 20 here.
            cout<<"You middle aged";
        else // Anything else basically. Will implicitly mean age >= 50 here.
            cout<<"You are quite old!";
    Braces for single line statements are not necessary. Whether it is good practice to do or not I will not comment on.
    If you find yourself making lots of bugs with adding another line to an if statement which has no braces and you forget to add braces, you may want to always use them.
    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
    Aug 2012
    Posts
    6
    Quote Originally Posted by Elysia View Post
    (why are you using int?).
    Sorry like I said, I'm still very new and just read over the first 2 lessons once. I'm not quite sure what a string is that's why I didn't use it, I thought that int would be ok to user here. I'm going to look into strings now and post back when updated.

    EDIT: Looked into this and was easier to do than thought. Since I already had 'using namespace std;' I just changed int to string. Now works like I wanted to thanks all for the help.
    Last edited by Kegs; 08-09-2012 at 09:34 AM.

  10. #10
    Registered User
    Join Date
    Jul 2012
    Posts
    21
    Quote Originally Posted by Kegs View Post
    So I've added in,

    Code:
    int name;
    
    cout<<"/nYour name is: "<< name;
    cin<< name;
    cin.ignore();
    However when I press enter it just goes through the whole program without pausing for me to enter my age, then it to repeat that.
    (P.S Sorry for asking so many questions)

    so here you are declaring name as int (or string now that you've changed it) but you are telling the computer to display 'name' before any data is stored to it.. try flipping the order, also try using getline so you can accept a first and last name if it is entered.

    Code:
    string name; ////declaring name as a string
    
    cout<<"please enter your name\n";      ///if you ran the program without asking first, how will anyone know what to enter??
    getline(cin, name);            ////the computer reads in the entire line, even if a space is entered. If you use cin>>, the computer will stop reading at the first "whitespace".
    cout<<"\nyour name is: " << name;     /// output statement and whatever name the user entered.
    hope this helps, and whenever you get stuck just keep at it!! it will all eventually click in your mind.

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    Seems to work a lot better using 'getline', Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob needing help
    By Youthm in forum C Programming
    Replies: 2
    Last Post: 05-15-2011, 10:30 AM
  2. Noob needing some help and insight
    By punktilend in forum C++ Programming
    Replies: 1
    Last Post: 02-24-2009, 03:41 PM
  3. Noob programmer needing help.
    By Gamemaniac00 in forum C++ Programming
    Replies: 3
    Last Post: 11-10-2006, 06:31 AM
  4. Needing help please....
    By tameeyore in forum C Programming
    Replies: 2
    Last Post: 02-06-2005, 12:06 PM
  5. Novice needing help PLEASE!!!!!
    By Nick Dillon in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2002, 02:21 PM

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