-1

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
seif410
  • 1
  • 1
  • well actually i know that but what is the replacement and why does give the extra output – seif410 Jun 02 '21 at 17:11
  • the question gives the info not the solution – seif410 Jun 02 '21 at 17:11
  • `addNewMatch` seems to be unused and therefore irrelevant to the question. Please try to avoid unnecessary "noise" in code you post to keep the code we have to read through to a minimum. – fabian Jun 02 '21 at 17:12
  • @seif410 "*what is the replacement*" - you could define an `operator>>` for `match`, and then use `while (i < 10 && show >> getmatches[i]) { /* cout getmatches[i] ... */ ++i; }` – Remy Lebeau Jun 02 '21 at 17:13
  • 2
    @seif410 "_the question gives the info not the solution_" Huh? The top answer, on that, linked, question, suggests that the construct `while(!inStream.eof()) {int data; inStream >> data;}` should be replaced with `int data; while(inStream >> data){}`. How is that not the solution? – Algirdas Preidžius Jun 02 '21 at 17:14
  • 1
    The answer to the question I linked to explains how using `stream.eof()` in the loop will lead to the loop running more times than it should. Your loop is running 1 extra time which is why you're getting extra output. – Kevin Jun 02 '21 at 17:17
  • ok i did that and the id won't show up – seif410 Jun 02 '21 at 17:19
  • Why are you defining the `struct match` twice (once globally, the other in the function). As it may be your favorite `struct`, there is no reason to declare it twice; there is extra effort, because if you to change one of the structs, you'll need to change both. Also, compilers are of good quality these days, so there's no need to remind the compiler. – Thomas Matthews Jun 02 '21 at 19:45

1 Answers1

2

Don't use eof() in a loop, the way you are. Define an operator>> for match instead.

There are other issues with your code, too. You have struct match defined twice. Your getmatches[] array has the same name as the function it belongs to, use a more unique name instead. And getmatches() has a void return value, but you are trying to return a (void) value, which is not necessary.

Try this instead:

#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;
};

istream& operator>>(istream &in, match &m) {
    getline(in, m.id);
    getline(in, m.team1);
    getline(in, m.team2);
    getline(in, m.date);
    getline(in, m.time);
    getline(in, m.league);
    return in;
}

ostream& operator<<(ostream &out, const match &m) {
    out << "the match id: " << m.id << "\n";
    out << "teams: " << m.team1 << " vs " << m.team2 << "\n";
    out << "time of match: " << m.time << "\n";
    out << "date of match: " << m.date << "\n";
    out << "league: " << m.league << "\n\n";
    return out;
}

void getmatches() {
    int i = 0;
    const int size = 50;
     
    match matches[size];

    ifstream show("matches.txt");
    while (i < size && show >> matches[i])
    {
        cout << matches[i] << endl;
        ++i;
    }
}

int main() {
    getmatches();
}

Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770