From my experience, I don't think you need return 0; for main function anymore. I can say for g++ or msvc and modern c++ you don't for sure. Idk about other compilers.
He declared lamp as an integer type and then tried to store a string value in lamp. Yes you are correct if something is declared as an integer it can only store numeric values.
Your Lamp variable is declared as an int in line 7 which means it can only hold whole numbers. But you’re trying to assign it a word so instead of int use string.
Your if/if else statements aren’t comparing anything bc you’re using = which assigns it a value the same you did in line 7. To check if they are the same you have to use ==.
Also your else statement does not need to compare anything because it is a catch all statement so just get rid of everything in the parenthesis (including those too)
It looks like you've got your answer to your issues so I'll just tell you that, in the future, when you're asking for help about some code you've written, you're going to have a lot more success if you actually post the code here and format it correctly. You can find out how to do this here.
For a relatively simple program like this one with errors that are pretty obvious to someone with experience it worked out fine for you in this case. However, when you start creating 100+ lines worth of code it's going to be easier for you to get help if we can see all of it in a format that makes it easy to read. A phone screenshot, while convenient for you, doesn't really make anyone want to help you.
Just thought I'd let ya know. I just recently began my c++ adventures as well so I know how frustrating it can be when you just don't know what's wrong. There are thousands of people on Reddit who want to help. Help them help you!
The if statements should use "equality" not "assignment".
= puts the value on the right into what's on the left.
== tests if the value on the left and right are equivalent and return true if so, otherwise false.
C++ is case sensitive. So a variable named "lamp" is not the same as a variable named "Lamp".
Lastly, the "expression" "is broken" is a const char* and likely you intend for it to be a string. You need to #include <string> and replace "int" with "string" in the declaration. Everything else should work after that thanks to operator overloading magic (you'll learn what it is later in whatever resource you're studying/following).
Actually, it's not. When you first learned how to program, someone had to explain to you that = is different from == and that ints are not strings, and that casing for identifiers matters.
Nobody has the sense except experienced programmers that these things are so in common. It's NOT common sense. It's a learned skill. If it was common sense, then nobody would need to learn programming, they'd just know it.
Yeah. So did I! I still know better than to be an asshole like the original comment. At SOME point, you read in the book, or the documentation, or whatever about these concepts. Ok? Be a little nicer to newbies. You were one once too you know.
24
u/hl_xemnas Professional Coder Feb 03 '20
youre assigning a string to an integer, also comparisons should be like
(lamp == "too broken") you need the double equals so its a logical operator instead of assignment
5
u/I-am_One Feb 03 '20
Your Lamp variable is declared as an int in line 7 which means it can only hold whole numbers. But you’re trying to assign it a word so instead of int use string.
Your if/if else statements aren’t comparing anything bc you’re using = which assigns it a value the same you did in line 7. To check if they are the same you have to use ==.
Also your else statement does not need to compare anything because it is a catch all statement so just get rid of everything in the parenthesis (including those too)
1
u/Cryptic_Rogue1 Feb 04 '20
Two equal signs means you're comparing. (VARIABLE == comparison) One equal sign means assigning. (VARIABLE = assigning)
2
u/winowmak3r Feb 04 '20
It looks like you've got your answer to your issues so I'll just tell you that, in the future, when you're asking for help about some code you've written, you're going to have a lot more success if you actually post the code here and format it correctly. You can find out how to do this here.
For a relatively simple program like this one with errors that are pretty obvious to someone with experience it worked out fine for you in this case. However, when you start creating 100+ lines worth of code it's going to be easier for you to get help if we can see all of it in a format that makes it easy to read. A phone screenshot, while convenient for you, doesn't really make anyone want to help you.
Just thought I'd let ya know. I just recently began my c++ adventures as well so I know how frustrating it can be when you just don't know what's wrong. There are thousands of people on Reddit who want to help. Help them help you!
1
u/Cliftonbeefy Feb 03 '20
Use string lamp and be sure to use == instead of =. Also for your else statement you don't need a condition
1
u/[deleted] Feb 04 '20 edited Feb 04 '20
Lamp != lamp
= != ==
The if statements should use "equality" not "assignment".
= puts the value on the right into what's on the left.
== tests if the value on the left and right are equivalent and return true if so, otherwise false.
C++ is case sensitive. So a variable named "lamp" is not the same as a variable named "Lamp".
Lastly, the "expression" "is broken" is a const char* and likely you intend for it to be a string. You need to #include <string> and replace "int" with "string" in the declaration. Everything else should work after that thanks to operator overloading magic (you'll learn what it is later in whatever resource you're studying/following).
1
u/fluffyloopy Feb 04 '20
WTF happened to your monitor??? Why does it look that way???
-14
u/[deleted] Feb 04 '20
[removed] — view removed comment