Need help - map elements

I can't seem to find why my code doesn't compile. If someone can take a look at it and help me correct my mistakes, that would be much appreciated. The goal is to make it display the 5 elements of the map, then display all the elements that has the integer 100.

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
map <string, int> new_map;
map <string, int>::iterator iter;
iter = new_map.begin();

new_map.insert(pair<string,int>("Bob",100));
new_map.insert(pair<string,int>("Bill",80));
new_map.insert(pair<string,int>("Joe",100));
new_map.insert(pair<string,int>("Swagmaster",1));
new_map.insert(pair<string,int>("James",50));

cout << iter->first << '\t' << iter->second << '\n';


while(iter != new_map.end())
{
if (*iter == 100)
++iter;
else
cout << "Student did not score 100" << endl;

}
}


Error message:
lab9.cxx:28:29: error: no match for ‘operator!=’ in ‘iter.std::_Rb_tree_iterator<_Tp>::operator* [with _Tp = std::pair<const std::basic_string<char>, int>, std::_Rb_tree_iterator<_Tp>::reference = std::pair<const std::basic_string<char>, int>&]() != new_map.std::map<_Key, _Tp, _Compare, _Alloc>::end [with _Key = std::basic_string<char>, _Tp = int, _Compare = std::less<std::basic_string<char> >, _Alloc = std::allocator<std::pair<const std::basic_string<char>, int> >, std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, int> >]()’
Last edited on
It would help if you posted your complete error messages, exactly as they appear in your development environment.

You problem is at line 24. A map iterator is a pair. You're trying to compare the pair to 100 which makes no sense. The line should be:
1
2
 
if (iter->second == 100)


Also, you're not incrementing iter if the score was not 100.

PLEASE USE CODE TAGS (the <> formatting button) when posting code. It makes it easier to help you.

Last edited on
Alright thanks that solved my problem.
Topic archived. No new replies allowed.