``````#include <iostream>

using namespace std;

class Fraction {
public:
Fraction() : num(0), denom(1) {}
Fraction(int num, int denom) { setNum(num); setDenom(denom); }

int getNum() const { return num; }
int getDenom() const { return denom; }

void setNum(int num) { this->num = num; }
void setDenom(int denom) { if (denom) this->denom = denom; }
void checkGuess()
{
(if num ? denom = print())
else
cout<<"Wrong, Try again";
}
void reduce()
{
int g = gcd(num, denom);
num /= g;
denom /= g;
}

void print()
{
reduce();
cout << num << "/" << denom << "\n";
}

private:
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int num, denom;
};

int main()
{
Fraction f1(9, 11), f2(10, 3);
Fraction f3(f1.getNum() * f2.getNum(), f1.getDenom() * f2.getDenom());
f1.print(); cout<<"*"; f2.print
cin>>num.checkGuess();
cin>>denom.checkGuess();
f3.print();
}
``````

......that is what i have so far for my fraction program which i added the check guess in after i ran the other parts of the program. so i know that it doesnt run as is but minus the checkguess it run fine. I need to figure out how to print a different problem with random fraction generated when they get the previous problem correct. so for instance 1/3* 2/2 = 2/6 this is wrong the right answer is 1/3. after they type in the right answer a new problem shoudl appear for them to answer. any help please. thank you guys.

Edited by Reverend Jim: Fixed formatting

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by StuXYZ

I would make the checkGuess() take in 2 integers that the user inputs with cin ( int num, denom ) and then compare it to the reduced fraction in f3.

``````int num, denom;
cout << "num: ";
cin >> num;
cout << "denom: ";
cin >> denom;
f3.checkGuess(num, denom);``````

The checkGuess() checks by just going

``````void checkGuess(int xnum, int xdenom)
{
if( xnum == num && xdenom == denom )
{
cout << "Correct!\n";
print();
}
else
cout << "Wrong, try again!\n;
}``````

Is this what you are trying for?

Second. this program is screaming for a overloaded operator* so you can write `Fraction f3=f1*f2;` .
The major point about classes is so that you can use them as abstract items.

Small points:

The checkGuess is both wrong algorithmically and syntatically.

You need something like this:

``````void checkGuess(const Fraction& G)
{
if (G.num==this.num &&
G.denom=this.denom)
std::cout<<"success"<<std::endl;
}``````

But you need to reduce the guess, have an operator>> and operator<<
to allow you to do this

``````Fraction X(3,4)
std::cout<<X<<std::end;``````

Avoid using the ? : operator since you are just getting confused. Try an `if(condition) { } else { }` first, then later you can add them when it is working.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.