What is the proper way to manage multiple possibilities?

2

Each possibility has an answer. I know it should be done with switch / case , but I understand that it only works with constants, and what I use is a variable, right? I tried to use it and it gave me syntax errors.

What I have done seems like a fudge.

I explain what it is supposed to do: when I invoke the function I pass a 1 or a 0. If I pass a 1, I am in the field of positive responses and if something else happens (in this case a 0) , in the of the negatives. Then within each area generates a random number that goes from 1 to 4, and depending on the number that comes out must issue a response.

void respramdom(int x1)
{
    if (x1 == 1)
    {
        int numaleatorio = randomInt2(engine);

        if (numaleatorio == 1)
            cout << "Very good!\n";
        else if(numaleatorio == 2)
            cout << "Excellent\n";
        else if(numaleatorio == 3)
            cout << "Nice work!\n";
        else
            cout << "Keep up the good work!\n";
    }

    else
    {
        int numaleatorio = randomInt2(engine);

        if (numaleatorio == 1)
            cout << "No. Please try again.\n";
        else if (numaleatorio == 2)
            cout << "Wrong. Try once more.\n";
        else if (numaleatorio == 3)
            cout << "Don't give up!\n";
        else
            cout << "No. Keep trying.\n";
    }
}
    
asked by Gojira 09.03.2018 в 16:38
source

1 answer

1
  

I should do it with switch / case , but I understand that it only works with constants.

The switch works with constants and variables, the case requires constants. Therefore your code could look like this:

void respramdom(int x1)
{
    if (x1 == 1)
    {
        switch (randomInt2(engine))
        {
            case 1:
                cout << "Very good!\n";
                break;
            case 2:
                cout << "Excellent\n";
                break;
            case 3:
                cout << "Nice work!\n";
                break;
            default:
                cout << "Keep up the good work!\n";
                break;
        }
    }
    else
    {
        switch (randomInt2(engine))
        {
            case 1:
                cout << "No. Please try again.\n";
            case 2:
                cout << "Wrong. Try once more.\n";
            case 3:
                cout << "Don't give up!\n";
            default:
                cout << "No. Keep trying.\n";
        }
    }
}

But you do not even need a switch for this task:

void respramdom(int x1)
{
    static const std::string respuestas[4][2]
    {
        {"Keep up the good work!", "No. Keep trying."},
        {"Very good!",             "No. Please try again."},
        {"Excellent",              "Wrong. Try once more."},
        {"Nice work!",             "Don't give up!"},
    };

    int numaleatorio = randomInt2(engine);
    numaleatorio = ((numaleatorio > 0) && (numaleatorio < 4)) ? numaleatorio : 0;
    x1 = x1 == 1 ? 0 : 1;

    std::cout << respuestas[numaleatorio][x1] << '\n';
}
    
answered by 13.03.2018 в 09:59