Double Returning 0 - Visual C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • compman9902
    New Member
    • Mar 2007
    • 105

    Double Returning 0 - Visual C++

    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.

    [CODE=cpp]String^ originalString = textBoxDecrypt->Text;
    String^ sPercent;
    String^ currentChar;
    long double dPercent;
    int level = int(numEncrypt->Value);
    int iPercent;
    int position = 0;
    clearFields();
    textBoxDecrypt->Text = originalString;
    numEncrypt->Value = level;
    progressBarEncr ypt->Maximum = originalString->Length;
    while (position < originalString->Length) {
    currentChar = originalString->Substring(posi tion, 1);
    textBoxEncrypt->Text = textBoxEncrypt->Text + "[" + currentChar + "]";

    dPercent = (position / originalString->Length) * 100;
    iPercent = int(dPercent);
    progressBarEncr ypt->Value = position;



    sPercent = iPercent.ToStri ng();
    labelEncrypt->Text = "%" + sPercent;


    MessageBox::Sho w("(Position/Length): " + position + "/" + originalString->Length + "\r\ncurrentCha r: " + currentChar + "\r\ndPerce nt: " + dPercent + "\r\niPerce nt: " + iPercent + "\r\nPercen t Equation: " + "((" + position + "/" + originalString->Length + ") * 100 )","DEBUG: Variables",Mess ageBoxButtons:: OK,MessageBoxIc on::None);
    position = position + 1;
    }
    labelEncrypt->Text = "%" + "100";
    progressBarEncr ypt->Value = originalString->Length;[/CODE]
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It looks like dPercent is zero becuse in line 16 position appears to be zero.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      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

      Code:
      double dPercent = (100.0 * position) / originalString->Length;

      Comment

      • compman9902
        New Member
        • Mar 2007
        • 105

        #4
        Originally posted by Ganon11
        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

        Code:
        double dPercent = (100.0 * position) / originalString->Length;
        That worked great, Thanks

        Comment

        Working...