I'm trying to create two functions that save and load a game. The save function just saves stats, a counter, and the current 2-D map into the save.
For some reason, when I try to save the character string name, it only saves the last letter. No idea why...
Furthermore, in my load, my counter is not being read (not sure if this is caused by name not being read as well). This in turn causes my array buffer to be unable to read what it is supposed to read.
Before I wrote this code, I wrote a mini version of this as a way to test it out. It only included the hero.hp and the map. It was a successful test, which was why I continued to write everything down for it. However, this came up.
This is my header file.
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
|
#include <string>
#include<iostream>
using namespace std;
typedef class _STATS_ {
public:
int atk;
int def;
int agi;
};
class _OVERALL_ {
public:
_STATS_ ability;
string name;
int hp;
double crit;
};
void runetype();
void main();
void mob();
void herostats();
//void boss();
|
This is where my load and save function are.
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "struct.h"
using namespace std;
_OVERALL_ hero;
char map[20][60];
void save(void) //int counter
{
hero.ability.atk=50;
hero.ability.agi =20;
hero.ability.def = 30;
hero.name = 'nice'; //<---problem here as 'nice' is only saved as an 'e' on the text file. No idea why..
int counter = 4;
for (int i=0;i<20;i++)
{
for (int o=0;o<60;o++)
{
map[i][o]='W';
}
}
ofstream save;
save.open("save.txt", ios::trunc);
save <<hero.hp<<endl<< //save hero stats
hero.ability.atk<<endl<<
hero.ability.def<<endl<<
hero.ability.agi<<endl<<
counter <<endl<<
hero.name<<endl;
save.write((char*)map, sizeof (map));//save the map
save.close();//close connection
}
void load ()
{
char buffer[1220]; //buffer
int i =0;
char hp[5];
char attack[5];
char def[5];
char agi[5];
char counter[1];
char name[20];
int INThp;
int INTattack;
int INTdef;
int INTagi;
int INTcounter;
ifstream load; //open stream
load.open ("save.txt");
if(load.fail())
{
cout<<"fail open";
}
else
{
cout<<"open succesful"<<endl;
}
if (!load.eof()) //read until end of file
{
load.getline(hp,5);
stringstream convert0(hp); //conver CHAR to INT on HP
if ( !(convert0 >> INThp) )
{
INThp=0;
}
hero.hp=INThp;
cout<<INThp;
load.getline(attack,5);
stringstream convert1(attack); //conver CHAR to INT on ATK
if ( !(convert1 >> INTattack) )
{
INTattack=0;
}
hero.ability.atk=INTattack;
cout<<INTattack;
load.getline(def,5);
stringstream convert2(def);//conver CHAR to INT on DEF
if ( !(convert2 >> INTdef) )
{
INTdef=0;
}
hero.ability.def=INTdef;
cout<<INTdef;
load.getline(agi,5);
stringstream convert3(agi);//conver CHAR to INT in AGI
if ( !(convert3 >> INTagi) )
{
INTagi=0;
}
hero.ability.agi=INTagi;
cout<<INTagi;
load.getline(counter,sizeof(counter));
stringstream convert4(counter);//conver CHAR to INT counter
if ( !(convert4 >> INTcounter) )
{
INTcounter=0;
}
cout<<counter; //cout counter<---error here. It returns as 0, when it should be 4.
cout<<INTcounter;// cout INTcounter
load.getline(name,20);
cout<<name; //cout name <--Error here as nothing came out.
load.getline(buffer,sizeof (buffer)); //Retrieve characters in map and give it to buffer up until ! mark
for (int y=0;y<20;y++) //transfer buffer values to maze
{
for(int x=0;x<60;x++)
{
map[y][x] = buffer[i];
i++;
}
}
}
load.close();
for (int y=0;y<20;y++)
{
for(int x=0;x<60;x++)
{
cout<<map[y][x];
}
}
system ("pause");
}
void main (void)
{
int x;
cout<<"1: load"<<endl;
cout<<"2: save"<<endl;
cin>> x;
if (x==1)
{
load();
}
else
{
save();
}
}
|
One more thing. Since my header already includes <iostream>, my cpp file does not need it right?