Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By Elysia

accepting two inputs

This is a discussion on accepting two inputs within the C++ Programming forums, part of the General Programming Boards category; Hi, I am new to C++ and reading through the book. I am trying all the practice problems. Could someone ...

  1. #1
    Registered User Aratel's Avatar
    Join Date
    Oct 2012
    Posts
    4

    Exclamation accepting two inputs

    Hi,

    I am new to C++ and reading through the book. I am trying all the practice problems. Could someone maybe help me out with the first practice problem at the end of chapter 4. I can't figure out how to make the program accept two different inputs. I tried to make one x and one y. But the program I make only asks for the first input.

    Here's the problem:

    Ask the user for two users' ages, and indicate who is older; behave differently if both are over 100.


    Thanks in advance. Sorry for the nooby question.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    "The book" is a rather vague reference. However, I presume you mean the one that the site owner wrote, which I have not read (although I would if I got a free copy ).

    It's impossible to say what you're doing wrong if we can't see your code.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User Aratel's Avatar
    Join Date
    Oct 2012
    Posts
    4
    Yes, I mean the one the site owner wrote. Here is how I attempted the problem:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x;
        cout << "Enter first age:";
        cin >> x;
        int y;
        cout <<"Enter second age:";
        cin >> y;
    
        if (x < y)
        {
            cout << "The second person is older:";
        }
    
        else (x > y)
        {
            cout << "The first person is older:";
        }
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Eh, that looks like it should work (though it does not consider what happens if they are of the same age). Are you running your program in a separate command prompt window?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User sirama's Avatar
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    7
    Corrected your code to show the output. Too see the output w/o this code change, run your exe is dos prompt.

    Code:
    #include <iostream>
    #include <conio>
    Code:
    using namespace std;
    
    int main()
    {
        int x;
        cout << "Enter first age:";
        cin >> x;
        int y;
        cout <<"Enter second age:";
        cin >> y;
    
        if (x < y)
        {
            cout << "The second person is older:";
        }
    
        else (x > y)
        {
            cout << "The first person is older:";
        }
        getch();
    
    
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Do not use sirama's suggested code unless you have good reason to do so. <conio> and getch are non-standard and unnecessary here.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    Quote Originally Posted by Aratel View Post
    Yes, I mean the one the site owner wrote. Here is how I attempted the problem:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x;
        cout << "Enter first age:";
        cin >> x;
        int y;
        cout <<"Enter second age:";
        cin >> y;
    
        if (x < y)
        {
            cout << "The second person is older:";
        }
    
        else (x > y)
        {
            cout << "The first person is older:";
        }
    }
    the portion highlighted in red will not do what you expect.

  9. #9
    Registered User Aratel's Avatar
    Join Date
    Oct 2012
    Posts
    4
    Thank you . It worked when I tried else if instead of else. Now, when I try the last part of the problem, to behave differently if they are both over 100, my code is not working using &&. Anyone know why not? I appreciate all the input:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x;
        cout << "Enter first age:";
        cin >> x;
        int y;
        cout <<"Enter second age:";
        cin >> y;
    
        if (x < y)
        {
            cout << "The second person is older:";
        }
    
        else if (y < x)
        {
            cout << "The first person is older:";
        }
    
        else (x && y > 100)
        {
            cout << "You are both too old.";
        }
    }
    Last edited by Aratel; 10-08-2012 at 10:44 AM.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    Quote Originally Posted by Aratel View Post
    Thank you . It worked when I tried else if instead of else. Now, when I try the last part of the problem, to behave differently if they are both over 100, my code is not working using &&. Anyone know why not? I appreciate all the input:

    Code:
    else (x && y > 100)
    again, this will not work! you cannot have a condition with an else, unless it is an else if.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by Aratel View Post
    Thank you . It worked when I tried else if instead of else. Now, when I try the last part of the problem, to behave differently if they are both over 100, my code is not working using &&. Anyone know why not? I appreciate all the input:
    else (x && y > 100)
    should be
    else if (x > 100 && y > 100)

    If you want to include a condition, you must use else if. Else is only for the last statement which is executed if no other if/else if are true.
    x && y > 100 does not mean what you think it does.
    It means
    x != 0 && y > 100
    You must be explicit for both sides; one condition cannot cover more one "side".
    Elkvis 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 Aratel's Avatar
    Join Date
    Oct 2012
    Posts
    4
    Thanks again to all for the informative input. I think I got it now. I am now working on the next practice problem, with which I am also having difficulty.

    I can't seem to figure out how to get the program to accept two different passwords. Line 9 is the problem. First, I tried:
    if (password == "5151" || "51")

    Again, thanks for taking the time to respond to my nooby questions

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string password;
        cout << "Enter password:" << "\n";
        getline (cin, password, '\n');
        if (password == "5151" || password == "51")
        {
            cout << "Access Granted" << "\n";
        }
        else
        {
            cout << "Access Denied" << "\n";
            return 0;
        }
    }
    Last edited by Aratel; 10-09-2012 at 08:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() not accepting 'ans' value
    By Zaheer Attar in forum C Programming
    Replies: 4
    Last Post: 09-10-2011, 01:42 PM
  2. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  3. accepting +123 help
    By peanut in forum C Programming
    Replies: 7
    Last Post: 11-01-2006, 10:48 PM
  4. Accepting Decimals
    By dragon_man in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2003, 10:09 PM
  5. Accepting function key 'F12'
    By Fire Flash in forum C Programming
    Replies: 1
    Last Post: 03-18-2002, 03:32 PM

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