Please help with one question. I am writing a program in Qt with an interface. When you click a button, certain actions must be performed, some values ​​are worth checking and QInputDialog to change the value during program execution. We wrote the do-while construction to check, if the value was entered incorrectly, the program works regularly again offering to enter the correct value, but when I enter the required value, the program just hangs and stops working. I do not understand why, when using exactly the same design, but in the console application everything works fine. Please tell me how to solve this problem.

void MainWindow::on_pushButton_clicked() { QLocale::setDefault(QLocale::C); .... bool bv_1 = true; bool ok; do { if (ver_1 < 0 || ver_1 > 1) { double test = QInputDialog::getDouble(this, "Ошибка ввода!", "Недопустимая вероятность смерти. Введите число от 0 до 1!", ver_1, -2147483647, 2147483647, 3, &ok ); if (ok) { ver_1 = test; } else { bv_1 = false; break; } } }while(bv_1); ....} 
  • First, what is the initial value of ver_1 ??? Why is this information not in question? Secondly, when you enter a value between 0 and 1 you get an infinite loop, as it is written in your code. - AnT
  • This is a decimal number that is entered into the form, the problem is clearly not in it, because Without a cycle, everything worked fine. And using exactly the same cycle, but in the console application, everything worked fine. Therefore, I did not even consider that there was a mistake - Batradz Sanakoev

1 answer 1

Obviously, the only way out of the loop is to hit the if(ok)… else branch. So the cycle must be interrupted when the number is successfully checked:

 while((ver_1 < 0) or (1 < ver_1)) { double test = QInputDialog::getDouble(this, "Ошибка ввода!", "Недопустимая вероятность смерти. Введите число от 0 до 1!", ver_1, -2147483647, 2147483647, 3, &ok ); if(ok) { ver_1 = test; } } 
  • Thanks a lot, helped. Somehow he did not think of such a thing - Batradz Sanakoev