PDA

View Full Version : C++ help



smc18
March 14th, 2008, 02:27 AM
Hi, I am stuck with some math, I'm not sure if I just need to format the output or if there is a problem with something else.

Here is an excerpt of the code,


sum = 3/15;

cout << sum << endl;

will just return a value of 0.

or


sum = 3/15 + 2;

cout << sum << endl;

will return a value of 2


However the code,


sum = 10 + .5;

cout << sum << endl;

will return a value of 10.5

Why is 3/15 returning a value of 0?

I'm programming this on a Windows machine, using Dev-C++, I have included
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
and declared sum as a float data type, not an int. Any help is appreciated, thanks. If you need more information please just ask.

slavik
March 14th, 2008, 02:34 AM
when you do 3/15, what really happens is integer division (the one with a remainder).

what you want to do is 3.0/15, this will force real division and you will get the expected 0.2.

3/15 is 0 with 3 as remainder

smc18
March 14th, 2008, 02:45 AM
Haha it worked, that simple eh?, Thanks alot.