[Solved] How to stop a program if some criteria is not met



  • Hi,

    Quick question.

    I have a button and two lineEdits that when the button is clicked it calls a function called calculate() which checks the two lineEdits to see if they are empty or the input was "0", if one of them is empty or the input was "0" it displays a message but if the fields contain numbers other than "0" it executes the calculations, my problem is that if the fields are empty or input was "0" I do get the message but after I click the "ok" button on the popup message (dialog box) it goes and execute the calculations which of course makes the program hang... since there is nothing to make calculation with.

    I know this may be a C++ question but I was under the impression that when you use IF statements if a criteria isn't met the program stops there and it doesn't continues with the rest of the code (I may be thinking about while loops).

    My question is, how can I stop the program and have it re-evaluate the fields to see if they are empty or the input was "0"? In other words I don't want the program to get to the calculations code if there are no valid numbers.

    This is how my simplified code looks like:
    @void MainWindow::calculate()
    {
    // VALIDATING FIELDS
    if (partW == "" || partL == "") // if fields are empty show the following message
    {
    QMessageBox::information(this, "Warning", "Please enter a value");
    }

    else if(partW == "0" || partL == "0") // if the input is 0 show the following message
    {
        QMessageBox::information(this, "Warning", "Please enter a value other than 0");
    
    }
    else // if all fields contain numbers other than 0 or are not empty do the calculation
    {
    

    // calculations go here
    // I only want these calculation to be executed if the fields contain numbers other than 0 and are not empty of course

    }
    

    }// end calculate

    void MainWindow::on_pushButton_Calculate_clicked()
    {
    calculate();

    }//end on_pushButton_Calculate@

    Thanks a lot for your help.



  • The else branch is only executed if none of the if branches match; so if you see a message box the calculation code should not be executed (at least not in the code you've shown).

    QString already provides means of converting a string to a number (and checking the result).
    @
    bool partWIsValid = false;
    double partWValue = partW.toDouble(&partWIsValid);

    bool partLIsValid = false;
    double partLValue = partL.toDouble(&partLIsValid);

    if (partWIsValid == true && partLIsValid == true)
    {
    ...
    }
    else
    {
    QMessageBox::information(this, "Warning", "Please enter a valid number");
    }
    @


  • Moderators

    There are probably a couple of different options.

    Why aren't you checking the validity of the input directly in the on_pushButton_Calculate_clicked() method? If your criteria are not met, you shouldn't start the calculate routine. At least that would work in your sample of the calculation routine.
    When you are not starting the calculation you are probably staying in the input routine in the user may correct the input.

    Another option would be checking the value during input with QValidator.

    The calculation method is simply returning at the moment that might be already enough. The program is still running and the user might be able to input new values.

    However, this all depends on the remainder of your application.


  • Moderators

    An if statement will execute the items in its block only if the comparison criteria is true. If not, it falls through to the else block if it's not (if there is an else block.)

    In your code, you're checking two very narrow cases. One is if the box has exactly nothing in it. The other is if it has only a 0 in it. You're not catching cases where there might be white space involved as well.

    You have a few options:

    • You could check the contents with a "QRegExp":/doc/qt-4.8/qregexp.html rather than just an == in your if statements
    • You could use "QString::simplified()":/doc/qt-4.8/qstring.html#simplified to reduce down any extraneous white space in your fields.
    • (My preference) You could install a "QIntValidator":/doc/qt-4.8/qintvalidator.html (or QDoubleValidator) on your text fields to prevent anything but numbers from being entered.
    • Other things...


  • You may want to look at:
    "QLineEdit::setValidator":http://doc.qt.nokia.com/4.7-snapshot/qlineedit.html#setValidator
    This validator can also be set in the QtDesigner where you probably generated you ui.

    However, C++ is a procedural programming language, it will not stop execution anywhere like a car stops at a traffic light. But I see no error in your code. When the user clicks the pushButton, then the logic will be calculated. If requirements are not met, the last else is not evaluated and the pushButton slot will return your program's execution back to the main event routine. If the above two if statements are not true, only the final else will happen.

    EDIT: .. mid air collision ;-)



  • @Lucas Geyer

    bq. The else branch is only executed if none of the if branches match; so if you see a message box the calculation code should not be executed (at least not in the code you’ve shown).

    So, it should work.

    @Konahnig

    bq. Why aren’t you checking the validity of the input directly in the on_pushButton_Calculate_clicked() method? If your criteria are not met, you shouldn’t start the calculate routine. At least that would work in your sample of the calculation routine.
    When you are not starting the calculation you are probably staying in the input routine in the user may correct the input.

    I haven't tried this because this function will be called in multiple instances (buttons) and I didn't want to duplicate my code and because I thought that the IF statement should work, but I now think that this may be the easiest way to make it work.

    @mlong and @franku
    How can I use QLineEdit::setValidator or QIntValidator?

    I wish I could post the complete program to find out why this logic is not working, is a simple if statement, I know you guys would find the problem in a second.

    Thank you all very much for your help

    [Edit: Added bq. to quotes -- mlong]



  • You do as well. See how you expressed the if statements, what "==" operator will be called? If the statements do not exactly what you want, you missed something. I.e. how do you compete with blanks?



  • Found the problem. As you probably suspected it had nothing to do with the IF statement :(

    But thank you for clarifying my doubts about the IF statement I wouldn't find the issue since I wasn't sure if I was doing it right.

    Thank you all a lot and sorry for wasting your valuable time.


  • Moderators

    bq. Thank you all a lot and sorry for wasting your valuable time.

    It's not a problem :-) That's what we're here for! Glad you got it working. Please be sure and go back and edit the thread's original post and prepend [Solved] to the beginning of the title. Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.