I am trying to read data from a file to an array of struct, and then print it.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct match {
string id;
string team1;
string team2;
string date;
string time;
string league;
};
void getmatches() {
int i = 0;
const int size = 50;
struct match {
string id;
string team1;
string team2;
string date;
string time;
string league;
};
match getmatches[100];
fstream show;
show.open("matches.txt");
while (! show.eof () && i < 10 )
{
getline(show, getmatches[i].id);
getline(show, getmatches[i].team1);
getline(show, getmatches[i].team2);
getline(show, getmatches[i].date);
getline(show, getmatches[i].time);
getline(show, getmatches[i].league);
cout << "the match id: " << getmatches[i].id << endl;
cout << "teams " << getmatches[i].team1 << "vs" << getmatches[i].team2 << endl;
cout << "time of match:" << getmatches[i].time << endl;
cout << "date of match " << getmatches[i].date << endl;
cout << "league:" << getmatches[i].league << endl << endl << endl;
i++;
}
return show.close();
}
int main() {
getmatches();
}
The file I am dealing with has each element in a line, and the problem is that it gives me an extra empty output. I tried to change the counter and condition, but it didn't work.
There is nothing more I can think of. I think it's a basic solution, but I can't figure it out.