Why do I not get the expected output?

Here is my code

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main(){
  float a;

  a=(2+(6/10));
  cout<<a;

  return 0;
}


For some reason, it only outputs 2, rather then 2.6 as it does if you do the problem on a calculator.
If I were to do 2+.6 I do get the expected output.

So why do I get 2, rather then 2.6?

Using Code::blocks with MSVC++ on Windows XP.
It's using integer division, not floating point division. Try (6.0/10.0).
Excellent, that worked! And I only had to add it to the 10.
Note that I couldn't add it to the 6 since that is an unknown value, and you can't do a.0 without an error.

Thanks for the help!

Also like to add in here for future readers, (sense I just found this out) that if you are trying to return a float value, you need to have you fuction be a float, not an int.
Last edited on
Topic archived. No new replies allowed.