27

C++: Running total not incrementing with for loop

Python: How can I force division to be floating point? Division keeps rounding down to 0

C++ has totally different ways of solving the issue than Python.

Why the duplicate marking?

What purpose does it serve to block answering said question in C++ context?

18
  • 1
    I get the discrepancy in tags, but fundamentally the answer is the same; turn one operand into a float.
    – Makoto
    Jan 30, 2018 at 8:15
  • 8
    Yes, but C++ has different ways to turn operands into floats. So answering the same question in the context of C++ might yield better answers for problems in C++ context.
    – 0xbaadf00d
    Jan 30, 2018 at 8:17
  • 3
    My bad. Instead of trying, (and, admittedly, gettnig the language wrong even if the issue was right), to find a dupe that explained the issue, I should have voted-to-close the Q as 'Unclear' or 'Too broad'. It was unclear why the OP chose to use integer arithmetic for a series with term values <1, and too broad because OP displayed no attempts at debugging the issue. I am chastised enough now, and you can be sure that I wil never link any dupe that explains a problem ever again. Jan 30, 2018 at 9:33
  • 6
    @MartinJames it is neither unclear nor too broad. The reason why the poster used integer arithmetic is irrelevant to the answer to the question and you don't know how much effort was put in to debugging it. The OP is clearly a novice and might not know exactly how to debug the code effectively. In any case, the question is easily answered without needing to know the questioner expended superhuman efforts.
    – JeremyP
    Jan 30, 2018 at 10:43
  • 10
    @JeremyP printing out a couple values, or using a debuigger, is a superhuman effort? WOW! Where do we sign up for our costume, cape etc? Jan 30, 2018 at 10:51
  • 2
    @MartinJames You seem to be saying that one of the criteria for a question being valid is that the questioner demonstrate that they expended a certain amount of effort in debugging. I don't agree with you.
    – JeremyP
    Jan 30, 2018 at 10:58
  • 4
    @JeremyP we may have to agree to disagree:) Jan 30, 2018 at 11:15
  • 2
    Should you only accept questions where the one asking has demonstrated a certain level of expertise? I don't think so. We are all learning here, the position on the curve doesn't matter, only the speed.
    – 0xbaadf00d
    Jan 30, 2018 at 11:23
  • 9
    @0xbaadf00d SO is a site for professional and enthusiast programmers. The position on the curve does matter, else the skilled and experienced developers on SO will be overwhelmed with tuition requests and homework dumps. Jan 30, 2018 at 13:30
  • 1
    Oh - by 'else the skilled and experienced developers on SO will be overwhelmed with'.... I meant 'even more than they already are'. Jan 30, 2018 at 13:31
  • Maybe inexperienced programmers are overwhelmed with all those tough questions from experienced developers. :p
    – GolezTrol
    Jan 30, 2018 at 14:54
  • 1
    Anyway, it shouldn't be a blame game, but if a mistake is made, somebody should be allowed to make a remark about that without triggering a load of sarcasm and a needless rant. This question in itself is written well enough, and doesn't seem to fit the bill of 'yet another homework dump'.
    – GolezTrol
    Jan 30, 2018 at 14:55
  • 3
    Would it make sense to have a canonical language agnostic question for this issue of integer division, since this question comes up frequently for many programming languages? We have such a thing for floating point math. Jan 30, 2018 at 15:17
  • 7
    @GolezTrol what tough questions? Questions like 'why did you not print out intermedaite results or use a debugger?'. Is that particularly tough? It's not a homework dump - it has an attempt. What it does not have is any apparent attempt at debugging, a problem with a very large set of newbie qiestions. Why can they not just do a little work and either fix their problem, or give us better clues? Jan 30, 2018 at 15:20
  • 3
    @FredLarson entering 'C always 0 as result' into a popular search engine gives: 'About 3,200,000 results', with the top 3 results, inlcuding stackoverflow.com/q/9455271/758133 from SO, identifying the division problem.. It's not even necessary to use the phrase 'integer division' to find the OP's problem. Jan 30, 2018 at 15:29

