Hi. I'm working on my C++ assignment about soccer and I encountered a problem with map.
My problem that I encountered is that when I stored 2 or more "midfielders" as the key, even the cout data shows different, but when I do a multiplication on the 2nd ->second value, it "adds up" the first ->second value and multiply with it.
E.g.
John midfielder 1
Steven midfielder 3
I have a program that already reads in the playerPosition. So the map goes like this:
John 1 (Key, Value)
Steven 3 (Key, Value)
if(playerName == a->first && playerPosition == "midfielder")
{
cout << a->second*2000 << endl; //number of goals * $2000
}
So by right, the program should output:
2000
6000
But instead, I'm getting
2000
8000
So, I'm assuming it adds the 1 to 3 (resulting in 4) and multiplying with 2000, which is totally wrong...
I tried cout a->first and a->second in the program and I get:
John 1
Steven 3
But after the multiplication, it's totally different. Any ideas?
Thanks.
Edit: Ok, I try. I'm actually calculating the bonus for each position field. I have already inserted the field data into the map and here is the actual codes.
multiset<string, less<string> >::iterator q, p = myset.begin();
q = myset.begin()++;
while (p != myset.end())
{
if(*p == *q)
{
currentScore = (int) myset.count(*p);
mymap.insert(pair<string, int>(*p, currentScore));
}
else if(*p != *q && topScore == 0)
{
topScore = (int) myset.count(*q);
topScorer = *q;
mymap.insert(pair<string, int>(*q, topScore));
}
else if(*p != *q)
{
currentScore = (int) myset.count(*p);
mymap.insert(pair<string, int>(*p, currentScore));
if(currentScore > topScore)
{
topScore = currentScore;
topScorer = *p;
mymap.insert(pair<string, int>(*p, topScore));
}
}
p++;
}
map<string, int>::iterator a = mymap.begin();
while(a != mymap.end())
{
if(playerName == a->first && playerPosition == "goalkeeper")
{
goalkeepers++;
goalkeeperBonus+=(a->second*5000);
sumBonus+=goalkeeperBonus;
}
else if(playerName == a->first && playerPosition == "midfielder")
{
midfielders++;
midfielderBonus+=(a->second*2000);
sumBonus+=midfielderBonus;
}
a++;
}
The test data is:
Score: 3-1
Ben
Steven
Ben
Score: 2-0
John
Steven
Score: 1-0
Ben
Score: 0-0
Score: 1-1
Cole
Score: 1-2
Ben
Score: 3-0
Cole
Steven
Ben
I tried to cout during the while loop and I got the output as:
Ben 5
Cole 2
John 1
Steven 3
This should be the correct output with Steven having 3 goals. But I'm getting 4, added with John's. Is there any way to assign the bonus to the a->first which is the player's name?