CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2008
    Posts
    44

    Float display as 0

    Hi,

    The following only outputs a zero. Not sure why.

    cout << "Percent Connected: " << (Ccounter/(totalElements*6))*100 << " %\n\n" << endl;

    Ccounter and totalElements are Ints.

    Thanks

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Float display as 0

    Integer arithmetics rounds everything down to the next smaller int. Try to convert your operands to double before dividing.
    - Guido

  3. #3
    Join Date
    May 2002
    Posts
    1,435

    Re: Float display as 0

    Casting one of the ints should do it, then all other ints will be promoted in the calculation.

    cout << "Percent Connected: " << ((double)Ccounter/(totalElements*6))*100 << " %\n\n" << endl;
    Last edited by 0xC0000005; August 1st, 2008 at 11:24 AM. Reason: remove quoted text

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured