Error making a save game...

In response to my previous topic I tried making a savegame. Sometimes it works, sometimes the variables just become extremely hgh numbers.

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
void save(hero& Hero)
{
    system("CLS");
    ofstream save("save.txt");

    save<< Hero.getNaam() << "\n";
    save<< Hero.getWep() << "\n";
    save<< Hero.getShield() << "\n";
    save<< Hero.getMaxHp() << "\n";
    save<< Hero.getHp() << "\n";
    save<< Hero.getMoney() << "\n";
    save<< Hero.getArmor() << "\n";
    save<< Hero.getDmg1() << "\n";
    save<< Hero.getDmg2() << "\n";
    save<< Hero.getPotion() << "\n";
    save<< Hero.getLevel() << "\n";
    save<< Hero.getXp() << "\n";
    save<< Hero.getWepDmg() << "\n";

    cout<<"Saving game...";
    cout<<"\n\nProgress: ";
    for(int i = 1; i != 40; ++i){
    Sleep(50);
    cout<<"|";
    }


    save.close();
}


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
void load(hero& Hero)
{
    string naam;
    string wep;
    string shield;
    int maxHp;
    int hp;
    int money;
    int armor;
    int dmg1;
    int dmg2;
    int potion;
    int level;
    int xp;
    int wepDmg;

    ifstream load("save.txt");


    load>> naam >> wep >> shield >> maxHp >> hp >> money >> armor >> dmg1 >> dmg2 >> potion >> level >> xp >> wepDmg;
    Hero.setNaam(naam);
    Hero.setWep(wep);
    Hero.setShield(shield);
    Hero.setMaxHp(maxHp);
    Hero.setHp(hp);
    Hero.setMoney(money);
    Hero.setArmor(armor);
    Hero.setDmg1(dmg1);
    Hero.setDmg2(dmg2);
    Hero.setPotion(potion);
    Hero.setLevel(level);
    Hero.setXp(xp);
    Hero.setWepDmg(wepDmg);

    load.close();

}


I use this to show all variables:

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
void stats(hero Hero)
{
    stats:
    system("CLS");
    cout<<setw(20)<<left<<"Name:"<<Hero.getNaam()<<"\n\n";
    cout<<setw(20)<<left<<"Hero level:"<<Hero.getLevel()<<"\n\n";
    cout<<setw(20)<<left<<"XP points:"<<Hero.getXp()<<"\n\n";
    cout<<setw(20)<<left<<"XP to next lvl:"<<levels[XP]-Hero.getXp()<<"\n\n";
    cout<<setw(20)<<left<<"Hitpoints:"<<Hero.getHp()<<"\n\n";
    cout<<setw(20)<<left<<"Money:"<<Hero.getMoney()<<"\n\n";
    cout<<setw(20)<<left<<"Weapon:"<<Hero.getWep()<<"\n\n";
    cout<<setw(20)<<left<<"Attack power:"<<Hero.getDmg1() + Hero.getWepDmg()<<"-"<<Hero.getDmg2() + Hero.getWepDmg()<<"\n\n";
    cout<<setw(20)<<left<<"Shield:"<<Hero.getShield()<<"\n\n";
    cout<<setw(20)<<left<<"Defense power:"<<Hero.getArmor()<<"\n\n";
    cout<<setw(20)<<left<<"Potions:"<<Hero.getPotion()<<"\n\n\n";
    cout<<"9: Exit statistics";
    int keuze = getch()-'0';

        if(keuze != 9){
        cout<<"ERROR, try again";
        Sleep(2000);
        system("CLS");
        goto stats;
    }
}



I don't see why it works sometimes, other times it doesnt...
Hmmm... My guess is that it works when your hero's name, weapon and shield have no space characters. And it doesn't work when at least one of these variables has at least one space character. Check your previous topic and see what Galik and R0mai said about getting strings.

http://cplusplus.com/forum/beginner/28251/#msg151981

This example could also help:

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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

void input_f1(string & str, int & num);
void input_f2(string & str, int & num);
void input_f3(string & str, int & num);
void clear_cin();

int main()
{
    string str;
    int num;

    cout << "enter a string and a number "
        "(in separate lines):\n";
    input_f1(str,num);
    cout << "\nyou entered:\n" << str << '\n' << num << endl;
    clear_cin();

    cout << "\nenter another string and a number "
        "(in separate lines):\n";
    input_f2(str,num);
    cout << "\nyou entered:\n" << str << '\n' << num << endl;
    clear_cin();

    cout << "\nenter yet another string and a number "
        "(in separate lines):\n";
    input_f3(str,num);
    cout << "\nyou entered:\n" << str << '\n' << num << endl;

    cout << "\nhit enter to quit...";
    cin.get();

    return 0;
}

void input_f1(string & str, int & num)
{
    cin >> str;
    cin >> num;
}

void input_f2(string & str, int & num)
{
    getline(cin,str);
    cin >> num;
}

void input_f3(string & str, int & num)
{
    string input;

    getline(cin,str);
    getline(cin,input);
    istringstream(input)>>num;
}

void clear_cin()
{
    cin.clear();
    while (cin.get()!='\n');
}

Try entering strings containing spaces and see the difference between f1 and f2. f3 is a somewhat more sophisticated version of f2; it doesn't affect your input stream's status.

Info on stringstream -> http://cplusplus.com/reference/iostream/stringstream/
Last edited on
Topic archived. No new replies allowed.