Show Progress in % problem

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 173
Reputation: Lukezzz is an unknown quantity at this point 
Solved Threads: 1
Lukezzz Lukezzz is offline Offline
Junior Poster

Show Progress in % problem

 
0
  #1
Nov 26th, 2008
I am stuck in a problem where I should show a progress in % in a label. I have manage this code and think this should work but the label doesn´t update the % value it only shows "Progress... 0 %".
Is there anything I perheps should add to update the value in the label in realtime.
I have instead of:
Application:: DoEvents()

Also tried this without success.
  1. this->Update();
  2. this->ResumeLayout(true);

  1. //Show progress in %
  2. double ShowProgress = 0;
  3.  
  4. for( int i = 0; i < 2231; ++i )
  5. {
  6. Thread::Sleep(10);
  7. ShowProgress = (i / 2231) * 100;
  8. label1->Text = "Progress... " + Convert::ToString(ShowProgress) + " %" ;
  9.  
  10. Application::DoEvents(); //Update Form
  11. }
Last edited by Lukezzz; Nov 26th, 2008 at 8:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Show Progress in % problem

 
0
  #2
Nov 26th, 2008
You are making the mistake of assuming that integer division yields a floating point result - it doesn't: it yields an integer result and integers can't represent fractions.

In your code i/2231 is an integer division, and yields an integer result by rounding towards zero (hence always yields zero if i is in the range [0,2231) ).

You need to force the division i/2231 to be a floating point division to get the result you expect. For example;
ShowProgress = ((double)i / 2231) * 100;
or
ShowProgress = (i / 2231.0) * 100;
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 173
Reputation: Lukezzz is an unknown quantity at this point 
Solved Threads: 1
Lukezzz Lukezzz is offline Offline
Junior Poster

Re: Show Progress in % problem

 
0
  #3
Nov 26th, 2008
Yes, that was the problem

Thank you very much for your help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 337 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC