Thread: if statements causing program to crash

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    7

    if statements causing program to crash

    My program crashes on runtime. It can compile, but crashes at .exe. But everything works, right up to until I run the following code:
    Code:
    cout<< "centroid y: "<<trialone.centroidy()<<endl;
    the method centroidy is defined as follows:
    Code:
    double EarthCalc::centroidy()
    {
        double firstmoment = 0.0;
        double mass1 = 0.0;
        double mass2 = 0.0;
        double count = 2159;
    
    
        for (int i = 0; i<colNum; i++ )
        {
            count--;
    
    
            for ( int j = 0; j<rowNum; j++ )
            {
                
                if ( array[i][j] > 0 )
                {
                    mass1 = mass1 + (LONDISTcalc(count/12.0 - 90)) *(LATDISTcalc(count/12.0 - 90))*(array[i][j])*2.5;
                }
                if ( array[i][j] < 0 )
                {
                    mass2 = mass2 + (LONDISTcalc(count/12.0 - 90) * LATDISTcalc(count/12.0 - 90) *array[i][j]);
                }
    
    
                firstmoment = firstmoment + (j/12.0 - 90)*LONDISTcalc(count/12.0 - 90)*LATDISTcalc(count/12.0 - 90);
            }
        }
        return (firstmoment/(mass1 + mass2));
    }
    Ive narrowed the problem down to the 2 if statements. For some reason, the if statements crash the program, even though I checked the code like 50 times and saw nothing wrong. I am accessing a huge array of like 18 MB, but still, I accessed that array before in other methods of the class as well. But it didnt crash. WTF?

    Anyways, I've also attached the 3 files with all the code as well. If anyone can help me, thanks.

    UPDATE: I fixed the code by deleting the if statements entirely. I used some of the other methods in the class to work around using ifs and only using the fors. But it would be still nice if someone can tell me why it crashed me
    Attached Files Attached Files
    Last edited by mantracker; 11-10-2012 at 08:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array causing program to crash
    By rTeapot in forum C++ Programming
    Replies: 11
    Last Post: 07-03-2012, 10:40 AM
  2. Is my code causing my system to crash?
    By camel-man in forum C Programming
    Replies: 1
    Last Post: 04-14-2011, 05:54 PM
  3. fscanf causing a crash
    By dougwilliams in forum C Programming
    Replies: 6
    Last Post: 11-18-2007, 04:52 PM
  4. what is causing this seemingly simple app to crash?
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 08:36 PM
  5. allegro causing a crash
    By kooma in forum Game Programming
    Replies: 5
    Last Post: 04-06-2002, 02:01 PM