-1

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;
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Micky
  • 17
  • 3
  • 4
    `main` asks the user to input some numbers, and passes them along to `findSlope`. The latter completely ignores those parameters, asks the user to enter still more numbers, and performs calculations on those. Did you mean it this way? – Igor Tandetnik Nov 07 '17 at 23:40
  • 2
    Don't *define* functions in header files. [Don't do `using namespace std;` in header files](https://stackoverflow.com/questions/14575799/using-namespace-std-in-a-header-file) (or [in general](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)). Learn about [header include guards](https://en.wikipedia.org/wiki/Include_guard) and [`#pragma once`](https://en.wikipedia.org/wiki/Pragma_once). And read compiler warning (and if you don't get any then enable more). – Some programmer dude Nov 07 '17 at 23:42
  • 2
    Your `for` loop calculates the same thing `rep` times. Did you mean it this way? – user207421 Nov 07 '17 at 23:54
  • 3
    `findSlope` doesn't have a return type. Your compiler probably assumes `int`, and re-interprets the `float` return value as `int`. It should be `float findSlope(...) {...}` – Cris Luengo Nov 08 '17 at 00:12
  • 1
    Common rule of thumb is to place function definitions (code) into a source file, e.g. *.cpp or *.cc. Header files are used for function declarations. – Thomas Matthews Nov 08 '17 at 00:31
  • 1
    Your `findSlope` function doesn't use the `rep` parameter. You should get rid of it. – Thomas Matthews Nov 08 '17 at 00:32
  • 2
    You may want to clarify your terminology. A *point* has two *ordinates*. When you prompt the User, you are asking for 4 *ordinates* or 2 *points*. – Thomas Matthews Nov 08 '17 at 00:35

1 Answers1

1

In principle this code should not compile in C++, because you don't specify the type of the findSlope() function.

If you force compilation with the -fpermissive compiler flags, the function will be considered as returning an int. The conversion from float to int could explain the weird behavior that you describe (truncating and negative in case of overflow in the conversion).

Try to correct the code with:

float findSlope(float p1,float p2,float p3,float p4,float rep)
{
    ...
}

Miscellaneous remarks:

  • you could consider using double instead of float. The precision is higher and the overhead with modern CPU is not so high.
  • you do two things in findSlope.h header that you shouldn't do: first you use a namespace that would then contaminate all the files including that header; second you define a function, so that if you include the same header in different compilation units, each .cpp file would recompile the function, which might lead to linker errors.
Christophe
  • 68,716
  • 7
  • 72
  • 138