C++ map menu class, return int to main switch-statment. need help

class Menu {
private:
	const map<string, int> menuChoice = { { "analize",1 },{ "load" ,2 },{ "save",3 },{ "exit", 9 } };
public:
	int inputValidation(string usrChoice)
	{
		int temp;

		auto validateUsrChoice = menuChoice.find(usrChoice);
		if (validateUsrChoice != menuChoice.end()) {
			temp = menuChoice->second;
		}	

		return temp;
	}
};
int main()
{
	bool runPrg = true;

	do {
		string usrInput;
		Menu menu;

		cout << "\tMenu: Analize file : 1 \n\t Load : 2  blblabla ...\n\t" << endl;
		
		cout << "\t";
		getline(std::cin, usrInput);
		int c = menu.inputValidation(usrInput);

		switch (c)

Hello again guys, edit my post!
brb with error msg’s. But the problem is this part: " temp = menuChoice->second; " , i cant get it to work. The thing is that i dont really know how .find works… is it so that the compiler adds iterators and doing most of the work/coding behid the sceen?
Ive tried that and also " temp = menuChoice.second; " , didnt work either!

stick your code in three grave marks on both sides to improve formatting.

ex.

```
code
```
2 Likes

And the problem is…?

You don’t mention what the problem is, but two things that might be a problem are:

  1. the temp variable in inputValidation is not initialized, so if the item isn’t found in the map it might contain anything.
  2. you use std::cout, std::endl, etc… but you also write just map, and not std::map. Unless there’s a using declaration hiding somewhere, it might not compile.
1 Like

Hello, never mind, I added “using namespace std;” , in a atemp to clear up the code a bit for my newbie eyes!

The problem is that i dont really know how .find works. How to store the “pointer value - to deref” or how i then store the second elements to something" if it even works like that?
Also i dont really understand: " -> " , is that like a function pointer?
And last, “.second” dosent work either, and i think this is the thing. The way i did it, if .find finds the string in this case, it returns a iterator? but how do i use that?
Here is the different errrors:

cod

Here is an example that looks like my code(or almost identical):

#include <iostream>
#include <map>
 
int main()
{  
    std::map<int,char> example = {{1,'a'},{2,'b'}};
 
    auto search = example.find(2);
    if(search != example.end()) {
        std::cout << "Found " << search->first << " " << search->second << '\n';
    }
    else {
        std::cout << "Not found\n";
    }
}

From: http://en.cppreference.com/w/cpp/container/map/find

So i belive that maybe this dosent work “->” when im trying to use it in the same class?

'Best regrads!

Comment: This is how it looks now, when i did it my self?! :S
And maybe thats good, to be honest, because its the first time ive used a iterator hehe
I gues this is what the compiler does for me when i use the find-member function?

for (map<string, int>::iterator it = menuChoiche.begin(); it != menuChoiche.end(); ++it)
{
if (usrChoiche == it->first)
temp = it->second;
}

LOL i found the bugg in my code:
None working solution:

auto validateUsrChoiche = menuChoiche.find(usrChoiche);
if (validateUsrChoiche != menuChoiche.end()) {
temp = menuChoiche->second;
}	

Working solution:

temp = validateUsrChoiche->second;

a -> b is the same as (*a).b. Both behave the same, but the first one is much more readable, especially when going deeper: (a->b->c)

find returns an iterator. The iterator’s contents can be accessed as if it was a pointer: *iter. Don’t forget to make sure the value was actually found, by checking that iter != yourMap.end()

1 Like

Aight! So in this context its a way to deref whats the pointer(aka iterator) is pointing at(map object) first/second.
So .second and ->second is the same way…

But in another context it could be(i think ive asked it earlier in this thread) a function pointer? A pointer to another class member-function, right?

I think you are mixing up function pointers and functions of pointers to instances. If A is a class with a function b and a is an instance of class A then a->b will invoke b.

class A {
    void b() { ... }
}


void main() {
    A *a = new A();

    // invokes b
    a->b();

    // same thing
    (*a).b();
}

Neither a nor b are function pointers though.

1 Like

Perfect! Thanks alot, think i got it now! Never used this before and this was so unclear to me…
Thanks again!