Help with a percentage

This is a discussion on Help with a percentage within the C++ Programming forums, part of the General Programming Boards category; I am having a problem with finding percentage. When displaying output from math problems i need: # complete, Right, Wrong, ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    9

    Help with a percentage

    I am having a problem with finding percentage. When displaying output from math problems i need: # complete, Right, Wrong, and percentage right. All but percentage comes out correct. If all problems are correct it shows 100% anything else is 0%. What am I doing wrong.

    cout << setw(17) << "Addition: " << setw(16) << TotalAddProb << setw(17) << AddCountRight << setw(11) << AddCountWrong << setw(11) << setprecision(2) << fixed << ((AddCountRight) / (TotalAddProb) *100) << "\n";

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    You only get a floating point result if your operands to the division operator are floats themselves[1]. So:

    Code:
    (((float)AddCountRight) / ((float)TotalAddProb) *100.0f)
    See if that works. It forces the operation to use floating point numbers, which will result in a correct output. You see, when all is right, you get the quotient between two equal integers, which is 1. If AddCountRight decreases, it is always <1. Since the operation is based with integers, that value is truncated, so any 0.xxxxx value will become 0. If you force the operation to be float based, you'll get your results.

    [1] Actually, I am not sure if it is enough to have a float operand, so I always play safe and have both of them be treated as floats. Someone please correct me about this issue or advise a better technique.
    Last edited by Jorl17; 09-24-2010 at 05:34 PM.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    9

    Thanks

    Thanks that worked I understand where I went wrong. I am new to programming and this board has been invaluable. Thanks again..

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Only one of the operands need to be a floating number. You can also multiply by 1.0 to convert it to a floating point number without the use of casts.
    So long as the first expressions is a floating point, then the entire result will be a floating point.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Percentage Complete?
    By Abda92 in forum C++ Programming
    Replies: 16
    Last Post: 09-21-2007, 03:04 PM
  2. Calculating a percentage
    By swgh in forum C++ Programming
    Replies: 3
    Last Post: 11-24-2006, 09:24 AM
  3. Percentage
    By skyglin in forum C Programming
    Replies: 7
    Last Post: 06-23-2003, 05:18 PM
  4. Getting a percentage from a bunch of random numbers?
    By kidkash in forum C Programming
    Replies: 6
    Last Post: 02-11-2003, 07:42 AM
  5. please help me fix my coding for this assignment
    By sayeem81 in forum C Programming
    Replies: 1
    Last Post: 01-29-2002, 01:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21