I'm trying to calculate slope input by user by using a function defined in a header file and returning the slope to the main function.
My problem right now is that sometimes the slope that the program calculate is wrong even though my formula is correct.
Also sometimes the slope given is just rounded up or down and randomly negative. Did I do anything wrong here?
My main code:
#include <iostream>
#include "findSlope.h"
using namespace std;
int main()
{
float p1, p2, p3, p4, rep, slope;
int i;
cout << "input point 1:";
cin >> p1;
cout << "input point 2:";
cin >> p2;
cout << "input point 3:";
cin >> p3;
cout << "input point 4:";
cin >> p4;
cout << "input amount of repetition:";
cin >> rep;
cout << "\nYour points are =" << p1 << "\t"
<< p2 << "\t" << p3 << "\t" << p4;
for ( i=0;i<rep;i++)
{
slope = findSlope(p1,p2,p3,p4,rep);
cout << "Point 1\tPoint2\tSlope\n";
cout << "("<<p1<<","<<p2<<")\t";
cout << "("<<p3<<","<<p4<<")\t";
cout << slope;
}
return 0;
}
My header file:
#include <iostream>
using namespace std;
findSlope(float p1,float p2,float p3,float p4,float rep)
{
float slope;
cout << "\nInput your first coordinates (seperated by space) :";
cin >> p1 >> p2;
cout << "Input your second coordinates (seperated by space) :";
cin >> p3 >> p4;
slope = (p4-p2)/(p3-p1);
return slope;
}