Value returning 5.0000, not include decimal

Hello, my take two values of type int and divide by three other values of type int. This value is stored in the variable "answer". However, when i do this the answer it gives back is 5.0000, but the actual value should be 5.2204. Any suggestions?

// Program Numbers sends numbers to the output stream in
// specified formats.

#include <iostream>
#include <iomanip>
using namespace std;

const int NUM1 = 1066;
const int NUM2 = 1492;
const int NUM3 = 512;
const int NUM4 = 1;
const int NUM5 = -23;

int main ()
{

float answer;

cout << fixed << showpoint;
cout << setw(10) << NUM1 << endl;
cout << setw(10) << NUM2 << endl;
cout << setw(10) << NUM3 << endl;
cout << setw(10) << NUM4 << endl;
cout << setw(10) << NUM5 << endl;

answer = (NUM1 + NUM2) / (NUM3 + NUM4 + NUM5);

cout << setw(10) << setprecision(4) << answer << endl;

system("pause");
return 0;
}
answer = (NUM1 + NUM2) / (NUM3 + NUM4 + NUM5);

I believe the above line C++ is doing the integer division instead of floating point division. Usually how I do is to put a . behind to "request" C++ to do floating point division.

E.g
answer = (1066. + 1492.) / (512. + 1. + -23.);

cout << fixed << setw(10) << setprecision(4) << answer << "\n";
Last edited on
Thanks. Your post made me think of something we did in class. The way we do this is:


answer = float(NUM1 + NUM2) / float(NUM3 + NUM4 + NUM5);
Topic archived. No new replies allowed.