C++11 C++。txt读入问题。getline读取完整文件

C++11 C++。txt读入问题。getline读取完整文件,c++11,ifstream,getline,C++11,Ifstream,Getline,首先,请原谅我的代码很丑。在所有潜在的解决方案都不起作用之后,我得到的试图修复这段代码的大量想法把它弄得一团糟。基本上,我正在编写一个炉石翻版,它读取两个包含卡信息的.txt文件,并与它们战斗,以查看哪个玩家获胜。问题是,当我试图保存玩家的名字(文件中的第一行)时,它会保存整个文件,而不仅仅是第一行。当我设法解决这个问题时,由于某种原因,用于保存卡对象信息(格式:卡名称、卡电源、卡运行状况)的for循环无法正确保存。任何帮助都将不胜感激,我已经试着解决这个问题两天了,但没有任何东西能完全解决这个

首先,请原谅我的代码很丑。在所有潜在的解决方案都不起作用之后,我得到的试图修复这段代码的大量想法把它弄得一团糟。基本上,我正在编写一个炉石翻版,它读取两个包含卡信息的.txt文件,并与它们战斗,以查看哪个玩家获胜。问题是,当我试图保存玩家的名字(文件中的第一行)时,它会保存整个文件,而不仅仅是第一行。当我设法解决这个问题时,由于某种原因,用于保存卡对象信息(格式:卡名称、卡电源、卡运行状况)的for循环无法正确保存。任何帮助都将不胜感激,我已经试着解决这个问题两天了,但没有任何东西能完全解决这个问题。我将在代码之前先附加读入文件

免责声明:有很多行,对此我很抱歉。此外,我认为问题可能是我的Mac电脑没有将.txt保存为具有正确行结尾的格式。我正在使用XCode作为我的IDE。非常感谢任何愿意帮忙的人

文件1:

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
主要内容:

#包括“Player.h”
使用名称空间std;
int main()
{

cout您的代码几乎正确。它可以读取玩家的姓名和卡号,但您的代码显示如下:

in>>numCards;
playerName = "";
numCards = 0;
cards = new Card* [numCards];
首先,它读取卡的num并将其存储到numCards,这是正确的。 接下来,您清除numCards的值,然后,您丢失了卡的num,因此它后面的代码将以
numCards==0执行
您只需注释行
numCards=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;
    }
}
in>>numCards;
playerName = "";
numCards = 0;
cards = new Card* [numCards];