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

M

Maxd out

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
 
M

Mike Wahler

Maxd out said:
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
 
K

Kevin Goodsell

Maxd said:
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
 
C

Chris \( Val \)

|
| | > 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
 
K

Kevin Goodsell

Chris said:
|
|
| #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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top