Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Hanging when working with basic types in Qt

Hanging when working with basic types in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 889 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by tomy
    #1

    In a program, I want to display the number 0.12345 as .12345 (removing the first zero) in a lineEdit. For this I wrote the simple code below:

    QString s;
    QTextStream ss(&s);
    
      double temp = 0.12345;
      int n = 0;
    
     if(temp > 0)
        {
          ss << ".";
         while(true)
           {
             temp *= 10;
             n = temp;
             if(temp == n)
                 break;
           }
         ss << n;
      }
    
     lineEdit-> setText(s);
    

    When I run the it, the compiler (Qt Creator) hangs and I need to rerun the code to normally go out of it.

    What is the problem that the compiler acts that way, please, and how to solve it?

    K 1 Reply Last reply
    0
    • tomyT tomy

      In a program, I want to display the number 0.12345 as .12345 (removing the first zero) in a lineEdit. For this I wrote the simple code below:

      QString s;
      QTextStream ss(&s);
      
        double temp = 0.12345;
        int n = 0;
      
       if(temp > 0)
          {
            ss << ".";
           while(true)
             {
               temp *= 10;
               n = temp;
               if(temp == n)
                   break;
             }
           ss << n;
        }
      
       lineEdit-> setText(s);
      

      When I run the it, the compiler (Qt Creator) hangs and I need to rerun the code to normally go out of it.

      What is the problem that the compiler acts that way, please, and how to solve it?

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @tomy

      You programmed an endless loop!

      Not the compiler nor Qt creator hangs. Your application sucks the whole computation power until it crashes.
      You are comparing a double with an integer. The integer gots converted to a double, but because of the conversion both numbers are never the same and your break is never triggered.

      Probably something like the following may work

       while(true)
      {
               temp *= 10;
               n = temp;
               if( fabs ( temp -n) < 0.000001 )
                   break;
      }
      

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      2
      • K Offline
        K Offline
        koahnig
        wrote on last edited by koahnig
        #3

        A bit of description of the problem you are having you can find after the first headline Comparing for equality

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        2
        • nestoracN Offline
          nestoracN Offline
          nestorac
          wrote on last edited by
          #4

          We can't compare a double to an int. Anyways, I find the "break;" disturbing. I don't feel comfortable with it. I would try to remake this code.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved