Thread: Funny things happening to values outside of function

  1. #1
    Registered User
    Join Date
    Apr 2003

    Funny things happening to values outside of function

    Hi again all,
    This time i'm having trouble in returning a value from a class function. The function is supposed to work out the euclidian distance between two points i.e. sqrt( (x1-x2)squared + (y1-y2)squared). The distance i'm working out is over 10 points. When I was running the function in MSDOS it worked fine, however now that i'm tyring to run the program in MFC windows things aren't quite working.
    Here is the function for calculating the length:

    int CLine::SectionLength(int m_XYCoOrds[][2])
    	int TotalLength =0;
    	int SectionLength =0;
    	int Xvariance = 0;
    	int Yvariance = 0;
    	for(int i=0;i<8;i++)
    		variance = (m_XYCoOrds[i][0] - m_XYCoOrds[i+1][0]);
    		Yvariance = (m_XYCoOrds[i][1] - m_XYCoOrds[i+1][1]);
    		SectionLength = (sqrt( (Xvariance*Xvariance) + (Yvariance*Yvariance)));
    		TotalLength = TotalLength + SectionLength;
             return TotalLength;
    where m_XYCoOrds is a 2d array with i referring to the current point (0-9) and [i][0] being x, [i][1] being y.

    The TotalLength value returned is stored in the object under the name m_Length.

    When I try and see what the value is i call this in the View.cpp file

    CString test; //string to show the total length number
    int test3  =  pDoc->LineArray[0].m_Length;//get the first lines length
    test.Format(_T("test length is %d",test3));
    this has been producing the value -842150451
    I don't think its just a problem with the test.Format line as I can access other data members in the class that are initialised fine.
    As a quick test before wasting anyones time I set the function to just return the value 15, however m_Length was still showing -842150451. Can anyone tell me where this magic number has come from.
    Any help whatsoever is again greatly appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Well that negative number is probably what the variable is set to if it is not initialized. Its just a garbage value. Why do you instantiate a class then use pDoc-> and then access the presumably member variable of that class? You never even call your function. I'm not quite sure of your logic here.

  3. #3
    Registered User
    Join Date
    Apr 2003


    yes you are right, sorry to have completely wasted your time. I was calling the random function in a different place, and when running it was never actually calling the length function. My apologies but thanks for taking the time to look through it for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM