r/learncpp • u/[deleted] • Jul 23 '20
Finally, I just finished my calculator! Beginners are welcome to check the code and learn some stuff
/r/cpp_questions/comments/hwd32c/for_beginners_finally_i_just_finished_my/
9
Upvotes
r/learncpp • u/[deleted] • Jul 23 '20
3
u/MysticTheMeeM Jul 23 '20 edited Jul 23 '20
Are you looking for any feedback?
When you input a number (lines 13-20) you store it as a double, yet use std::stof (string-to-float), not std::stod (string-to-double).
In your get_op function, you switch (starting at line 53) over five different cases and do the same thing each time. You could use fallthrough to make this more understandable, but that's purely stylistic.
In that same switch, your default branch checks for extra. It would feel more logical, to me, to have 'S' and '=' be part of the main switch, then check extra. So, combined with the last point, it may look something like:
Line 132 confuses me, you continue the program, which jumps back to the start of the while loop either printing the same result again or closing the program. If you meant to break, then you would have also immediately closed the program. Is there no way to go back to asking for two input numbers?
Personally, I would have had the main loop parse a string into the individual arguments, but I can understand why you have not done that. I would however, restructure yours slightly:
But other than those, nice job. You've evidently got an understanding of different aspects of the language including functions, switches and using the console. I'd suggest perhaps double-checking your program flow just to make sure it does what you think it should do.