To the best of my knowledge, does the code work?
It is very hard to tell, almost impossible actually, if the OP is aware of the flaw in the code.
As I don't know the skill of the OP, here's what I think happened:
- The OP wrote the initial program.
- The OP noticed that one of the lines in the output was a bit off and therefore added the if-else code to adjust the output.
- The OP noticed that the program was outputting the correct result, but felt that the if-else approach was not a good one and therefore asked a question on a StackExchange site.
Summary: I don't think the OP was aware of the problem. Based on these things, I have to estimate the skill of the OP.
What the OP needs
The OP needs to understand his own code. To determine whether or not the question is on-topic, let's take a look at our magical six questions:
- Does my question contain code? (Please include the code in the question, not a link to it) Yes
- Did I write that code? Yes
- Is it actual code from a project rather than pseudo-code or example code? Yes
- Do I want the code to be good code, (i.e. not code-golfing, obfuscation, or similar) Yes
- To the best of my knowledge, does the code work? The big question mark, but based on what I wrote above: Yes. It works, even if it's by mistake, it works.
- Do I want feedback about any or all facets of the code? Although not explicitly written, probably yes
The big "problem" here is that the OP is lacking a lot of knowledge.
Why your answer is a good answer:
You explain what is going on and how to do it correctly.
What your answer didn't mention that I've seen in other C++ reviews:
using namespace std;
Mentioning these things could possibly have made your answer a better answer from a Code Review point of view. However, based on the OP's knowledge, there are more important things to learn first.
Here or Stack Overflow?
- Would the question be on-topic on Stack Overflow? Probably. However, a lot of users there would probably suggest that the OP post it to Code Review so it could possibly have ended up here anyway.
- Is it off-topic on Code Review? No.
Another Stack Exchange site alternative would be Code Understanding (Area 51 proposal). Even though this question isn't about understanding the code, understanding the code is what the OP needed.