cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
it is not working on windows. i am unabl
it is not working on windows. i am unable to find the error!!
Jan 28, 2013 at 5:39pm UTC
newbie456
(35)
#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;
}
Jan 28, 2013 at 6:53pm UTC
MiiNiPaa
(8886)
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.
Jan 28, 2013 at 7:06pm UTC
newbie456
(35)
Thanks a lot !!
Topic archived. No new replies allowed.