imtinan
imtinan

Reputation: 181

Programme is terminating in c++;

I have written the code which will print the slope from the coordinates of a line and then print it. but when I give input, my code is terminating. what is the problem?

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int x1,y1,x2,y2,m;

    cin>>x1>>y1>>x2>>y2;

    m=(y1-y2)/(x1-x2);

    cout<<m;
}

Upvotes: 0

Views: 100

Answers (3)

NAND
NAND

Reputation: 687

Consider adding more corner tests, also note when x1 is equal to x2 you will face division by zero error, so you should modify your program with if statement to check that they are not equal.

so you should add this to your code:

if(x1==x2){
  cout<<"Error division by zero"<<endl;
  return 1;
}

Upvotes: 1

ufoq
ufoq

Reputation: 103

Problem is probably here m=(y1-y2)/(x1-x2);. When x1==x2 you have division by zero, please add some checks.

Upvotes: 4

omri klein
omri klein

Reputation: 146

I tested your code using c++ compiler online and it worked.

You may have a compiler error or something else.

Upvotes: -1

Related Questions