godzsa
godzsa

Reputation: 2395

How to make a condition with a string?

So I want to make a function with a parametre which is a conditon, so I can call the the function with different conditions eg.

int input(string condition){
    int number;
    cin>>number;
    if (!condition){
        cout<<"Wooah maaan, thx!"
    } else {
       cout<<"You can do better!"
    return number;
}


void something(){
   int g_num;
   cout<<"Give me a number between 1 and 6";
   g_num=input("number<6&&number>1");
}

How is this possible, because with similar I get en error:

cannot convert 'std::string' to 'bool' in assignmen

Any ideas?

(I just started learning c++, so please don't judge me, I know I am lame.)

Upvotes: 0

Views: 173

Answers (5)

Vlad from Moscow
Vlad from Moscow

Reputation: 310970

You can use lambda expressions instead of strings. For example

#include <functional>

int input( std::function<bool( int x )> f )
{
    int number;
    cin>>number;

    if (! f( number ))
    {
        cout<<"Wooah maaan, thx!"
    } 
    else 
    {
       cout<<"You can do better!"
    }

    return number;
}

input( []( int ) { return ( ( std::cout << "The first\n" ), true ); } );
input( [] ( int ){ return ( ( std::cout << "The second\n" ), false ); } );

Upvotes: 0

jrok
jrok

Reputation: 55395

That's not how it works, a string literal can't magicaly be turned into executable code.

One way to achive what you want would be a function template that accepts some callable object that you can use as a predicate. Example:

template<typename Function>
int input(Function f)         // the type of f is deduced from the call to this
{                             // function
    int number;
    if (!(cin >> number)) return -1; // input validation!
    if (f(number))
       cout << "thanks!";
    else
       cout << "meh...";
    return number;
}

You can use this in different ways - pass it a pointer to a function, an object of type that overloads function call operator (operator()) or a lambda function. There are constraints on the object you'll pass as an argument - it needs to be callable with one argument of type int and it needs to return some type that's convertible to bool.

Example:

 int g_num = input( [](int i){ return i > 1 && i < 6; } ); // pass a lambda

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490108

This is possible, but it's a non-trivial task, and one you probably want to avoid if you can.

If you decide to go ahead anyway, you'll need to define the grammar of the expressions you want to support, write a parser for that grammar, then evaluate the expressions. You might want to consider something like Boost Spirit or byacc for the parser. The'll have tutorials and demos that give at least some idea of how to define the grammar.

Upvotes: 2

David G
David G

Reputation: 96810

Conditions cannot be in the form of a string. Just do this:

int input()
{
    int number;
    cin >> number;

    if ((number > 1) && (number < 6)) {
        cout << "Wooah maaan, thx!"
    } else {
       cout << "You can do better!"

    return number;
}

Upvotes: 0

rerun
rerun

Reputation: 25495

What you are attempting there is to execute code at runtime which is in a string. You can't do that directly an can only be accomplished by using some sort of hosted scripting engine and that is not trivial.

What you can do is store the number and evaluate the condition

int input(bool condition){

    if (!condition){
        cout<<"Wooah maaan, thx!"
    } else {
       cout<<"You can do better!"
    return number;
}

void something(){
   int g_num;
   cout<<"Give me a number between 1 and 6";
   int number;
   cin>>number;
   g_num=input(number<6 && number>");
}

There are still issues with that code as in what happens if its a bad number which is passed on to the rest of the program.

Upvotes: 0

Related Questions