This is code for the algebraic slope formula (y2 - y1) / (x2 - x1). I need help. My error is:

Uninitialized variable formula2 is being used without being initialized.

Can anyone help me???

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
	int formula1[2];
	int formula2[2];
	int formula3[2];
	int formula4[2];
	int B, b;
	int slope1((formula2[1] - formula1[1]) /(formula2[0] - formula1[0]));
	int slope2((formula4[1] - formula3[1]) /(formula4[0] - formula3[0]));
	cout << "Welcome to the program.  ^_^" << endl;
	cout << endl;
	cout << "What is the X number for the first slope intercept formula? ";
	cin >> formula1[0];
	cout << endl;
	cout << "What is the Y number for the first slope intercept formula? ";
	cin >> formula1[1];
	cout << endl;
	cout << "What is the X number for the second slope intercept formula? ";
	cin >> formula2[0];
	cout << endl;
	cout << "What is the Y number for the second slope intercept formula? ";
	cin >> formula2[1];
	cout << endl;
	cout << "The slope for the first equation is: " << slope1 << "x" << endl; 
	cout << endl;
	cout << "For the formula y = mx + b, your m is: " << slope1 << "x" << endl;
	cout << endl;
	cout << "What do you want b to be? ";
	cin >> B;
	cout << endl;
	cout << "What is the X number for the first slope intercept formula? ";
	cin >> formula3[0];
	cout << endl;
	cout << "What is the Y number for the first slope intercept formula? ";
	cin >> formula3[1];
	cout << endl;
	cout << "What is the X number for the second slope intercept formula? ";
	cin >> formula4[0];
	cout << endl;
	cout << "What is the Y number for the second slope intercept formula? ";
	cin >> formula4[1];
	cout << endl;
	cout << "The slope intercept for the second equation is: " << slope2 << "x" << endl;
	cout << endl;
	cout << "For the formula y = mx + b, your m is: " << slope2 << "x" << endl;
	cout << endl;
	cout << "What do you want b to be? ";
	cin >> b;
	cout << endl;
	if (slope1 == slope2 && B == b)
	{
		cout << "The Lines are the same line. " << endl;
		cout << endl;

	}
	else if (slope1 == slope2 && B != b)
	{
		cout << "The lines are parallel." << endl;
		cout << endl;
	}
	else if (slope1 != slope2 && B == b)
	{
		cout << "The lines intersect at B." << endl;
		cout << endl;
	}
	else if(slope1 != slope2 && B != b)
	{
		cout << "The lines intersect." << endl;
		cout << endl;
	}
	system("pause");
	return 0;
}

Try initializing your array elements before you pass them to your function....Just like your error message indicated.

How do I initialize???

First off this is a cool program. I'm doing something similar to this right now, distance. I would help, but I'm not 100% sure what the problem is. Though I may just be over looking it.

I have tried it a couple of times, but it seems no matter what I put in it gives me "739x" as my slope for the first line.

Am I supposed to initialize here??

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

int main()
{
	int formula1[2];
	int formula2[2];
	int formula3[2];
	int formula4[2];
	int B, b;

	int slope1 = (formula2[1] - formula1[1]) / (formula2[0] - formula1[0]);
	int slope2 = (formula4[1] - formula3[1]) / (formula4[0] - formula3[0]);
Member Avatar for MonsieurPointer

You are trying to calculate your slope before the user inputs values. You need to place int slope1 = ... and int slope2 = ... after the code where values are stored in formula[n]. Also, declaring slope1 and slope2 is dangerous, because a float / double can turn out as result from your formula.

You are trying to calculate your slope before the user inputs values. You need to place int slope1 = ... and int slope2 = ... after the code where values are stored in formula[n]. Also, declaring slope1 and slope2 is dangerous, because a float / double can turn out as result from your formula.

Yes and no. I agree with your first statement, the calculations need to be moved to a better location. In actuality, once the OP does that, there should no longer be an initialization issue because the user input is the initialization. But your second statement is a little weak.

Don't forget that C++ has 2 different division operators. Both are represented by the '/' character, but there are integer division and floating-point division versions of it. If you trace this code you will see that the division is int/int, which means this code uses the integer division version.

@OP:
I would advise you to declare slope1 and slope2 as double variables instead and make some changes to use floating-point division instead. The reason being that integer division does not produce a result with a decimal/fractional part. What is the slope of a line that rises 11 units in 3 horizontal units? The slope is 11/3 which is 3.6667, but integer division truncated the decimal part of the solution and only produces a slope of 3, which is 9/3. There is a big difference between those two solutions which makes your solution invalid.

It is very simple to make this change; simply re-define slope1 and slope2 as double values (instead of int) and cast your (x2-x1) result to a double.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.