Annoying bug I can't find or fix!

This is a discussion on Annoying bug I can't find or fix! within the C++ Programming forums, part of the General Programming Boards category; I've got a question on a program I've been trying to write (I'm pretty much done except for one annoying ...

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    Annoying bug I can't find or fix!

    I've got a question on a program I've been trying to write (I'm pretty much done except for one annoying bug). The bug (in the program below) occurs right above where the bolded comment is and for the rest of the program, however, doesn't occur before this (I proved this with the numerous 'cout' statements). I don't know why it occurs or how it occurs, but it does. The program I'm writing tells two variations of the equation of the line that passes through the two points entered and the slope of that line. All goes well when initializing the pointArrays, the right values are stored (at least I think); x1 and y1 for pointArray11 and x2 and y2 for pointArray21. I've proved this by adding cout statements after each initialization index. Now this is where I'm stumped: in the cout statement that displays all of the values together, pointArray11[0] changes its value to the value stored in pointArray21[1] and all others still hold their correct values. I have no idea how this happens! pointArray11[0] retains the value stored in pointArray21[1] for the rest of the program. Can anyone see why this is happening? I sure can't. Thanks to anyone who spends their time to solve my pathetic program's problem.

    Code:
    #include <iostream>
    
    using namespace std;
    
    double yIntercept(double rise, double run, double xcoor, double ycoor);
    
    int main()
    {
        int cont;
        double pointArray11[1];
        double pointArray21[1];
        
        cout << "This program finds the slope and two variations of the equation of the line that passes through these two points."<<endl;
        while(cont!=0)
        {  
            cout << "Please enter the coordinates of the first point." << endl;
            for(int coor = 0; coor<2; coor++)
            {
                double* coordinateSpace1 = new double;
                cout << "Coordinate?";
                cin  >> *coordinateSpace1;
                pointArray11[coor] = *coordinateSpace1;
                delete coordinateSpace1;
            }
            cout << pointArray11[0]<<pointArray11[1]<<endl;
            
            
            cout << "Please enter the coordinates of the second point." << endl;
            for(int rooc = 0; rooc<2; rooc++)
            {
                double* coordinateSpace2 = new double;
                cout << "Coordinate?";
                cin  >> *coordinateSpace2;
                pointArray21[rooc] = *coordinateSpace2;
                delete coordinateSpace2;
            }
            cout << pointArray21[0]<<pointArray21[1]<<endl;    
            
            cout << pointArray11[0]<<pointArray11[1]
                 << pointArray21[0]<<pointArray21[1]<<endl;
            //pointArray11[0] changes to same value as pointArray21[1] during the cout statement immediately above and for the rest of the program     
            
            double slope1 = pointArray21[1] - pointArray11[1];
            double slope2 = pointArray21[0] - pointArray11[0];
            cout << "The slope of the line is: " << slope1 <<"/" << slope2 << endl;
            cout << "The equation for the line is: y-" << pointArray11[1] << "=" << slope1 << "/"<<slope2 << "(x-"<< pointArray11[0] <<")"<<endl;
            cout << "The slope-intercept form of this equation is: y=" <<slope1<<"/"<<slope2<<"x+"<<yIntercept(slope1, slope2, pointArray11[0], pointArray11[1])<<endl;
            cout << "Continue? Press zero to quit and any number to continue." <<endl;
            cin  >> cont;
        }       
        
        return 0;
    }
       
    double yIntercept(double rise, double run, double xcoor,  double ycoor)  
    {
        double b = rise/run*(0-xcoor)+ycoor;
        return b;
    }

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Lightbulb

    Code:
    #include <iostream>
    
    using namespace std;
    
    double yIntercept(double rise, double run, double xcoor, double ycoor);
    
    int main()
    {
        int cont;
        double pointArray11[2];
        double pointArray21[2];
        
        cout << "This program finds the slope and two variations of the equation of the line that passes through these two points."<<endl;
        while(cont!=0)
        {  
            cout << "Please enter the coordinates of the first point." << endl;
            for(int coor = 0; coor<2; coor++)
            {
                cout << "Coordinate?";
                cin  >> pointArray11[coor];
            }
    
            cout << pointArray11[0]<<pointArray11[1]<<endl;
            
            
            cout << "Please enter the coordinates of the second point." << endl;
            for(int rooc = 0; rooc<2; rooc++)
            {
                cout << "Coordinate?";
                cin  >> pointArray21[rooc];            
            }
    
            cout << pointArray21[0]<<pointArray21[1]<<endl;    
            
            cout << pointArray11[0]<<pointArray11[1]
                 << pointArray21[0]<<pointArray21[1]<<endl;
            //pointArray11[0] changes to same value as pointArray21[1] during the cout statement immediately above and for the rest of the program     
            
            double slope1 = (pointArray21[1] - pointArray11[1]);
            double slope2 = (pointArray21[0] - pointArray11[0]);
            cout << "The slope of the line is: " << slope1 <<"/" << slope2 << endl;
            cout << "The equation for the line is: y-" << pointArray11[1] << "=" << slope1 << "/"<<slope2 << "(x-"<< pointArray11[0] <<")"<<endl;
            cout << "The slope-intercept form of this equation is: y=" <<slope1<<"/"<<slope2<<"x+"<<yIntercept(slope1, slope2, pointArray11[0], pointArray11[1])<<endl;
            cout << "Continue? Press zero to quit and any number to continue." <<endl;
            cin  >> cont;
        }       
        
        return 0;
    }
       
    double yIntercept(double rise, double run, double xcoor,  double ycoor)  
    {
    	if(run)
    	{
    
    		double b = ((rise/run)*(0-xcoor)+ycoor);		
                    return b;
    	}
    
    	else
    	{
    		cout << "\n\n\t\aDivision By Zero Error! ";
    		return 1;
    
    	}
    
    
    }




    There are just two issues with your program... you declared your arrays to have only one element.... so your 'for' loops are actually accessing the array out of bounds on the last iteration (this is known as an, "off-by-one" error) so in your array delcarations, I added an additional element. array[2] has elements [0] and [1]

    Also, in your intercept function you have a division with a variable in the denominator.. so you should somehow account for a 'division by zero'.
    Last edited by The Brain; 11-21-2004 at 11:21 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Ah, I see now. The array declarations were the main problem. Thanks alot for pointing that out to me! Never did get a divide by zero, though.

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    you should handle a 'division by zero' exception anytime you have a variable in the denominator.. as the domain for any denominator is all real numbers except for 0.

    Use some test data:

    (0,1) and (0,2) will not have a "run" (the slope for this vertical line is "undefined")

    you should test your algorithms at the boundries.. looking for zeros is always a good test.
    Last edited by The Brain; 11-21-2004 at 12:20 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 10:13 AM
  2. linux find utilty
    By vaibhavs17 in forum Tech Board
    Replies: 5
    Last Post: 05-12-2009, 05:40 AM
  3. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM
  4. RE: Find out running processes from command prompt
    By sampatel in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-18-2001, 08:15 AM
  5. cant find the errors.
    By sscook69 in forum C++ Programming
    Replies: 2
    Last Post: 09-10-2001, 05:26 PM

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