compare an input to see if it's contained within a paramter list

Discussion in 'C++' started by Maxd out, Aug 29, 2003.

  1. Maxd out

    Maxd out Guest

    Hi all

    As a newbie to C++ just wondering if you can compare a user input e.g. the
    integer 2 to see if that integer is contained within a parameter list. If so
    how can it be done and can it be done by referencing?

    Tks
    Maxd out, Aug 29, 2003
    #1
    1. Advertising

  2. Maxd out

    Mike Wahler Guest

    Maxd out <> wrote in message
    news:u3K3b.71320$...
    > Hi all
    >
    > As a newbie to C++ just wondering if you can compare a user input e.g. the
    > integer 2 to see if that integer is contained within a parameter list. If

    so
    > how can it be done and can it be done by referencing?



    #include <iostream>

    bool contained(int& param1, int& param2, int& param3)
    {
    int input(0);
    return (std::cin >> input) && (input == param1 ||
    input == param2 ||
    input == param3);
    }

    int main()
    {
    std::cout << "User input is"
    << contained(1, 2, 3) ? " " : " not "
    << "contained in parameter list\n";

    return 0;
    }

    Did you mean something else?

    -Mike
    Mike Wahler, Aug 29, 2003
    #2
    1. Advertising

  3. Maxd out wrote:

    > Hi all
    >
    > As a newbie to C++ just wondering if you can compare a user input e.g. the
    > integer 2 to see if that integer is contained within a parameter list.


    Uh... I guess you could do this:

    void func(int a, int b, int c, int d)
    {
    std::cout << "enter an integer: ";
    int i;
    std::cin >> i;

    if (i == a || i == b || i == c || i == d)
    {
    std::cout << "That number is in the parameter list" << std::endl;
    }
    else
    {
    std::cout << "That number is not in the parameter list" << std::endl;
    }
    }

    But somehow I don't think that's what you mean. Are you sure "Parameter
    list" is the term you wanted to use?

    > If so
    > how can it be done and can it be done by referencing?


    What is "referencing"?

    Your question is very unclear.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Aug 29, 2003
    #3
  4. "Mike Wahler" <> wrote in message
    news:LtK3b.363$...
    |
    | Maxd out <> wrote in message
    | news:u3K3b.71320$...
    | > Hi all
    | >
    | > As a newbie to C++ just wondering if you can compare a user input e.g. the
    | > integer 2 to see if that integer is contained within a parameter list. If
    | so
    | > how can it be done and can it be done by referencing?
    |
    |
    | #include <iostream>
    |
    | bool contained(int& param1, int& param2, int& param3)
    | {
    | int input(0);
    | return (std::cin >> input) && (input == param1 ||
    | input == param2 ||
    | input == param3);
    | }
    |
    | int main()
    | {
    | std::cout << "User input is"
    | << contained(1, 2, 3) ? " " : " not "
    | << "contained in parameter list\n";

    std::cout << "User input is "
    << contained( 1, 2, 3 ) ? std::cout << " " :
    std::cout << " not contained in parameter list\n";


    Hello Mike, you left a couple of important bits out :).

    Btw, why not pass by const reference, or by value ?
    It saves the warnings about temporaries.

    Cheers.
    Chris Val
    Chris \( Val \), Aug 30, 2003
    #4
  5. Chris ( Val ) wrote:
    > "Mike Wahler" <> wrote in message
    > news:LtK3b.363$...
    > |
    > |
    > | #include <iostream>
    > |
    > | bool contained(int& param1, int& param2, int& param3)
    > | {
    > | int input(0);
    > | return (std::cin >> input) && (input == param1 ||
    > | input == param2 ||
    > | input == param3);
    > | }
    > |
    > | int main()
    > | {
    > | std::cout << "User input is"
    > | << contained(1, 2, 3) ? " " : " not "
    > | << "contained in parameter list\n";
    >
    > std::cout << "User input is "
    > << contained( 1, 2, 3 ) ? std::cout << " " :
    > std::cout << " not contained in parameter list\n";


    I don't believe there is an operator<<(ostream, ostream) available, so
    your code will not compile. Even if it did it wouldn't give the right
    output. The original code was fine.

    >
    >
    > Hello Mike, you left a couple of important bits out :).
    >
    > Btw, why not pass by const reference, or by value ?
    > It saves the warnings about temporaries.


    You are right on this point. But I think the warnings should be errors.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Aug 30, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randy

    Grabbing paramter in the URL

    Randy, Feb 8, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    342
    Eliyahu Goldin
    Feb 8, 2005
  2. tshad
    Replies:
    5
    Views:
    510
    Steve C. Orr [MVP, MCSD]
    May 17, 2005
  3. Islamegy®

    SQL Output Paramter problem

    Islamegy®, Apr 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,210
  4. Ranginald
    Replies:
    2
    Views:
    566
    Ranginald
    May 3, 2006
  5. Andreas Volz

    regex for url paramter

    Andreas Volz, Dec 7, 2004, in forum: Python
    Replies:
    0
    Views:
    271
    Andreas Volz
    Dec 7, 2004
Loading...

Share This Page