Need help with a function

Hi,

I'm new to this forum as you can tell and I am also relatively new to coding. This semester I am taking a c++ course without any previous knowledge of code. Anyway I would like your insight on this assignment I am working on.

So far I have gathered the following things that I will need:

I need to declare a map with both string and int. String to hold the names and int to hold their points.
I need a for loop (I think) to keep a running total of the scores.
I need a map iterator.

My question is how do I parse through the text and tie everything together. I have posted the assignment below. Just a note before anyone gets upset: I want tips on how to go about doing this. By tips I mean like "Hey you need to declare a variable to store blah blah blah" or "Instead of a for loop you're going to need a while loop because blah blah blah"

THANK YOU SO MUCH FOR READING!


For this assignment, you will write a program to analyze some formatted data in a text file. Scores for 200 games (one game per line) in an accountants' kickball league are provided here: scores.txt.

The text is like this:

Rhinoceroses 119 Baboons 52
Caimans 51 Dodos 24
Eels 118 Pigeons 88
Ocelots 85 Meerkats 75
Caimans 37 Baboons 83
Ocelots 68 Eels 29

(They are in a standardized format with the score for one match on each line, with the score for each team after its name.) Your job is to write a program which parses this file, keeping a running count of how many points are scored by each team (PF) and against each team (PA), printing those values out on the console in this format:

Baboons PF: 3360 PA: 3767
Assignment wrote:
how many points are scored by each team (PF) and against each team (PA)
Could you clarify what this means? The word "against" is particularly ambiguous in this case.

As for your map, you will want to map each team name to two integer values - I suggest using either a struct or std::pair.

As you suggested passively, it would be better to use a while loop than a for loop:
1
2
3
4
5
6
7
std::string ta, tb;
int sa, sb;
while(input_file_stream >> ta >> sa >> tb >> sb)
{
    //...process data...
    //...update maps...
}
Hi, thanks for your comment. So by "against" it is referring to the total number of points the team has had scored against them.

Edit:

"As for your map, you will want to map each team name to two integer values - I suggest using either a struct or std::pair." How would I do this?

I only know how to go as far as:

map<string, int>
Last edited on
You could use two maps, then.

1
2
map<string, int> pointsForMap;
map<string, int> pointsAgainstMap;

while (read team1Name, team1Points, team2Name, team2Points)
    pointsForMap[team1Name]+= team1Points
    pointsForMap[team2Name]+= team2Points

    pointsAgainstMap[team1Name]+= team2Points
    pointsAgainstMap[team2Name]+= team1Points



Or, per LB's suggestion:

1
2
3
4
5
6
7
struct pointsHolder
{
    int pointsFor;
    int pointsAgainst;
}

map<string, pointsHolder> pointsMap;


while (read team1Name, team1Points, team2Name, team2Points)
    pointsMap[team1Name].pointsFor += team1Points
    pointsMap[team2Name].pointsFor += team2Points

    pointsMap[team1Name].pointsAgainst += team2Points
    pointsMap[team2Name].pointsAgainst += team1Points
Last edited on
Topic archived. No new replies allowed.