views:

49

answers:

2

Hello.

I'm a student in an introductory C++ course. For one of our past assignments we have had to create a simple program to add fractions. Each new lab is just an application of new skills learned to making the same program. Now I need to make one using objects from a class definition.

After tooling with a multiplying example my professor gave us, I finally got the code to add fractions properly.

#include <iostream>
using namespace std;

class Fraction
{
private:
  float numer;
  float denom;

public:
  void Init(float n, float d);
  void Multi(Fraction *, Fraction *);
  void Print();
  Fraction() 
  {
     numer = 0.0;
     denom = 0.0;
  }
  void setNumer( float n ) { numer = n; }
  void setDenom( float d ) { denom = d; }
  float getNumer() { return numer; }
  float getDenom() { return denom; }
};

main()
{
  Fraction x;
  Fraction y;
  Fraction f;

  x.Init( 1.0, 4.0 );
  y.Init( 3.0, 4.0 );
  f.Init( 0.0, 0.0 );

  f.Multi( &x, &y );

  x.Print();
  y.Print();
  f.Print();
}

void Fraction::Init(float n, float d)
{
  numer = n;
  denom = d;
}
void Fraction::Multi(Fraction *x, Fraction *y)
{
  numer = ( x->numer*y->denom) + (x->denom*y->numer);
  denom = ( x->denom*y->denom);
}
void Fraction::Print()
{
  cout << "Fraction:" << endl;
  cout << "  Numerator: " << numer << endl;
  cout << "  Denominator: " << denom << endl;
}

Stackoverflow cut off my code. :/ (Not too sure why. I'm kinda new to the site)

Anyways, what I'd really like to do is set this program up so it could take user input for what the x and y fractions would be. In my past assignments I've just used cin and cout commands, but now have no idea what to do. Once I get that I know I can make it reduce the fractions properly and display properly, but I have no idea how make it take input.

Does anyone have any suggestions? (Or even better if you can direct me to a site that has more information like cplusplus.com I would be very grateful!)

Thanks you in advance.

+4  A: 

Renaming your Multi method Add would avoid a lot of potential confusion and is highly recommended.

As for input, what's wrong with (e.g.) std::cin >> numer >> denom (with numer and denom declared as integers) for example? Then of course you can pass them to the Init method, etc. (You'll probably also want to do prompting on std::cout before reading the user input, naturally).

Alex Martelli
First off, I'd like to thank you for the incredibly quick response. During the couple minutes after posting and then checking back, I tried the cin and cout commands and they worked fine. XDI was getting lots of trouble earlier.I also changed the Multi to Add.Thank you so much.
AdamY
@AdamY, you're welcome!
Alex Martelli
+1  A: 

You have a few options, read from cin and init:

float n, d;
cout << "Enter numerator: " << endl;
cin >> n;
cout << "Enter denominator: " << endl;
cin >> d;

x.Init(n, d);

Another, trickier option is to allow direct access to the numerator and denominator members (this won't work with your Init function):

class Fraction {
 public:
  // other stuff ... 
  float &numerator() { return numer; }
  float &denominator() { return denom; }
  /// other stuff ...
}

cout << "Enter numerator: " << endl;
cin >> x.numerator();
cout << "Enter denominator: " << endl;
cin >> x.denominator();
Stephen