#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())
            cout<<"Thats is the correct answer";
        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
  cout<<"Please enter your guess"
  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.

Recommended Answers

All 2 Replies

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?

First off please read the formum posting rules about using code tags.
e.g. http://www.daniweb.com/forums/thread78223.html

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:
Add std::endl on each of your cout outputs.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.