Why is it rounding numbers?

This is my code:

1
2
3
4
5
6
7
8
9
10
11
#include "stdafx.h"
#include <iostream>

using namespace std;

int main ()
{
	cout << 5 / 2;
	cin.get();
	return 0;
}


I'm using Visual C++ 2010 Express and the problem is it keeps rounding numbers. I get in this case a result 2! Is it the compiler or maybe something else?
closed account (z05DSL3A)
It is doing exactly what it should be doing, integer division.
airman9519 wrote:
I get in this case a result 2!

That's how integer division works ( http://www.cplusplus.com/forum/beginner/23124/ ).

Try 5/2.0 or 5.0/2. This will force the compiler make a double division and you'll get what you want.
Thanks, I'm not that good at C++.
Topic archived. No new replies allowed.