stroring game data

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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

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 */
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.


Show us this code - if you've already coded this part, you are 90% of the way there already.
something like this in main reads a file, but i dont know how to do it with methods and objects

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 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();
    }

You put that chunk of code inside this:
1
2
3
4
bool Game::fillGame(ifstream & din)
{

}


and use din instead of readarrayFile. Also, replace stored with round_Of and other member variables. However, I recommend that you do the strings last - they may be a little tricky (requiring delimiters, etc...).

Try that first and then show us your new code.
before i go ahead any farther i just want to double check and see it i am on the right track.

something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

bool Game::fillGame(ifstream &din, string player_1, string player_2, double player_1_score, double player_2_score, int round_Of)
{
int round_Of;
ifstream din;


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

}

No that's wrong.

Stop right now and pull up a book on C++ programming.
Read up on classes and class members and then answer your own question.

Without understanding the basics, it would take me a long time to explain everything over a BBS.
alright, i will. although i thought i kind of understood the basics. for example

i wrote the following small program (the below is not the whole thing, just pieces to show i understand)


if i had a temp.h with

1
2
3
4
5
void setKelvin(double Temperature);

        double getKelvin();

        void print();


and a temp.cpp with correct methods for converting and returning the correct output with a print



and in main.cpp i would ask for input and then call it with a temp object.

1
2
3
4
5
6
7
8
9
10
11

Temp input;
//asks user for input
    cout << "Please enter a temperature in degrees Celsius" << endl;
    cout << "Temp in degrees: ";
    cin >> userInput;

    
    // outputs what the user input
    input.setCelsius(userInput);
    input.print();
Last edited on
Give you a clue - Line 2:

bool Game::fillGame(ifstream &din, string player_1, string player_2, double player_1_score, double player_2_score, int round_Of)

is wrong.

If you can figure out why, then maybe you understand the basics.
Topic archived. No new replies allowed.