Need help with this dont know whats wrong

Ok so im working on a big text game and i am having a problem with the loading from a file, im not sure whats wrong i cant seem to figure it out, im sure its something small or a slight oversight, but here is my code, i am having trouble with lines 75-79 in main.cpp


Also can i put my class constructors in a different file?

I believe the problem lies withinmain.cpp when i set up the default variables, i thin the program is reading from them instead of from the file but i dont know what to do.

the contents of the text file are:

Chay Hawk
0
Ramsey the Cat
1
0
100
1

main.cpp

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <string>
#include <vector>
#include <limits>
#include <fstream>
#include "Pet_Class.h"
#include "Player_Class.h"
#include "Game_Class.h"
#include "Prototypes.h"

using namespace std;

Player::Player(string PLAYERNAME,
               float PLAYERMONEY): PlayerName(PLAYERNAME),
                                   PlayerMoney(PLAYERMONEY)
{

}

Player::~Player()
{

}

Pet::Pet(string PETNAME,
         float PETHEALTH,
         float PETTHIRST,
         float PETSTAMINA): petName(PETNAME),
                            petHealth(PETHEALTH),
                            petThirst(PETTHIRST),
                            petStamina(PETSTAMINA)
{

}

Pet::~Pet()
{

}

Game::Game(bool ISFIRSTSTARTUP): isFirstStartup(ISFIRSTSTARTUP)
{

}

Game::~Game()
{

}

int main()
{
    Player player("Player1", 0);
    Game game(true);
    Pet pet("PetName", 100, 0, 100);

    NewGameSetup(player, game, pet);
    return 0;
}

/*
=============================
NewGameSetup()

This function is run when the
game is first started up
=============================
*/

void NewGameSetup(Player &player, Game &game, Pet &pet)
{
    LoadGame(player, game, pet); // Loads the game so if the player started a game already then the game wont go through the startup again. This mainly is used to get isFirstStartup but also loads everything else.

    //Debug
    cout << player.PlayerName << endl;
    cout << pet.petName << endl;
    cout << pet.petHealth << endl;
    cout << game.isFirstStartup << endl;
    //End of debug

    The output is:
    
    Chay Hawk
    Blank space, should be the pet name
    0
    1

    no matter what i do it wont change, also i changed the 1 in the file to 0 and it didnt change when the program started up so what do i do?

    if(game.isFirstStartup == false)
    {
        MainGame();
    }
    if(game.isFirstStartup == true)
    {
        cout << "Welcome to Text Pet, Lets get set up\n" << endl;

        cout << "Please enter your name" << endl;
        getline(cin, player.PlayerName);

        cout << '\n';

        cout << "Ok thank you " << player.PlayerName << " now please enter your pets name" << endl;
        getline(cin, pet.petName);

        SaveGame(player, game, pet);
        MainGame();
    }
}

void MainGame()
{
    cout << "MainGame Function debug test" << endl;
}

void PetStore()
{

}

void MainMenu()
{
    int lv_choice;

    cout << "TEXT PET\n" << endl;

    cout << "1) " << endl;

    switch(lv_choice)
    {

    }
}


Player_Class.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef PLAYER_CLASS_H_INCLUDED
#define PLAYER_CLASS_H_INCLUDED

#include <string>

class Player
{
    public:
        Player(std::string PLAYERNAME, float PLAYERMONEY);
        ~Player();

        std::string PlayerName;
        float PlayerMoney;
};

#endif // PLAYER_CLASS_H_INCLUDED 


Pet_Class.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef PET_CLASS_H_INCLUDED
#define PET_CLASS_H_INCLUDED

#include <string>

class Pet
{
    public:
        Pet(std::string PETNAME,
            float PETHEALTH,
            float PETTHIRST,
            float PETSTAMINA);
        ~Pet();

    std::string petName;
    float petHealth;
    float petThirst;
    float petStamina;
    //float pet
};

