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;
}