无法更新阵列清单

时间:2018-11-26 00:01:25

标签: c++

我正在尝试遍历数组,但是具有多行的播放器编号要么不累积其第二个实例,要么获得奇怪的编号(最后一个)。

    PlayerHitsWalksOuts
   1     2   2   2
19   0   5   7
 2   0   5   7
18   4   2   0
 4   3   3   6
12   2   2   2
 7   0   0   9 // should be 7 0 0 6 , because two 7 0 0 3 lines in input file
 8   1   4   1
10   2   2   2
 3   3   3   6
11   6   0   0
17   4   2   0
 9   3   2   1

由于我确定我正确编写了main(),所以我迷失了为什么它不起作用。我认为该类文件也是正确的。将索引设置为0时,出现“分段错误(核心已转储)”。

#include "Player.h"
#include <iostream>
#include <fstream>

int findNumber(const Player p[], int numPlayers, int playerNumber);
void displayArray(Player team[], int team_size);

int main()
{
    fstream fin("baseball.txt");
    const int LIST_LENGTH = 20;

    int number = 0,
        hits,
        walks,
        outs,
        playerIndex,
        index = -1,
        teamSize = 0;

    cout << "This program tracks a baseball player's number "
         << "and their\nnumber of hits, walks, and outs for "
         << "each games in a season.\n";

    Player team[LIST_LENGTH];

    while (!fin.eof())
    {
        fin >> number >> hits >> walks >> outs;

        playerIndex = findNumber(team, teamSize, number);

        if (playerIndex == -1)
        {
            teamSize++;
            index++;
            team[index].setNumber(number);
            team[index].setHits(hits);
            team[index].setWalks(walks);
            team[index].setOuts(outs);
        }
        else
        {
            team[index].setHits(hits + team[index].getHits());
            team[index].setWalks(walks + team[index].getWalks());
            team[index].setOuts(outs + team[index].getOuts());
        }

        displayArray(team, teamSize);

        fin.close();
    }
}

int findNumber(const Player p[], int numPlayers, int playerNumber)
{
    int i;
    for (i = 0; i < numPlayers; i++)
    {
        if (p[i].getNumber() == playerNumber)
            return i;
    }
    return -1;
}


void displayArray(Player team[], int team_size)
{
    cout << "\n\nPlayer\tHits\tWalks\tOuts\n"
         << "------\t----\t-----\t----\n";

    for (int i = 0; i < team_size; i++)
    {
        cout << team[i] << endl;
    }
}

player.h:

{#include "Player.h"
#include <iostream>
#include <iomanip>
using namespace std;

Player::Player()
    {
    Number = Hits = Walks = Outs = 0;
    }

int Player::getNumber() const
    {
    return Number;
    }

int Player::getHits() const
    {
    return Hits;
    }

int Player::getWalks() const
    {
    return Walks;
    }

int Player::getOuts() const
    {
    return Outs;
    }

void Player::setNumber(int n)
    {
    Number = n;
    }

void Player::setHits(int h)
    {
    Hits = h;
    }

void Player::setWalks(int w)
    {
    Walks = w;
    }

void Player::setOuts(int o)
    {
    Outs = o;
    }
const Player& Player::operator=(const Player & p)
    {
      if (this != &p)
    {
      Number = p.Number;
      Hits = p.Hits;
      Walks = p.Walks;
      Outs = p.Outs;
    }
      return *this;
    }
ostream& operator<<(ostream& out,  const Player & p)
    {
      out << setw(2) << p.Number << "\t" 
      << setw(2) << p.Hits << "\t" 
      << setw(2) << p.Walks << "\t"
      << setw(2) << p.Outs;
      return out;
    }

1 个答案:

答案 0 :(得分:1)

我不会对Why is iostream::eof inside a loop condition considered wrong?讲太多,因为在显示的代码中这无关紧要。使用输入操作本身作为循环的条件:

while (fin >> number >> hits >> walks >> outs)
{

在应该从中读取的循环中关闭输入文件:

while ( /* ... */ )
{
    // ...

    playerIndex = findNumber(team, teamSize, number);

    if (playerIndex == -1)
    // ...

    displayArray(team, teamSize);
    fin.close();  // <<========================= here. Remove that.
}

您将indexplayerIndex混淆了:

    // here you try to find the player by its number in the team
    playerIndex = findNumber(team, teamSize, number);

    if (playerIndex == -1)
    {
        teamSize++;
        index++;
        team[index].setNumber(number); // index could be ok in that case,
        // ...                         // but why use an extra variable
    }                                  // when teamSize (before it gets in-
    else                               // crementet) is the same?
    {
        // here you should be using the index that you found with
        // findNumber() instead of index. So replace index with playerIndex
        team[index].setHits(hits + team[index].getHits());
        team[index].setWalks(walks + team[index].getWalks());
        team[index].setOuts(outs + team[index].getOuts());
    }

如果您实现了operator+=,则它的代码可能会更短,更容易阅读,Player是一个接受值的构造函数,并且可能是类#include <cstdlib> // EXIT_FAILURE #include <fstream> #include <iostream> #include <iomanip> #include <algorithm> // std::find, std::copy #include <iterator> // std::ostream_iterator class Player { int Number = 0; int Hits = 0; int Walks = 0; int Outs = 0; public: Player() = default; Player(int number, int hits, int walks, int outs) : Number {number}, Hits {hits}, Walks {walks}, Outs {outs} {} Player& operator+=(Player const &other) { Hits += other.Hits; Walks += other.Walks; Outs += other.Outs; return *this; } int getNumber() const { return Number; } friend std::istream& operator>>(std::istream& in, Player &p) { int number, hits, walks, outs; if (in >> number >> hits >> walks >> outs) p = Player{ number, hits, walks, outs }; return in; } friend std::ostream& operator<<(std::ostream& out, Player const &p) { return out << std::setw(2) << p.Number << "\t" << std::setw(2) << p.Hits << "\t" << std::setw(2) << p.Walks << "\t" << std::setw(2) << p.Outs; } }; bool operator==(Player const &lhs, int Number) noexcept { return lhs.getNumber() == Number; } std::size_t findNumber(Player const *team, std::size_t numPlayers, int playerNumber) { return std::find(team, team + numPlayers, playerNumber) - team; } void displayArray(Player *team, std::size_t numPlayers) { std::cout << "\n\nPlayer\tHits\tWalks\tOuts\n------\t----\t-----\t----\n"; std::copy(team, team + numPlayers, std::ostream_iterator<Player>{ std::cout , "\n" }); } int main() { constexpr std::size_t LIST_LENGTH{ 20 }; Player team[LIST_LENGTH]; std::size_t teamSize{}; std::ifstream fin("test.txt"); if (!fin.is_open()) { std::cerr << "Couldn't open the players list for reading :(\n\n"; return EXIT_FAILURE; } Player p; while (teamSize < LIST_LENGTH && fin >> p) { auto playerIndex{ findNumber(team, teamSize, p.getNumber()) }; if (playerIndex == teamSize) team[teamSize++ ] = p; else team[playerIndex] += p; } displayArray(team, teamSize); } 的流提取运算符:

{{1}}