Reputation: 33
I am just learning C++ and have an assignment where things have to be output as fractions. I have never worked in C++ so I am not quite sure if I am doing this correctly. My program lets me type in the first 2 fractions, then it crashes (which I am assuming has something to do with my functions and how I implemented them in int main ) I normally would go to a tutor, but unfortunately the school I go to doesn't offer tutors for this subject, and I transferred from a school which taught C, so I am really struggling! Any help is greatly appreciated (:
#include<iostream>
using namespace std;
class gcd
{
public:
void finder();
void rename();
void add();
void subtract();
void multiply();
void divide();
void print();
private:
int n, d, n1, d1, temp1, temp2;
} g1;
void gcd::finder()
{
temp1 = n;
temp2 = d;
while (n != d)
{
if (n > d)
n = n - d;
else
d = d - n;
}
n1 = temp1 / n;
d1 = temp2 / d;
}
void gcd::rename()
{
n1 = n;
d1 = d;
}
void gcd::add()
{
n1 = (n1 * d) + (n * d1);
d1 = (d1 * d);
g1.finder();
}
void gcd::subtract()
{
n1 = (n1 * d) - (n * d1);
d1 = (d1 * d);
g1.finder();
}
void gcd::multiply()
{
n1 = n * n1;
d1 = d * d1;
g1.finder();
}
void gcd::divide()
{
n1 = n1 * d;
d1 = d1 * n;
g1.finder();
}
void gcd::print()
{
cout << n1 << "/" << d1 << endl;
}
int main()
{
int n, d;
cout << "Please enter 5 fractions with a space between the numerator and denominator" << endl;
cout << "For example, input 2/3 as 2 3" << endl;
cout << "Enter 1st fraction: ";
cin >> n >> d;
g1.rename();
cout << "Enter 2nd fraction: ";
cin >> n >> d;
g1.divide();
cout << "Enter 3rd fraction: ";
cin >> n >> d;
g1.multiply();
cout << "Enter 4th fraction: ";
cin >> n >> d;
g1.add();
cout << "Enter 5th fraction: " << endl << endl;
cin >> n >> d;
g1.subtract();
g1.print();
return 0;
}
Upvotes: 0
Views: 3853
Reputation: 3295
Fraction
class having the numerator and denominator and with operator overloads to perform basic arithmetic and IO. Fraction add(const Fraction& another) const
-> This will add a fraction passed to the member function as another
variable with itself and return a new fraction without mutating/changing itself.
void add(const Fraction& another)
-> This will add another
Fraction to itself (thus changing itself).
Here is an example :
class Fraction
{
int num, den ;
public:
Fraction(int n, int d) : num(n), den(d) {}
Fraction add(const Fraction&) const ;
Fraction sub(const Fraction&) const ;
Fraction mul(const Fraction&) const ;
Fraction div(const Fraction&) const ;
void show() const;
};
Fraction Fraction::add(const Fraction& another) const
{
return Fraction(num*another.den + den*another.num, den*another.den);
}
void Fraction::show() const
{
std::cout << num << "/" << den ;
}
... // define other methods likewise
The main function :
int main()
{
int n, d ;
std::cout << "Enter first fraction : ";
std::cin >> n >> d ;
Fraction a(n, d);
std::cout << "Enter second fraction : ";
std::cin >> n >> d ;
Fraction b(n, d);
Fraction sum = a.add(b);
std::cout << "Sum is : " ;
sum.show();
}
Note: You should also try to write a function to reduce the fractions i.e. eliminate common factors between numerator and denominator.
Upvotes: 1
Reputation: 35
Tried running and understanding your program , looks to me the class concept is not followed properly. You cannot access class variable directly inside main. So what can I understand from your code is you are calling rename to set various values, but your rename function is reassigning the values from the local uninitialized variables only. Here's what should you do:
Create a constructor for your class.
class gcd
{
public:
void finder();
void rename(int x, int y);
void add();
void subtract();
void multiply();
void divide();
void print();
gcd(int x, int y, int w, int z, int m, int p)
{
n = y;
d=x;
n1=w;
d1=z;
temp1=m; temp2=p;}
private:
int n, d, n1, d1, temp1, temp2;
};
Create object of gcd using above constructor. Also before dividing check for divide by zero.
Upvotes: 0