C ++ .txt读入问题。 getline阅读完整档案

时间:2016-04-07 23:17:50

标签: c++11 ifstream getline

首先,请原谅我的丑陋代码。我试图修复这些代码的大量想法在所有可能无法解决的潜在解决方案之后混淆了。基本上,我编写了一个Hearthstone rip-off,用卡片信息读取两个.txt文件并与他们进行战斗以查看哪个玩家获胜。问题在于,当我试图保存播放器的名称(文件中的第一行)时,它会保存整个文件而不是第一行。当我设法解决这个问题时,用于保存卡对象信息的for循环(格式:卡名称,卡片电源,卡片运行状况)由于某种原因无法正确保存。任何帮助将不胜感激,我已经尝试解决这个问题两天没有完全解决问题。我会先在代码之前附上读入的文件。

免责声明:这有很多内容,我很抱歉。另外我认为问题可能是我的Mac没有以具有正确行结尾的格式保存.txt。我使用XCode作为我的IDE。非常感谢任何愿意提供帮助的人!

File1中:

The Innkeeper
3
Tunnel Trogg
1
3
Neptulon
7
7
Fire Elemental
6
5

文件2:

Malfurion
3
Leper Gnome
2
1
Aviana
5
5
Cenarius
5
8

主:

#include "Player.h"

using namespace std;

int main()
{
cout << "Please enter file name of the first player: " << endl;
string inFile = "";
getline(cin, inFile);

Player* p1 = new Player(inFile);

cout << "Now enter the file name of the second player: " << endl;
getline(cin, inFile);
Player* p2 = new Player(inFile);

p1->battle(*p2);

delete p1;
delete p2;

return 0;
}

玩家标题:

#include "Card.h"
#include <fstream>

#ifndef Player_h
#define Player_h
using namespace std;
class Player
{
private:
    string playerName;
    int numCards;
    Card ** cards;
    int wins = 0;
public:
    Player(std::string inFile);
    void battle(Player p2);
    Card* getCard(int counter);
    ~Player();
};
#endif /* Player_h */

卡片标题:

#include <string>
#include <iostream>

#ifndef Card_h
#define Card_h

using namespace std;

class Card
{
public:
    Card();
    string getName();
    int getPower();
    int getHealth();
    void setName(string newName);
    void setPower(int newPower);
    void setHealth(int newHealth);

    Card* duel(Card&);

    friend ostream& operator<<(ostream& o, Card& c);
    friend bool operator==(Card& p1Card, Card& p2Card);
private:
    string name;
    int power;
    int health;
};
#endif /* Card_h */

玩家来源:

#include "Player.h"

using namespace std;

Player::Player(string inFile)
{
    ifstream in(inFile, ios::in);\
    if (!in)
    {
        cerr << "There was a problem opening the file. Sorry, try again!" << endl;
        return;
    }

    getline(in, playerName);
    cout << playerName << endl;
    in>>numCards;

    playerName = "";
    numCards = 0;
    cards = new Card* [numCards];

    string tempName = "";
    int tempPower = 0;
    int tempHealth = 0;

    for (int i = 0; i<numCards; i++)
    {
        in.ignore();
        cards[i] = new Card();
        getline(in, tempName);
        cout << "in for loop: " << endl;
        cout << tempName << ",";
        cards[i]->setName(tempName);
        in >> tempPower;
        in.ignore();
        cout << tempPower << ",";
        cards[i]->setPower(tempPower);
        in >> tempHealth;
        cout << tempHealth << "             done"<< endl;
        cards[i]->setHealth(tempHealth);
    }
}

void Player::battle(Player p2)
{
    int draws = 0;

    cout << "Let the battle begin!" << endl;
    cout << numCards << endl;

    if (wins > p2.wins)
    {
        cout << playerName << " wins over " << p2.playerName << ", " << wins << " to " << p2.wins;

        if (draws == 0)
        {
            cout << " and no ties." << endl;
        }

        else
        {
            cout << " and " << draws << " ties." << endl;
        }
    }
    else if (p2.wins > wins)
    {
        cout << p2.playerName << " wins over " << playerName << ", " << p2.wins << " to " << wins;

        if (draws == 0)
        {
            cout << " and no ties." << endl;
        }

        else
        {
            cout << " and " << draws << " ties." << endl;
        }
    }
    else if (p2.wins == wins)
    {
        cout << "It is a draw between " << playerName << " and " << p2.playerName << ", with " << wins << " for each and ";
        if (draws == 0)
        {
            cout << "no ties." << endl;
        }

        else
        {
            cout << draws << " ties." << endl;
        }
    }

    cout << "Here are the detailed results:" << endl;
    for (int i = 0; i < numCards; i++)
    {
        cout << *cards[i] << " vs. " << *p2.cards[i] << " - ";

        if (*cards[i] == *p2.cards[i])
        {
            cout << "It is a draw." << endl;
        }

        else if (cards[i]->duel(*p2.cards[i]) == NULL)
        {
            cout << "It is a draw." << endl;
        }

        else if (*cards[i]->duel(*p2.cards[i]) == *p2.cards[i])
        {
            cout << p2.cards[i]->getName() << "wins for " << p2.playerName << "." << endl;
        }

        else if (*cards[i]->duel(*p2.cards[i]) == *cards[i])
        {
            cout << cards[i]->getName() << "wins for " << playerName << "." << endl;
        }
    }
}


Player::~Player()
{
    if (cards != NULL)
    {
        for (int i = 0; i < numCards; i++)
        {
            if (cards[i] != nullptr)
            {
                delete cards[i];
                cards[i] = NULL;
            }
        };
    }
}

卡片来源:

#include "Card.h"
using namespace std;

Card::Card()
{
    name = "";
    power = 0;
    health = 0;
}

string Card::getName()
{
    return name;
}

int Card::getPower()
{
    return power;
}

int Card::getHealth()
{
    return health;
}

void Card::setName(string newName)
{
    name = newName;
}

void Card::setPower(int newPower)
{
    power = newPower;
}

void Card::setHealth(int newHealth)
{
    health = newHealth;
}

Card* Card::duel(Card& otherCard)
{
    if ((otherCard.getHealth() - this->getPower() <=0) && (getHealth() - otherCard.getPower() <= 0))
    {
        return NULL;
    }

    else if ((otherCard.getHealth() - this->getPower() >0) && (getHealth() - otherCard.getPower() >0))
    {
        return NULL;
    }

    else if (otherCard.getHealth() - this->getPower() <=0)
    {
        return this;
    }

    else if (this->getHealth() - otherCard.getPower() <=0)
    {
        return &otherCard;
    }

    return NULL;
}


ostream& operator<<(ostream& o, Card& c)
{
    o << c.getName() << " (" << c.power << ", " << c.health << ") " << endl;

    return o;
}

bool operator==(Card& p1Card, Card& p2Card)
{
    if (p1Card.health == p2Card.health &&
        p1Card.power == p2Card.power &&
        p1Card.name == p2Card.name)
    {
        return true;
    }
    else
    {
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码几乎是正确的。它可以读取播放器的名称和卡号,但您的代码如下所示:

in>>numCards;
playerName = "";
numCards = 0;
cards = new Card* [numCards];

首先,它读取卡的数量并将其存储到numCards,这是对的。 接下来,你清除numCards的值,然后,你丢失了卡的数量,所以跟随它的代码用numCards == 0执行 您只需对numCards = 0行进行注释,您的代码即可正确执行。