I've just started learning C++ and I need to finda way to add, subtract, multi, and divide fractions. Any help would be great!

Printable View

- 07-25-2002UnregisteredCan anyone help a newbie
I've just started learning C++ and I need to finda way to add, subtract, multi, and divide fractions. Any help would be great!

- 07-25-2002/\/\ E /\/ @
well...I'm not good myself but...you need to have different variables for the numbers and then the operator...for example, you can initialize two integer variables:

(Note: you must type #include <iostream.h> at the top of your cpp file)

int num1 = 0;

int num2 = 0;

next you decide on an operator...say multiplication.

cin>>num1; /*that's the code to get a value for the first

variable from the user*/

cin>>num2; /*that's the same thing for variable num2*/

//now to display all the information... cout<< is ConsoleOUT

//anytime something is in quotes " " they are literal

cout<<num1<<" * "<<num2<<" = "<<num1*num2<<endl;

/*in that last bit, we basically said...show num1, the * symbol, num2, the = symbol, and the answer to the multiplication question*/ - 07-25-2002master5001
Try this:

Code:`float x = 4.25;`

float y = 1.165;

cout << (2*x)+(y/3);

- 07-25-2002Unregistered
Thanks alot for the help i think i got it.

- 07-25-2002Ruski
You better get some tutorial on what are INT/FLOAT/DOUBLE .. then you'll be able to use them. Hint: Float and double are real numbers, but float is a little different. It's got a FLOATING POINT. :)

- 07-25-2002Unregistered
Now if you want to do this:

(1/2) + (1/3) = 5/6

in fraction form, not using decimals, like we all did in arithmetic, that's another kettle of fish that you can try when you get to classes and overloading operators, etc.