it is not working on windows. i am unable to find the error!!

#include <iostream>
using namespace std;
class ratio{
public:
ratio(int n=0, int d=1): num(n), den(d) { reduce(); }
void print() const { cout<<num<<"/"<<den<<endl; }
private:
int num, den;
void reduce();
};
void swap(int &a,int &b)
{ int temp=a; a=b; b=temp; }

int gcd(int m, int n)
{ if(n>m) swap(m,n);
while(n>0) { int temp=m%n; m=n; n=temp; }
return n; }

void ratio::reduce()
{
int g=gcd(num,den);
num/=g;
den/=g; }

int main ()
{ ratio x(360,100);
x.print();
system("pause");
return 0;
}
Look at gcd fnction. It isnt working correctly.
while(n>0) { int temp=m%n; m=n; n=temp; }
loop will terminate only when n == 0; (n won't be negative if both n and m is positive)
then you do this return n;. You returning zero here. And then:
1
2
int g=gcd(num,den);
num/=g;

TA-DA! Division by zero.
Thanks a lot !!
Topic archived. No new replies allowed.