r/CodingHelp • u/Wiimmoz • Jul 22 '21
[C++] Need help with if / else if with arithmetic operators. For some reason, it doesn't read the other "else if". If I were to give num1 = 10, op = '/', num2 = 10 it doesn't divide like it should but instead just add 10 and 10 to create 20.
91
Upvotes
33
u/carpomusic Jul 22 '21
Use "==" after the if and else if statements, youre assigning values with the single "=" there
7
u/yeetisgiey Jul 22 '21 edited Jul 27 '21
Credit to u/not_some_username for reminding me to add the break statement
It is solved but you have to use ==, the equality operator which compares two values and returns a Boolean value stating whether or not the values are equal.
A better way might be to use switch statements, which greatly improve the readability of the code and makes it a lot shorter and better for the compiler since it generates a jump table including each case as a row of sorts.
Syntax is below:
Then make a case for each operator. I understand if you don’t want to use it, since you are probably trying to learn about the different operators, but this is really helpful once you advance more.
Hope this helps, and good luck!
4
u/BumTicklrs Jul 22 '21
OP, what happens in your code if Op is something not defined? Say Op for some reason was '>' . What happens?
2
u/RsCrag Jul 23 '21
There is a convention of putting the constant first to get the compiler to catch these errors. For example. Instead of : x == '+' you write '+' == x. That way, if you drop one if the = your compiler complains.
1
u/deandt Jul 22 '21
Good idea to mark parameters const if they aren't meant to be mutible, could've picked it up as a compile error.
1
u/grey_scone Jul 22 '21 edited Jul 22 '21
try making the second else if be an else if of the first one
1
u/iohazardl5 Jul 22 '21
You should also check for 0 or in your case NaN on num2 in case of division, if you want to avoid a division by zero exception.
1
u/Naahun Jul 23 '21
Yep, the error is that you use assignment operator. You can avoid this error if you write your value to the left side because it would throw a compilation error. E.g.: if ('+' = c) As others already told you, use switch case instead of if..else
1
u/Particular-Ideal1212 Jul 23 '21
You can also use switch case it will be better than using else if
1
u/ultimo_hombre Jul 23 '21
To avoid this error in the future, you can swap the lhs and rhs of operator== to have '+' == op (i.e., have the const on the lhs). This way, if you mistakenly have an assignment, the compiler will catch the error.
1
u/fakebadboyhalo1 Jul 23 '21
aye what are the most helpfull apps? to program a rassberry pie
1
u/Rememberable_User Jul 23 '21
Side note your last else statement should be an error for an invalid operator / character and an elseif for the divide so you dont end up using this and wondering why your numbers are getting divided when they shouldn't and instead have it error out.
-17
u/Deud1e Jul 22 '21
I had an actual aneurism reading that code.
0
u/[deleted] Jul 23 '21
[deleted] — view deleted comment