So I have this file which has some strings and numbers, it's from the Spanish football league:
Malaga 1 Levante 1
Malaga 1 Osasuna 1
Osasuna 1 Deportivo 1
Osasuna 1 Madrid 2
Osasuna 1 Levante 1
Osasuna 1 Malaga 1
#
Okay, what I have to do is read this and then save the different teams (Malaga, Levante, Osasuna, Deportivo and Madrid) in 5 different variables, also I have to save the goals they made in one variable for each team and the goals they recieved in another one for each team. Here is the code I have:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const char FI='#';
const int MAX_EQUIPS=20;
struct Equip {
string nomEquip;
int golsf;
int golsc;
int punts;
};
typedef Equip TaulaEquip[MAX_EQUIPS];
struct EquipLliga {
TaulaEquip t;
int n;
};
int cercaEquip(EquipLliga l, string equip) {
// Pre: --
// Post: si equip no hi es a d.t, retorna -1
// altrament retorna posicio de l'equip de nom equip a d.t
int ret=l.n-1;
bool trobat= false;
while (ret>=0 and not trobat) {
if (l.t[ret].nomEquip.compare(equip)==0) trobat= true;
else ret--;
}
return ret;
}
void llegir(ifstream & f) {
string string1;
f.open("Lliga.txt");
char output;
if (f.is_open()) {
while (!f.eof()) {
getline(f,string1);
cout << string1 << endl;
}
}
f.close();
}
void actualitzacioGols(ifstream & f, EquipLliga & e) {
// Pre: f obert
// Post: ha llegit totes les dades de f, incorporat altes i traspasos a al, i els
// ingresos i despeses dels equips per altes, baixes i traspasos a d
char tipus;
string equipA, equipB;
int golsf=0, golsc=0, cerca;
e.n=0;
f >> tipus;
while (tipus!=FI) { // per cada equip
cerca=cercaEquip(e,equipA);
if (cerca=-1)
{
e[n].e.nomEquip=equipA;
e[n].e.golsf=l[n].e.golsf+golsA;
e[n].e.golsf=l[n].e.golsf+golsB;
}
else
{
e[cerca].e.golsf=l[cerca].e.golsf+golsA;
e[cerca].e.golsc=l[cerca].e.golsc+golsB;
}
lliga.n++;
cerca=cercaEquip(e,equipB);
if (cerca=-1)
{
e[n].e.nomEquip=equipB;
e[n].e.golsf=l[n].e.golsf+golsA;
e[n].e.golsf=l[n].e.golsf+golsB;
}
else
{
e[cerca].e.golsf=l[cerca].e.golsf+golsA;
e[cerca].e.golsc=l[cerca].e.golsc+golsB;
}
}
int main() {
}
I'm having problems with the function 'void actualitzacioGols(ifstream & f, EquipLliga & e)'. I don't know how to code it so that it reads to the first space and then saves it to the first team variable, 'equipA', and then the first number to the first goal variable, 'golsf' and the same with the other two.
Any idea or usefull tips to solve this? I'm kind of new to C++.