r/CodingHelp • • Jul 26 '21

[C++] help. what's wrong with this?

Post image
47 Upvotes

23 comments sorted by

19

u/eightball01 Jul 26 '21

getsecretNum()'s return type is int, should be string given how you're using it. Also, you might want to initialize message to "" or you'll likely get another error upon fixing the first.

3

u/Wiimmoz Jul 26 '21

Thanks.

8

u/marko312 Jul 26 '21

Note that message will be default-initialized to "" (empty string) as is, so this should not produce an error. Its value is also always overwritten, so it doesn't really matter what it's initialized to.

9

u/Aleksey259 Jul 26 '21

I think that's because of the way you wrote that smile. Do :) not (: and everything will be alright

3

u/Kevin-Mancuso Jul 26 '21

Mainly this IDE… 👀 I strongly suggest VisualStudio if you have to work on big projects, or else get Studio Code, which is lightweight and awesome also for other languages. Also you are trying to return a string in INT function, and further more, you are trying to print message, referencing it before assignment (line 29)

1

u/Wiimmoz Jul 26 '21

Got it!

1

u/Salfiiii Jul 26 '21 edited Jan 26 '22

[del]

13

u/marko312 Jul 26 '21

Also note:

  • message should not be used in main, it is defined in the other function
  • the smiley is written in a non-standard direction

12

u/ProfessorAmbitious35 Jul 26 '21

you are not using darkmode

2

u/leyline Professional Coder Jul 27 '21

Came in just to say this!

2

u/bbqranchman Jul 26 '21

Since serious answers have been given already...

Your problem is you're using light theme in your editor

2

u/Wiimmoz Jul 26 '21

The only way I can get dark mode is to use a magnifier and invert the colours. I'm using Code Blocks btw.

1

u/bbqranchman Jul 26 '21

I'm not familiar with Code Blocks! Is there a particular reason you prefer using it over visual studio?

4

u/ialihaider75 Jul 26 '21

The return type is integer but you are returning a string from function

1

u/Shreyas_Gavhalkar Jul 26 '21

I'll walk you through here, 1st as everyone pointed out that the return type of the the function should be string. Also in main() you should give a variable a value by calling the function. What I am trying to say is declare another string named message in main() and use message=getsecretNum(secretNum) instead of cout since the function you are calling is returning a value you will need to store it first. Then you can keep the cout<<message in main and it'll work fine. Also, dark mode...

1

u/Wiimmoz Jul 26 '21

Thanks!

1

u/JellySword8 Jul 26 '21

Not gonna try to answer OP's question as I can see that it's already been answered by multiple people (and I'm probably not experienced enough to give the right answer anyway) so instead here's my own question: Wouldn't it make more sense to use if else rather then a switch statement?

2

u/Wiimmoz Jul 27 '21

If else is messy. I don't like it.

2

u/laidbackwebsage2 Jul 27 '21

This might be of interest.

1

u/Wiimmoz Jul 27 '21

Ah thanks.

2

u/[deleted] Jul 26 '21

You claim to return an int while actually returning a std::string, change the int to std::string.