By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,499 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,499 IT Pros & Developers. It's quick & easy.

Double Returning 0 - Visual C++

compman9902
100+
P: 105
Hello, my problem is that everytime I run my code (below) the variable "dPercent" returns a zero value. Thank you for your time and help.

Expand|Select|Wrap|Line Numbers
  1. String^ originalString = textBoxDecrypt->Text;
  2. String^ sPercent;
  3. String^ currentChar;
  4. long double   dPercent;
  5. int     level = int(numEncrypt->Value);
  6. int     iPercent;
  7. int     position = 0;
  8. clearFields();
  9. textBoxDecrypt->Text = originalString;
  10. numEncrypt->Value = level;
  11. progressBarEncrypt->Maximum = originalString->Length;
  12. while (position < originalString->Length) {
  13.     currentChar = originalString->Substring(position, 1);
  14.     textBoxEncrypt->Text = textBoxEncrypt->Text + "[" + currentChar + "]";
  15.  
  16.     dPercent = (position / originalString->Length) * 100;
  17.     iPercent = int(dPercent);
  18.     progressBarEncrypt->Value = position;
  19.  
  20.  
  21.  
  22.     sPercent = iPercent.ToString();
  23.     labelEncrypt->Text = "%" + sPercent;
  24.  
  25.  
  26.     MessageBox::Show("(Position/Length): " + position + "/" +   originalString->Length + "\r\ncurrentChar: " + currentChar + "\r\ndPercent: " + dPercent + "\r\niPercent: " + iPercent + "\r\nPercent Equation: " + "((" + position + "/" + originalString->Length + ") * 100 )","DEBUG: Variables",MessageBoxButtons::OK,MessageBoxIcon::None);
  27.     position = position + 1;
  28. }
  29. labelEncrypt->Text = "%" + "100";
  30. progressBarEncrypt->Value = originalString->Length;
Nov 13 '08 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
It looks like dPercent is zero becuse in line 16 position appears to be zero.
Nov 13 '08 #2

Ganon11
Expert 2.5K+
P: 3,652
I'm not sure about this, because you seem to be using C# or C.NET, but when you divide an int by an int (i.e. position by originalString->Length), you always get an int, never a double. Maybe you could compute

Expand|Select|Wrap|Line Numbers
  1. double dPercent = (100.0 * position) / originalString->Length;
Nov 13 '08 #3

compman9902
100+
P: 105
I'm not sure about this, because you seem to be using C# or C.NET, but when you divide an int by an int (i.e. position by originalString->Length), you always get an int, never a double. Maybe you could compute

Expand|Select|Wrap|Line Numbers
  1. double dPercent = (100.0 * position) / originalString->Length;
That worked great, Thanks
Nov 13 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.