This HAS to be some stupid error on my part..

This is a discussion on This HAS to be some stupid error on my part.. within the C++ Programming forums, part of the General Programming Boards category; Well I excerpted as much as possible to make it easier to determine the problem, and I'm sure it's something ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    This HAS to be some stupid error on my part..

    Well I excerpted as much as possible to make it easier to determine the problem, and I'm sure it's something really simple, but I just started programming a week ago and I'm just trying to learn one problem at a time, and this one has me stumped

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int possible [10];
        int total [10];
        
        possible [1] = 5;
        total [1] = 7;
        
        cout<<possible [1] / total [1] * 100;
        cin.get();
    }
    Why won't it give me a percentage? It outputs 100 if both arrays are set to 1, but any other combination yeilds 0. And it's kind of important that I use arrays here.. Can anyone help?

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Because you're using integer math.

    5/7 = 0 in integer math.

    You need to either make your arrays floating-point types, or cast them to floating-point types before the division.

    Or, you could do the multiplication first and then the division, as long as you're aware that, for example, 67.99% would round down to 67%.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    2
    It was THAT easy.. Thanks!

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    When you divide an integer by an integer, the return value is an integer as well. So you need to change the type of a variable (in this case, to a float) when you're dividing two variables.

    Code:
    cout << static_cast<float>(possible [1]) / total [1] * 100;
    Also, though multiplication and division are supposed to be equal in terms of precedence you'll get the following result if you don't bracket it correctly:

    Code:
    possible / (total * 100)
    You need to explicitly bracket it as:
    Code:
    cout << (static_cast<float>(possible [1]) / total [1]) * 100;

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Don't do all that casting. Make the variables floats in the first place.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    Let me suggest double, instead. Float in my opinion should only be used on those cases where there is a real intention of limiting precision. With only 6 digits of guaranteed precision, float is usually too small for most operations.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A stupid question...probably
    By Phoenix_Rebirth in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2007, 04:53 PM
  2. RichEdit, how to centralise the focused part?
    By Zahl in forum Windows Programming
    Replies: 1
    Last Post: 11-25-2002, 02:35 PM
  3. Stupid people should wear SIGNS
    By C_Coder in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 07-15-2002, 08:49 AM
  4. Beginning MFC (Prosise) Part III - Now What? :: C++
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2002, 05:58 PM
  5. Stupid over reaction
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-30-2001, 08:03 PM

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