Thread: two user age

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

    Red face two user age

    Hey, i have read your book and in if chapter at Practice problems
    1. Ask the user for two users’ ages, and indicate who is older; behave differently if both are over 100.
    I have a problem with my code below, can you help me please?



    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int first_user_age;
        int second_user_age;
        cout << "Insert your first age" << '\n';
        getline (cin, first_user_age, '\n');
        cout << "Insert your second age/your friend age" << '\n';
        getline (cin, second_user_age, '\n');
        
        bool is_user_user_age_more_than_100 = first_user_age && second_user_age > 100;
        if (is_user_user_age_more_than_100)
        {
            if (first_user_age > second_user_age)
            {
                cout >> "User age is more than 100 year and the first user is older than the second user" \n;
            }
            if (first_user_age < second_user_age)
            {
                cout >> "User age is more than 100 year and the second user is older than the first user" \n;
            }
        }
        bool is_user_user_age_less_than_100 = first_user_age && second_user_age < 100;
        if (is_user_user_age_less_than_100)
        {
            if (first_user_age > second_user_age)
            {
                cout >> "User age is less than 100 year and the first user is older than the second user" \n;
            }
            if (first_user_age < second_user_age)
            {
                cout >> "User age is more than 100 year and the second user is older than the first user" \n;
            }
        }
        
        return 0;
    }
    Why is integer cannot combined with getline ?

    I got this message "[Error] C:\Users\Guest\Documents\C-Free\Projects\program\main.cpp:10: error: no matching function for call to `getline(std::istream&, int&, char)'

    Likewise the second problem i'm using integer

    2. Implement a simple “password” system that takes a password in the form of a number. Make it so that either of two numbers is valid, but use only one if statement to do the check.

    if we using string in problem 2 we could get an character besides number

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,234
    Quote Originally Posted by genei
    Why is integer cannot combined with getline ?
    Because the version of getline that you want to use reads into a std::string, not an int.

    One approach is to read the line into a std::string object, initialise a std::stringstream object using this std::string object, and then read into the int variable by using formatted input with the overloaded operator>>

    Quote Originally Posted by genei
    2. Implement a simple “password” system that takes a password in the form of a number. Make it so that either of two numbers is valid, but use only one if statement to do the check.

    if we using string in problem 2 we could get an character besides number
    It would probably suffice for you to directly read into the int variables by using formatted input with the overloaded operator>>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New user here :)
    By Hoincobia in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2011, 09:14 PM
  2. My user name?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-02-2006, 02:08 AM
  3. Getting value from user
    By Allessandro in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2005, 03:05 PM
  4. How to get User?
    By cfrost in forum Windows Programming
    Replies: 4
    Last Post: 01-08-2005, 07:17 AM
  5. New User
    By fastmonkey in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2002, 11:12 PM

Tags for this Thread