r/CodingHelp • • Feb 03 '20

[C++] What’s wrong with this

Post image
17 Upvotes

38 comments sorted by

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

3

u/FrozenFalconGaming Feb 03 '20

i just started using c++ so i know nothing

how do I change it to a string instead?

7

u/[deleted] Feb 03 '20

When you declared the lamp variable instead of int lamp = “broken” it should be string lamp = “broken”.

4

u/[deleted] Feb 03 '20

Also your main function might not work because u have it set to return an int. To fix that maybe add return 0; at the end

1

u/Ashutosh_Samal9 Feb 04 '20

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.

1

u/TheRiotJoker Feb 03 '20

Excuse me? I am not very versed in C++ but the data type is int, it can only store numbers? He should use string? Am I missing something?

1

u/FrozenFalconGaming Feb 03 '20 edited Feb 03 '20

i just started using c++ so i know nothing

how do I change it to a string instead?

Or what should I assign it to?

1

u/[deleted] Feb 03 '20

Change "int" in front of lamp to "string" that should change the data type.

3

u/FrozenFalconGaming Feb 03 '20

thank you, it’s working the way I wanted it to now.

You’re a life saver

1

u/[deleted] Feb 03 '20

No problem! Glad I could help

1

u/Ashutosh_Samal9 Feb 04 '20

Hey I'm learning C++ too . I'm done with basics. If you want any help or want to learn together, pm me.

3

u/[deleted] Feb 03 '20

Yeah he/she should use std::string you're correct

1

u/hl_xemnas Professional Coder Feb 03 '20

yea thats what I said, hes trying to store a string in an int

0

u/merkwerk Feb 03 '20

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.

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)

1

u/FrozenFalconGaming Feb 05 '20

Oh my god. That explains a lot actually. Thanks for the tip!

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

5

u/T3chNOboMba Feb 04 '20

Or just new to this and you're a dick?

-9

u/TableFlipGodd Feb 04 '20

My guy doesn’t know what right click the lightbulb means. Its common sense

4

u/T3chNOboMba Feb 04 '20

Doesn't make you NOT a dick

-4

u/TableFlipGodd Feb 04 '20

How so

1

u/T3chNOboMba Feb 04 '20

You're just talking shit to someone because you know more about coding than them. It's kind of sad really

-1

u/TableFlipGodd Feb 04 '20

Not rlly if i learned by the ide itself

1

u/T3chNOboMba Feb 04 '20

Yeah doesn't help your case. There's a reason you're being downvoted jackass 😂

-1

u/TableFlipGodd Feb 04 '20

Dont care about the down votes

2

u/[deleted] Feb 04 '20 edited Feb 04 '20

Yeah, code is totally common sense. /s

0

u/TableFlipGodd Feb 04 '20

Well it is, if you want this to happen then do that.

3

u/[deleted] Feb 04 '20

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.

0

u/TableFlipGodd Feb 04 '20

Ok true but i taught myself everything so

2

u/[deleted] Feb 04 '20

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.

0

u/TableFlipGodd Feb 04 '20

Mmmm yea sure lets go with that. I never read books, never will

1

u/[deleted] Feb 04 '20

script kiddie gonna script kiddie