#Referencing a specific value in a map

13 messages · Page 1 of 1 (latest)

tepid sparrow
#

My goal is to input a number as a string into a function and return its value as an integer. For example, if "three" is the input, then 3 would be the output. I am having troubles figuring out how to actually return the integer. Here is what I have so far.

void strToNum (string& what) {
    map<string, int> mp;
      mp["one"] = 1;
      mp["two"] = 2;
      mp["three"] = 3;
      mp["four"] = 4;
      mp["five"] = 5;
      mp["six"] = 6;
      mp["seven"] = 7;
      mp["eight"] = 8;
      mp["nine"] = 9;
      mp["zero"] = 0; 
    
    return mp[what];
}

VSCode is telling me "return value type does not match the function type". Adding a type (void, string) before "what" has not helped.

mint hazelBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

maiden heath
#

try to understand the error message return value type does not match the function type

#

what value are you returning and what is the return type of the function

tepid sparrow
#

Good lord, a freakin' integer. 😄

maiden heath
tepid sparrow
#

What is the format for that though? Where does int actually go? return int mp[what] and return mp[int what] both get me squawked at

maiden heath
#

Hint: right now the return type of your function is void

tepid sparrow
#

Thank you so much! That was grade A help

#

The entire function type needed to be declared as int

#

!solved

mint hazelBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity