I am supposed to read in data from a file and store it. I know how to read from a file and display it, in main, what i don't understand is how to do that with classes and objects. This is the part im having a little trouble with and would appreciate any advice on how to go about figuring this out. Obviously this is an assignment and i am not asking for "answer code" but advice.


Create the Game class. Each instance of Game should store all data of a particular game (playerOne, playerTwo, playerOneScore, playerTwoScore, roundOf). For example, in Ohio State's first game of the 2011 NCAA Tournament, this information would be:

playerOne: "Ohio State"

playerTwo: "Texas-San Antonio"

playerOneScore: 75

playerTwoScore: 46

roundOf: 64

I am creating a Game.h file and Game.cpp file here is the Game.h file. if you have any advice on something i should add or take out that would be appreciated. this program is being written from scratch.

class Game
{
    // default constructor
    Game();

   

public:

    string player_One;
    string player_Two;

    double player_One_Score;
    double player_Two_Score;

    bool fillGame(ifstream & din);

    int round_Of;

    void Margin_Of_Victory;
    void print;

};

#endif	/* GAME_H */

for example in main this would read a file.

i just dont know how to do it with methods and objects

int stored;
ifstream readarrayFile;

   
    readarrayFile.open("file.txt");
    if(!readarrayFile)
        cout << "Error: file could not be found";
    else
    {
        readarrayFile >> stored;
        while(!readarrayFile.eof())
        {
            cout  << stored;
            readarrayFile >> stored;
        }
        cout << endl << endl;
        readarrayFile.close();
    }

It looks like your members in game are public, so all you have to do is make a game object and load it up with the data

Game g1;
g1.player_One = stored;

and so on
You just have to make sure that the data you read/write to a file is in the order that you will eventually code it in. So always store the data in a certain order and when you load the information back into your game object it will be the right data.

Edited 5 Years Ago by jjman4562: syntax

This article has been dead for over six months. Start a new discussion instead.