p. bosch
p. bosch

Reputation: 139

Reading string from a file and saving different parts of it in different variables in C++

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++.

Upvotes: 0

Views: 299

Answers (3)

salek
salek

Reputation: 444

Here's a complete solution for your problem. Hope it helps!

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <algorithm>

using namespace std;
#define WIDTH 10

int main()
{
    string team1, team2; 
    int   team1goals, team2goals;

     // Data structure for storing Team name and scores
     map<string, pair<int, int>> scores;

    while (cin >> team1 >> team1goals >> team2 >> team2goals ) {
          scores[team1].first  +=   team1goals; 
          scores[team2].second  +=  team1goals;

          scores[team2].first += team2goals;
          scores[team1].second += team2goals;
    }

    cout << endl << setw (WIDTH) << "Team Name"  << setw(WIDTH) << "given" << setw(WIDTH) << "received";
    for_each( begin(scores), end(scores), [&]( pair<string, pair<int,int>> i ) {
        cout << endl << setw(WIDTH) << i.first  << setw(WIDTH) << i.second.first << setw(WIDTH) << i.second.first;
    });

    return 0;
}

heres the result

/*
input file: inputFile.txt
-------------------------------
Malaga 1 Levante 1
Malaga 1 Osasuna 1
Osasuna 1 Deportivo 1
Osasuna 1 Madrid 2
Osasuna 1 Levante 1
Osasuna 1 Malaga 1
-------------------------------

runs as:
 yourexename < inputFile.txt
-------------------------------
output:

 Team Name     given  received
 Deportivo         1         1
   Levante         2         2
    Madrid         2         2
    Malaga         3         3
   Osasuna         5         5

*/

Upvotes: 0

Tony
Tony

Reputation: 848

I'd use Find and Substr.

You can find the first space and then get the substring from 0 up to that space. Then get the number which is +1 after the space, and then make a new string out of the rest of string and do the same thing with the other name.

Upvotes: 0

JBL
JBL

Reputation: 12907

I suggest you have a look at this post explaining how to split a string using a delim

In your case, you might want to use a space to split your string. Since you have a fixed format, you can then retrieve the information you want (team and goals) by accessing the vector's elements as an array, with hard coded indexes, if you're sure your file will always have the same format.

Upvotes: 1

Related Questions