1

I need to read a .txt file into an array and store them in a deck so to speak. The .txt file looks like this:

1 Dwarf 2 4
11 Armour 1
6 Wall 0 5
10 Sword 2
4 Bless 2 2
1 Cannon 4 1
1 Dwarf 2 4
1 Cannon 4 1
2 Fireball 3
1 Cannon 4 1

Each line is: type name attack and health.

I am only allowed to use type 1 cards; i.e. I need a deck for each type.

I have looked but I am struggling to find an answer for this specific problem. Can anyone point me in the right direction using plain English?

Bilal Siddiqui
  • 349
  • 3
  • 17
  • 6
    Arguably, your best bet at this point are these [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron Aug 12 '18 at 15:11
  • 1) Define a card-representing structure; 2) Define a function that reads and returns one such card; 3) Use the function repeatedly. – molbdnilo Aug 12 '18 at 15:15
  • If you do some google search, you can absolutely find plenty number of examples. – eneski Aug 12 '18 at 15:33
  • I have googled but i am struggling to understand how to implement as i have 2 files to do this with in the same program. – Matthew Alan Gibson-Storey Aug 12 '18 at 15:49
  • Search the internet for "c++ read file struct space separated". There are already a plethora of similar questions on StackOverflow. – Thomas Matthews Aug 12 '18 at 16:08
  • What is a *type 1* card and how does it differ from a *type 2* card? – Thomas Matthews Aug 12 '18 at 16:13
  • type 2 is just a different type of minion – Matthew Alan Gibson-Storey Aug 12 '18 at 16:26
  • There are some problems with your English. In particular, I don't understand the meaning of this sentence: "I am only allowed to use type 1 cards so also need to figure this out." – Patrick Parker Aug 12 '18 at 16:45
  • i mean there re multiple types of card i.e type 1, type 2 etc. however i am only allowed to use type 1 cards so need to filter these so they are the only ones iin the deck – Matthew Alan Gibson-Storey Aug 12 '18 at 16:57
  • " i am only allowed to use type 1 cards so need to filter these" @MatthewAlanGibson-Storey ok, then How can you recognize the card type? Do you already know how to filter (irrelevant detail) or is that part of your question (too broad) ? – Patrick Parker Aug 12 '18 at 19:57

1 Answers1

3

Let's assume your records look like this:

1 Dwarf 2 4

To model this in a class:

class Type_1_Card
{
    int         type;
    std::string name;
    int         attack;
    int         health;
};

In modeling, you choose a type and member that best fits the column of data.
Each row will be an instance of the model.

Speaking of rows, you'll need some kind of container for each row (otherwise known as a database). Since the number of rows is unknown at run-time, use a std::vector, because it can expand as necessary.

std::vector<Type_1_Card> database;

To honor the principles of data hiding and encapsulation, we'll have the class input the data, since the class knows the layout (fields and members). C++ allows overloading of operators and methods, so we'll overload a familiar method: operator>>.

class Type_1_Card
{
     // yada, yada, yada
  public:  
     friend std::istream& operator>>(std::istream& input, Type_1_Card& card);
};

std::istream&
Type_1_Card ::
operator>>(std::istream& input, Type_1_Card& card)
{
    input >> card.type;
    input >> card.name;
    input >> card.attack;
    input >> card.health;
    return input;
}

This hides the data by allowing the following code to read in the cards:

std::fstream data_file(/*...*/);
Type_1_Card card;
while (data_file >> card)
{
    database.push_back(card);
}

Magically, the contents of the card are not exposed, hidden, and the functionality is encapsulated into the class or object.

Edit 1: Expanding the encapsulation
In your program, you may want to find a card by name. You can add a method to the class, then search the database:

class Type_1_Card
{
    // yada, yada, yada
  public:
    bool is_card_name(const std::string& key_name) const
    {
        // Remember, comparison is case sensitive, 'A' != 'a'
        return name == key_name;
    }
};

This allows you to search the database for a card using a name.
Here's the brute force technique. There are simpler methods that use library functions, but that is left for further research.

const size_t index = 0U;
const size_t size = database.size();
for (index = 0; index < size; ++index)
{
    if (database[index].is_card_name("Dwarf"))
    {
        std::cout << "Dwarf card found at index " << index << "\n";
    }
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154