3 Answers 3

17

Yeah, this question is probably a better duplicate target. Still, the same concepts apply in Python as in C++: make sure one of the operands becomes a float or double; either by casting or by multiplying with 1.0.

Also, note the comment by the first close voter:

@0xbaadf00d TBH, my dupe was rubbish anyway - it's Python. I searched for 'C++ integer division' and got a Python answer as the top result. I should have used Google. OP - sorry, my bad:(

Another victim of Stack Overflow's less-than-optimal search function.

14
  • Almost, that question doesn't cover "magic" numbers. But it's still way better.
    – 0xbaadf00d
    Jan 30, 2018 at 8:20
  • Can you not remove the duplicate vote? He didn't, he had the time to do it too.
    – 0xbaadf00d
    Jan 30, 2018 at 8:21
  • 1
    I can only cast one reopen vote, and 5 are needed. It's better to let a moderator or C++ gold tag badge holder do that.
    – Glorfindel
    Jan 30, 2018 at 8:22
  • Sorry, I was talking about the one that made the comment you posted. He commented that his duplicate vote was rubbish, but didn't remove it. As you can see, he's credited for the duplicate vote.
    – 0xbaadf00d
    Jan 30, 2018 at 8:31
  • 1
    I've reopened it. I'll leave duplicate votes to the community though
    – Flexo Mod
    Jan 30, 2018 at 8:51
  • 2
    Did I get any rep for it? No. Was the broad stroke of the issue the same as the Python answer? Yes. Did the OP get the reason for its problem? Yes. Did I spend any mere time on this Q&A, having screwed up once and apologised for my bad judgement? No. I don't want 'credit'. I don't want 'votes'. I am tired for being flayed on meta for irrelevancies like this:( Jan 30, 2018 at 9:13
  • @0xbaadf00d even if Martin would have retracted his vote, the question would still be in the review queue.
    – Glorfindel
    Jan 30, 2018 at 9:16
  • OK, so I waste more time going back to fix someth9ing that does not need fixing and..... the dupe has gone. So, another waste of time. Jan 30, 2018 at 9:16
  • It is well known fact the C division result is always zero (even better dup as it has almost identical code) - you should pick another language that has some operations that work... Also most other are totally broken too - some can't even get "string replace" working - C# string replace does not work :) Jan 30, 2018 at 16:29
  • 1
    Speaking on SO's search, every time I want to mark a Null Pointer Exception question as a dupe, I want this Q and shows up just fine in the search, but not in the dupe dialog. Jan 30, 2018 at 18:35
  • 1
    @Draco18s that would appear to be because the result in the search is an answer, while the dupe dialog only shows questions.
    – CalvT
    Feb 1, 2018 at 15:45
  • @CalvT븃 Honestly, I don't care. I type in the exact title of the question and it doesn't show in the dupe dialog. It's got 211 up-votes on the Q and 2751 on the answer. That should be more than enough to make the system go "this one. You want this one." All I should have to search for is "null pointer" or "null reference" and get it instantly. Feb 1, 2018 at 18:13
  • @Draco18s sure, my thoughts as well. I was just pointing that the search functionality is probably the same, except the dupe search has a filter that is set to only show questions.
    – CalvT
    Feb 1, 2018 at 18:18
  • @CalvT븃 Looks like I have to use "NullPointerException" in full to get the Q. Annoying. :\ Feb 1, 2018 at 18:21
16

No, this question should definitely not be a duplicate of the Python question. Although the concept is the same (cast one operand to float), none of the answers there is directly applicable to C++.

In my opinion, it can only be a duplicate if all answers in the target (or at least most of them) are valid for both questions.

1
  • Given substantially the same question in different language context, I would accept any of the answers to the dupe target being relevant and applicable to the duplicate question. Jan 30, 2018 at 23:03
-1

I think it obvious that two different languages call for two different answers. The 'concept' can be sited, however.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .