문제

I have a text file of NFL teams. I am having a problem when parsing through the string when it comes to teams with 2 names and not one. (i.e. New England and Pittsburgh) the next item in the file is an int. I also have to read these values into a linked list.

infile     >> t.date // t is a team struct which contains char name and ints
           >> t.name 
           >> t.W
           >> t.L
           >> t.T

Can I just use an if else statement between the name and Wins to check if the next char is a char? And then if it is a char it could just save the next word, "England" for the second half of New England's name in the same name field, and if its an int it will move on to the Wins field.

txt file ex

New England 2 4 0
Pittsburgh 1 6 0

the code above was what I was trying to use to assign the name to the team struct

   struct team
{
public:
    team& do_input(std::istream& is);

    std::string date, name, name2;
    int wins, 
        losses, 
        ties;
    std::string perc,
        home, 
        road,
        div,
        conf;
     int league;
};
   infile >>t.date;

   while (infile >> t)
    {
        t.do_input(infile) ;
        //cout << t.date << t.name;
        L.push_back(t);
        t.name2 = " ";
}
도움이 되었습니까?

해결책

Let's start by maintaining your code. The first thing I would do is create an overload of operator >> that takes an input stream and a team object.

std::istream& operator >>(std::istream& is, team& t);

This function should delegate the operation of extracting the input to a method called do_input():

std::istream& operator >>(std::istream& is, team& t)
{
    if (is.good())
        t.do_input(is);
    return is;
}

do_input should read objects of type char into the respective string objects in your team class. Speaking of your team class, it's much better to use std::string for representation of string values:

struct team
{
    ...
    std::string date;
    std::string name;

    int W, L, T;
};

This way the string can be as large enough as we need it. We no longer have to worry about a potential buffer overflow (i.e reading a string larger than 10 bytes). We can also use the convenient methods that std::string provides.

It's further recommended that you use good variable names so that maintainers of your code can know at a glance what it means. For instance, try these:

int wins, losses, ties;

Now it is clear to me (and to others) what these names imply.


So team::do_input is a member function that reads in the strings and integers from the file to the data members of the instance from which it is being called. Here's a full implementation:

struct team
{
public:
    team& do_input(std::istream&);
private:
    std::string date, name;
    int wins, losses, ties;
};

team& team::do_input(std::istream& is)
{
    std::getline((is >> date).ignore(), name);
    is >> wins >> losses >> ties;

    return *this;
}

Error reporting has been left out to keep the example concise. I hope this helped.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top