Error calling functions

0

The program consists of hitting the lottery and then hitting another prize.

The problem is when declaring the congratulations function (). When I call this from the main it gives me an error and if I call it at the end of the lottery () function, it does not run but it does not skip error.

#include <iostream>
#include <fstream>

using namespace std;

bool loteria();
void felicitaciones(bool premio,bool bonus);

int main()
{
    loteria();
    return 0;
}

bool loteria()
{
    int num,num2;
    bool premio,bonus;
    cout<<"Introduce un numero del 1 al 9:";
    cin >> num;

    if(num==8)
    {
        cout <<endl<< "Premio"<<endl;
        premio=true;
    }else
    {
        cout << "Nada";
        return 0;
    }

    cout << "Si acierta de nuevo se lleva un bonus:";
    cin >> num2;

     if(num2==1)
    {
        cout << endl<<"Bonus"<<endl;
        bonus=true;
        return premio,bonus;
    }else
    {
        cout << "Nada";
        bonus=false;
        return premio,bonus;
    }
    felicitaciones(premio,bonus);
}

void felicitaciones(bool premio, bool bonus)
{
    if(premio==true && bonus==true)
    {
        cout << "Usted es un afortunado";
    }
    else
    {
        cout << "No tiene mucha suerte";
    }
}
    
asked by Vendetta 09.11.2017 в 13:33
source

2 answers

1

Your code has some other fault:

  • You can not put that kind of double return , since it's only going to return a single thing. If you need to return more than one element, I advise you to use another type of structure, either a TAD , or one that is returned, and the other, a parameter passed by reference, etc.
  • It does not make sense that the lottery () function has return value, because in your case you do not use it.
  • The congratulations function is not executed because if you set the function * lottery * it has a return value if it fulfills the 2nd condition (num2==1) and if not, it will return the following statement else . Now, your code continues, but it does not make sense since you will not get to execute any sentence after the return statement.
  • Precisely at the syntactic and logical level the congratulations function is correct. One possible implementation is the following:

    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void loteria(bool &premio, bool &bonus); // las variables son pasadas por referencia 
    void felicitaciones(bool premio,bool bonus);
    
    int main()
    {
        bool premio = false;
         bool bonus = false;
        loteria(premio, bonus);
        felicitaciones (premio, bonus);
    }
    
    void loteria(bool &premio, bool &bonus)
    {
        int num,num2;
        cout<<"Introduce un numero del 1 al 9:";
        cin >> num;
    
        if(num==8)
        {
            cout <<endl<< "Premio"<<endl;
            premio=true;
        }else
        {
            cout << "Nada";
        }
    
        cout << "Si acierta de nuevo se lleva un bonus:";
        cin >> num2;
    
         if(num2==1)
        {
            cout << endl<<"Bonus"<<endl;
            bonus=true;
        }else
        {
            cout << "Nada";
        }
    }
    
    void felicitaciones(bool premio, bool bonus)
    {
        if(premio==true && bonus==true)
        {
            cout << "Usted es un afortunado";
        }
        else
        {
            cout << "No tiene mucha suerte";
        }
    }
    
        
    answered by 09.11.2017 / 14:30
    source
    2

    C ++ does not allow multiple return of values.

    In your function loteria() you have returns like this:

    return premio,bonus;
    

    What that code does is invoke the comma operator ( , ) on premio and bonus . The comma operator evaluates the first expression ( premio ), discards it, and executes the second expression ( bonus ) and returns that expression. So your return order is exactly the same as:

    return bonus;
    

    Well, C ++ if you allow multiple return of values if you know how to do it.

    Changing your function bool loteria() to std::pair<bool, bool> loteria() could return two values if your return statement is like this:

    return {premio,bonus};
    //     ^~~~~~~~~~~~~^ <-- construye un std::pair<bool, bool>
    

    But it forces you to double the return on all points of the function, so your return 0; when you do not get a reward should be like this:

    return {false,false};
    //     ^~~~~~~~~~~~^ <-- construye un std::pair<bool, bool>
    

    You can pick up the return of bool loteria() to C ++ 17 style:

    int main()
    {
        auto [premio, bonus] = loteria();
        return 0;
    }
    

    If your compiler does not yet support C ++ 17 you can pick up the return like this:

    int main()
    {
        auto estado = loteria();
        bool premio = estado.first, bonus = estado.second;
        return 0;
    }
    
        
    answered by 09.11.2017 в 14:17