#endif // PET_CLASS_H_INCLUDED 


Prototypes.h

1
2
3
4
5
6
7
8
9
10
11
#ifndef PROTOTYPES_H_INCLUDED
#define PROTOTYPES_H_INCLUDED

void NewGameSetup(Player &player, Game &game, Pet &pet);
void SaveGame(Player &player, Game &game, Pet &pet);
void LoadGame(Player &player, Game &game, Pet &pet);
void MainGame();
void MainMenu();
void PetStore();

#endif // PROTOTYPES_H_INCLUDED 


SaveAndLoad.cpp

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <fstream>
#include "Player_Class.h"
#include "Pet_Class.h"
#include "Game_Class.h"

using std::ofstream;
using std::ifstream;
using std::endl;

void SaveGame(Player &player, Game &game, Pet &pet)
{
    ofstream SaveFile;

    SaveFile.open("TextPet.txt");

    SaveFile << player.PlayerName << endl;
    SaveFile << player.PlayerMoney << endl;
    SaveFile << pet.petName << endl;
    SaveFile << pet.petHealth << endl;
    SaveFile << pet.petThirst << endl;
    SaveFile << pet.petStamina << endl;
    SaveFile << game.isFirstStartup << endl;

    SaveFile.close();
}

void LoadGame(Player &player, Game &game, Pet &pet)
{
    ifstream LoadFile;

    LoadFile.open("TextPet.txt");

    getline(LoadFile, player.PlayerName);
    LoadFile >> player.PlayerMoney;
    getline(LoadFile, pet.petName);
    LoadFile >> pet.petHealth;
    LoadFile >> pet.petThirst;
    LoadFile >> pet.petStamina;
    LoadFile >> game.isFirstStartup;

    LoadFile.close();
}
Last edited on
This
1
2
    LoadFile >> player.PlayerMoney;
    getline(LoadFile, pet.petName);
is the problem: LoadFile >> player.PlayerMoney; leaves the end of line in the stream and getline(LoadFile, pet.petName); gets it as an empty line

Use ignore() before the getline():

http://www.cplusplus.com/reference/istream/istream/ignore/
then my program doesnt work, my program come up and its just a blank cmd screen. I just switched around the names save and load spot so it shouldnt do that again but i still would like to know hwo to fix this problem if it ever happens again.

1
2
3
4
5
6
7
SaveFile << player.PlayerName << endl;
    SaveFile << pet.petName << endl;
    SaveFile << player.PlayerMoney << endl;
    SaveFile << pet.petHealth << endl;
    SaveFile << pet.petThirst << endl;
    SaveFile << pet.petStamina << endl;
    SaveFile << game.isFirstStartup << endl;


also the isFirstStartub boolean value is not changing when i change it in the text file, it still shows up in the window as 1 even though i changed it to 0.

Im not sure but i believe the game might be reading from the objects in main, because no matter how i change it in the file it always is the same but when i change the objects values then thats when the stuff changes so what do i need to do?
Last edited on
bump
I seem to have gotten it working, but only time will tell, can i put my constructors in a .h file? if so do i need to put them all in their own or can they be put in the same .h file?
you can put the declarations in the header/.h file and the implementation in the .cpp.

However you can do 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
26
27
28
#ifndef PET_CLASS_H_INCLUDED
#define PET_CLASS_H_INCLUDED

#include <string>

class Pet
{
    public:
        Pet(string PETNAME,
         float PETHEALTH,
         float PETTHIRST,
         float PETSTAMINA): petName(PETNAME),
                            petHealth(PETHEALTH),
                            petThirst(PETTHIRST),
                            petStamina(PETSTAMINA)
{

}
        ~Pet();

    std::string petName;
    float petHealth;
    float petThirst;
    float petStamina;
    //float pet
};

#endif // PET_CLASS_H_INCLUDED  
That makes the constructor (or any other function of a class) implicit inline
Topic archived. No new replies allowed.