Thread: Simple Code error

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    102

    Simple Code error

    Hey all, I've been programming for a while, and I can't seem to figure out this little error.

    Code:
    #include <iostream>
    
    int main()
    {
        int Players = 12;  //12 Players
        
        float PChances[Players]; //Each Player has his/her own chance to draw shooter
        
        float PreviousChances = 0;
        
        int y = 12;
        
        for(int x = 0; x < 12; ++x)
            PChances[x] = 0;
        
        for(int x = 0; x < 12; ++x)
        {
            if(y > 0)
                PChances[x] = ( 1/y );  //Keeps returning as 0
    
            std::cout << 1/y;
                
            --y;
        }
        
        if(PChances[3] > 0)
            std::cout << "TRUE";
            
        std::cin.get();
    }
    The output is:
    000000000001

    Anything below 12 is resulting in 0
    If you can tell me why 1/y is always equaling 0 i would appreciate it greatly.
    Last edited by JJFMJR; 08-10-2007 at 11:14 PM.
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Integer division: 1/12 = 0
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    I thought that might be the problem, however I've been absent for so long i guess i forgot regular division. How can i not get 0?
    Last edited by JJFMJR; 08-10-2007 at 11:19 PM.
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How can i not get 0?
    Do a 1.0/y.

    Also, I note that you are using a variable length array extension by accident. You probably want to write:
    Code:
    const int Players = 12;  //12 Players
    
    float PChances[Players] = {0}; //Each Player has his/her own chance to draw shooter
    With the above code, you no longer need a loop to initialise all the elements to 0. I think that you might as well use double instead of float. Floating point literals are of double type unless otherwise specified.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM