Jump to content

[C++] Trouble with if statement

Go to solution Solved by Ganz,

Needs to be: if (dorm == "d")

 

The = operator sets dorm to "d"; the == returns a boolean which is what you need for an if statement.  In almost any coding language comparing to values is == and setting a value is just =.

Hi,

I'm learning C++ and have made a simple program to make it so a user can input if they want to use division or multiplication, and they can enter their number and get a result. For some reason I have errors with the if statement if I try and build it.

 

pkTgj.jpg

Link to comment
https://linustechtips.com/topic/414411-c-trouble-with-if-statement/
Share on other sites

Link to post
Share on other sites

Needs to be: if (dorm == "d")

 

The = operator sets dorm to "d"; the == returns a boolean which is what you need for an if statement.  In almost any coding language comparing to values is == and setting a value is just =.

Link to post
Share on other sites

Needs to be: if (dorm == "d")

 

The = operator sets dorm to "d"; the == returns a boolean which is what you need for an if statement.  In almost any coding language comparing to values is == and setting a value is just =.

Thank you :) I always forget to do == :S

Link to post
Share on other sites

In Javascript you even need 3 of '='. (most of the time)

if(a===b)

D:

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

2 of them works fine iirc, it's just how accurate you want it to be.

That;s why I said "most of the time".

 

3 '=' also checks to see if they are the same data type.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

In Javascript you even need 3 of '='. (most of the time)

if(a===b)

D:

No you don't because correct me if I'm wrong but the extra = sign just allows you to make sure they're also the same datatype. As in, both are int's and both === 2 or both are strings and both === 2.

i want to die

Link to post
Share on other sites

No you don't because correct me if I'm wrong but the extra = sign just allows you to make sure they're also the same datatype. As in, both are int's and both === 2 or both are strings and both === 2.

Exactly.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×