one second, the code you gave me for naming the file is still bugged
c:\users\snaef98\documents\visual studio 2008\projects\kingdom of mythos revised\kingdom of mythos revised\kingdom of mythos revised.cpp(1346) : error C2664: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const char *'
ofstream myfile (std::string("C:/KingdomOfMythos/Saves/" + filename + ".txt").c_str());
Next time please specify which line caused the error. It can be very hard without knowing it.
Well the save and load sorta worked, the save works, the load however messes up the strings because some of them are descriptions for items or names of stuff, IE it has spaces like
This is how I would:
SaveGame() function could remain the same, but the LoadGame() would look a bit different:
1 2 3 4 5 6 7 8 9 10
void LoadGame() {
ifstream in("savegame.txt");
string line;
getline(in, line); //This reads a whole line into varible 'line';
//The next step depends on what is the type of the variable you want the contents of 'line' to be saved
//if it's a string then it's simple : string_variable = line;
//if it's a number then you have to convert 'line' to for example int. To how to do this read this article and pick the method you like best : http://cplusplus.com/articles/numb_to_text/
//Do the above method for every variable. Again I recommend to use arrays, it will make your code much much sorter and managable.
}
Alternatively you can make short functions like these :
1 2 3 4 5 6 7 8 9 10 11
string LoadString(ifstream& in) {
string res;
getline(in, res);
return res;
}
int LoadInt(ifstream& in) {
string line;
getline(in, line);
return boost::lexical_cast<int>(line); //See the above link, if you haven't downloaded boost yet (highly recommended!) and want to use some other method
}
Again : don't mix getline() with operator>> on the same stream, choose either one and stick to it. It is possible to mix them, but it's error prone, and makes your life much harder.
For now im going to stick with the system you helped me design. Im just making sure users input _ for space on anything with strings which for the time being is only PlayerName which they input only once... the rest is all pre-programmed im using _ on all other strings instead of spaces and its worked fine. If other problems arise ill call on you but im gonna mark this topic as solved for now. i appreciate your help.
For now im going to stick with the system you helped me design. Im just making sure users input _ for space on anything with strings which for the time being is only PlayerName which they input only once... the rest is all pre-programmed im using _ on all other strings instead of spaces and its worked fine. If other problems arise ill call on you but im gonna mark this topic as solved for now. i appreciate your help.
What you want is some object serialization and de-serialization mechanism. We have that in Standard Java. Despite C++ STL advancement, it still lacks behind Java in terms of API rich-ness and varieties. I can forsee this to be one of them.
So I believe different C++ developers build their "own" version for write and read objects from file. For performance I would think write/read in binary format would be better than write/read in text format. Making this part of C++ STL would be great in future. Purely my opinion though.
I notice about Boost from this forum and take a look. It seem to be C++ add-on for those classes that are missing from the Standard C++ STL. I wonder it seem some Boost libraries are so useful that most developers would want them but somehow the C++ committee is a bit slow in approving them into the C++ standard.
For Java, the process is much shorter. Once certain classes are deemed so useful, the next JDK release would make them as part of the core. The time taken to approve as standard for Java is much faster.
Maybe this is my complaint but C++ committee should work faster to incorporate Boost libraries into Standard C++.