-6

I have this code in c++:

#include <iostream>

using namespace std;

int main() {
 float division = 5/2;
 cout<<division;
 return 0;
}

But it return 2, instead of 2.5 , why ?

J. Uchu
  • 121
  • 6
  • 2
    Because `5` and `2` are integers so it does integer division. – nwp Mar 04 '18 at 13:30
  • 1
    @everyonewhoansweredthisquestion Too FGITW. You all have the vote to close as duplicate privilges, use that instead. – user202729 Mar 04 '18 at 13:34
  • 1
    @user202729 But if I close all the trivial dupes instead of answering them, how can I hit my rep cap in half an hour? Does not compute. – nwp Mar 04 '18 at 13:37
  • @nwp Why is reputation important? ((I guess that you don't actually think so but you pretend to be one of the answerers)) – user202729 Mar 04 '18 at 13:38
  • 3
    @OP Are you learning C++ from a good book, or are you using other languages to figure out what C++ does? If it's the latter, don't learn C++ that way. – PaulMcKenzie Mar 04 '18 at 13:40
  • 2
    @user202729 Reputation equals trust of the community in you which comes with privileges. Also high SO rep might make you look competent to some people which might be reflected in your salary. The SO reward system clearly wants you to answer dupes, not close them. But that is something that is a [well known problem](https://meta.stackexchange.com/q/37466/266536) that seemingly will never get fixed. – nwp Mar 04 '18 at 13:45
  • @nwp Too bad. And thanks for the meta link. – user202729 Mar 04 '18 at 13:47
  • @nwp - I think the system has *some measure* of self-balancing. It's usually not beneficial to answer questions with *exact* duplicates, since high-rep users tend to employ their deletion privileges on those. – StoryTeller - Unslander Monica Mar 04 '18 at 13:49
  • @StoryTeller Only if they do delete them. Or... are duplicates auto-deleted? Let me check... – user202729 Mar 04 '18 at 13:51
  • [Here](https://meta.stackoverflow.com/questions/320522/how-are-duplicate-questions-deleted) is the relevant meta link for duplicate deletion. According to it, this question _will not_ get deleted automatically. – Arnav Borborah Mar 04 '18 at 13:53
  • @user202729 - I don't think the roomba script touches duplicates. The philosophy is that they are "post signs" that help find the already answered questions. But honestly, there are only so many ways to phrase this question to warrant it staying up. That's why high-rep users usually opt to delete manually instead. – StoryTeller - Unslander Monica Mar 04 '18 at 13:54

1 Answers1

4

Because you are dividing integers, not floats. What you assign the result to is irrelevant to how the division is performed.

If you want to divide floats, do

float division = 5.f/2.f;
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • @JesperJuhl Do both operands have to be floats or is just one enough, since it will make the whole operation a floating point division? – Arnav Borborah Mar 04 '18 at 13:47
  • 1
    @Arnav Borborah One is enough, the other will be promoted. See http://en.cppreference.com/w/cpp/language/implicit_conversion – Jesper Juhl Mar 04 '18 at 13:51