floating number/percentage outputiing issue

This is a discussion on floating number/percentage outputiing issue within the C++ Programming forums, part of the General Programming Boards category; i cant figure out how to display the damn floating number that represents percentage. Code: #include <iostream> using namespace std; ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    floating number/percentage outputiing issue

    i cant figure out how to display the damn floating number that represents percentage.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        const int theNumber = 8888;
        float percentage;
    
        for(int n = 0; n <= theNumber; n++)
        {
            percentage = (n / theNumber * 100);
            cout<<percentage<<" - "<<n<<"-> I'll be an excellent programmer."<<endl;
        }
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What is your output on what you have?

    If the precision is the problem look here

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Code:
    percentage = (n / theNumber * 100);
    This is performing integer math, not floating-point math.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    If the result is less than 100%, then the decimals would be chopped off and you get 0, multiply it by 100 which is still 0.
    You need to change one of the sides to a float or double.
    For example:
    percentage = (n * 1.0f / theNumber * 100);
    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. Strange issue in floating point addition
    By stephenwalter in forum C Programming
    Replies: 6
    Last Post: 02-23-2010, 01:28 AM
  2. floating point number comparison
    By lehe in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2009, 07:11 PM
  3. Testing Floating Point Return Values
    By jason_m in forum C Programming
    Replies: 5
    Last Post: 08-15-2008, 01:37 PM
  4. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  5. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM

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