views:

121

answers:

2

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?

A: 

I see nothing in your included code that would cause what you're getting. There's of course obviously more going on that might explain it, especially since your if should seemingly only apply to one player.

Noah Roberts
My codes are rather long, so I'm not sure if I can paste them all here. Or perhaps I could upload my script so any helpful souls can see what's wrong with it? Just wanted to ask if anyone encountered a similar problem and any solutions.
Wallace
Here are my codes zipped up with the test data and expected results.http://www.mediafire.com/?kzyuizlgi4yI'm already more than halfway through my assignment and I'm stuck at this part which I can't get it right.If uploading of files are not appropriate, let me know and I'll take it down.
Wallace
You don't need to provide all the code, just the important bits. Definitely provide your map definition, and the code that generates your `a` iterator. Skip the map population for now.
Dennis Zickefoose
+2  A: 

midfielderBonus+=(a->second*2000);

So that += means it will accumulate the data. i assume you are forgetting a midfielderBonus=0 at some point when you move on to the next player. Kind of hard to tell as the code snippet you included doesn't show the initialization of the bonus variables.

Lorenz03Tx
@Lorenz03Tx: I will solved the problem. The problem lies with the +=, as what many earlier others have said. I assigned this particular bonus to another variable and compute the overall bonus by adding every other bonus together.Thanks for the help. You guys are great :)
Wallace
If the problem is solved, you might want to close the question. Or you may continue to get answers.
Lorenz03Tx
Sorry, but how do I close this question? I just started using this community.
Wallace
There is a check box outline next to each answer. You can click on it to accept the answer. Or if you solved the question yourself, you can post your solution as an Answer, and accept it.
Lorenz03Tx