In general, I encountered such a problem as entering a number in the console. So it turned out that the console displays the numbers is not quite correct as we would like. enter image description here or enter image description here

In pure Qt, I would stuff "input" into QString, and there I would have processed it. BUT std :: cin does not support QString. Of course, you can set char [256] and process it, but it has a finite size and this is very sad; _: because I want to do everything correctly , or better through char [256] and when reaching a char size of more than 256, tell the user that he has no right to drive a larger number ???

I would even say how to act in such situations ??

Here is the code, but it is not really about the question (just added for a tick):

int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); double A_d = 0; double B_d = 0; QTextStream outStream(stdout); outStream.setCodec(QTextCodec::codecForName("cp866")); outStream << QString("Введите первую сторону треугольника: \n А = ") << flush; while(!(std::cin >> A_d)){ outStream << QString("Ошибка ввода, пожалуйста введите число заново.") << flush; std::cin.clear(); std::fflush(stdin); } outStream << QString(" Мы тут !!! ") << flush; outStream << A_d << flush; return a.exec(); } 
  • one
    I won’t say about QString , I don’t know, although it seems strange to me that developers couldn’t write appropriate functions for it ... But who prevents to work with string ? Why at once extreme - char[] ? - Harry
  • one
    The question is what? Need to enter doubles in hexadecimal? - bipll

1 answer 1

You already use QTextStream for output, why not use it for input instead of std::cin ?

 QTextStream in(stdin); QString str; in >> str; 

Even if for some reason you need std::cin , then, in extreme cases, you can always read into std::string and convert it to a QString

  • 0_o this is a turn, I really did not know about it. - timob256
  • or overload operator >> for QString - Bogdan