Hello,
I'm trying to solve an assignment that I have, but I don't want to get into the details of the assignment and confuse everyone. But, the basic idea is that I have to read a file, containing a list of team names and information about matches, and then I'd have to sort out the rankings of the teams based on the match details given.
My problem lies in the way the match details were written. Each match information is placed on a line by itself, and in this syntax
Team A!3|1!Team B
Team C!0|2!Team D
This means that in match 1, Team A scored 3 points against Team B, whereas Team B only scored 1.
I understand that I will be using arrays of struct, allowing me to store information about each team, along with points and everything else that has to go there.
I was unable however to read the match details. I mean, I understand that I have to use a getToken function that reads each token using strtok (I did that), but I was unable to return each token in order to store it in its correct variable in the struct. I was however able to print out each part of the token.
Here's my structure definition:
struct Team{
char teamName[30];
int gamesPlayed;
int goalScored;
int goalAgainst;
int wins;
int loss;
int points;
};
and here is the function that I used to print the tokens. My problem is in this function. Instead of it being void, I want it to actually return each token so that I can store it accordingly in each variable for my structs. I.E. store the first token in Team.name, store second token in Team.goalScored.
void getToken(char myString[]){
char *pch;
pch = strtok(myString,"!|");
while(pch != NULL){
cout << pch <<endl;
pch = strtok(NULL, "!|");
}
}
How can I do this? I have thought about function getToken storing each part of the token in an array of strings of a knows size (since I know that I'll always have four parts, team name, scores, scores, team name), and then return that array. But an array of strings is a 2D array and I have looked that up and apparently, you cannot return a 2D array from a function in C++.
Can someone please help me?