mercredi 2 août 2017

C++ If statement misbehaving

I'm quite a newbie (started to learn about coding just like 2 weeks ago) and I'd really appreciate some help to explain why my code isn't working. I wrote a simple code to calculate the probability. That's not really important part. The code requires some user input so in order to make it kinda foolproof, I wrote two if statements for various kind of wrong input (wrong data type, number too high, number too low) to throw an error message and kill the program. And if the input is right (means none of the conditions of the if statements is true), it should call a function diceTwo(des). However, if I input number 8 - which should be allright - for some reason that I can't figure out - it triggers the number too high/low if statement, so no matter what number I enter, the function never gets called.

    cin >> des;

    if (cin.fail())
    {
        cin.clear();
        cin.ignore(100, 'n');
        cout << "Invalid input! Use only whole positive numbers!" << endl;
        cout << "Try again." << endl;

        return -1;
    }

    if (des> 12 || des< 1);
    {
        cout << "Invalid input!" << endl;
        cout << "Remember the only possible values are from 2 to 12!" << endl;
        cout << "Try again." << endl;

        return -1;
    }

    diceTwo(des);

    cout << "The probability of this roll is " << x << "%" << endl;

This is the part that makes troubles. I already tried to split the problematic if statement into two (one for <1, one for >12), or adding a new if statement for des>1 && des<12 to call the function, none of it worked. I worked with if statements to provide foolproofness for user input few times before, always worked well, so I really can't seem to find what's wrong this time. Anyone could tell me how to fix it please?

Aucun commentaire:

Enregistrer un commentaire