Hello guys, I need to read some information from a text file. The program I'm working on is like a simple betting program.
What I need to read are;
match_code - team1 - team2 - odd1 - odd0 - odd2
139 Atletico Madrid - Real Madrid 2.85 3.40 2.35
But the spaces between datas are not known. We only know that both team names may contain more than one word and there is one space, exactly one dash and one more space (" – ") between team names.
Also match_code is an int and odds are double values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
while(getline(input, line))
{
istringstream ss(line);
ss >> match_code >> team1;
string temp;
while(ss >> temp)
{
if(temp != "-")
team1 += " " + temp; // to get the second word of team name
else
{
ss >> team2;
.
.
.
}
}
}
In that missing part, I need to get the second word of team name if there is one; and the three odds that are odd1 odd0 and odd2. Any help would greatly be appreciated. Thanks in advance!
I think that you can safely assume that the format of the spacing in a team's name is not significant. Hence, use a vector to collect all your data into individual strings.
Once done:
match_code is the first item in the vector.
The odds are the last three items in the vectors.
Eliminate those four items and your vector contains the two team names. Find the "-" string in the vector, and reassemble the names by placing spaces between them.
Your current method is OK also. The trick is to determine when the second team's name ends. If you can safely assume that no team has any word beginning with a digit in its name, then you can know when to break out of the loop and read the odds.