Text-based game, saving progress into some file, .sav? How to?

This is a discussion on Text-based game, saving progress into some file, .sav? How to? within the C++ Programming forums, part of the General Programming Boards category; Instead of Code: if(ctemp == '0') temp = 0; if(ctemp == '1') temp = 1; if(ctemp == '2') temp = ...

  1. #31
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    Instead of
    Code:
                if(ctemp == '0')
                  temp = 0;          
                if(ctemp == '1')
                  temp = 1;
                if(ctemp == '2')
                  temp = 2;
                if(ctemp == '3')
                  temp = 3;
                if(ctemp == '4')
                  temp = 4;
                if(ctemp == '5')
                  temp = 5;
                if(ctemp == '6')
                  temp = 6;
                if(ctemp == '7')
                  temp = 7;
                if(ctemp == '8')
                  temp = 8;
                if(ctemp == '9')
                  temp = 9;
    You could've just use
    Code:
    temp = ctemp - '0';
    I think you overcomplicated the loading process a bit, you could've just use loops like you did in saving process, in which you would extract one char from the stream at a time (char c; stream >> c; ), and since you know, you always get non-whitespace one, and you don't write anything other than digits and whitespaces, you can just put the c-'0' to your GameBoard.

  2. #32
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    ahh thats clever didn't thnk about that. so if the number was two by subtracting the default numberical value of char '0' I would get 2 left over to save as an int... thnx. A lil more outside the box than I was thinking.

  3. #33
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Quote Originally Posted by phantomotap View Post
    O_o

    The situation is exactly the same in all languages.

    You will need to know how to handle any data stream not managed by an available library if you want to work with that data.

    If a library that manages a data stream is available, you can safely ignore the details.

    If a library that manages a data stream is not available, you must learn the details.

    And you have missed the obvious case of creating your own data stream.

    Soma
    Of course you would have to know if you're dealing with low level stuff. But how often are you dealing with low level stuff?
    Compute integral from 0 to infinity of dealing with low-level stuff with respect to time, divided by the number of people using the language. Then compare that number for C and C++.
    The number for C++ should be radically lower. Thus it's not as important average.

    Quote Originally Posted by Lesshardtofind View Post
    Wow your solution is soo different than the one I've been using. I just saved data as strings and then converted the ones I wanted to be ints to ints. Is my method bad?

    Stores a current sudoku and its solution then loads a current and its solution. Converts the strings to its individual char and then into a int. I just read about fstreams and this was the solution I came up with. I'm sure its flawed if you feel like pointing out why I'd appreciate it.
    It's not bad. It just involves a lot of more work. Plus the abstraction level is lower, so if you make a change, chances are you'll break a lot of code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #34
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Thanks for help, everyone! I've made a little program, just to check, if it works:

    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    // include headers that implement a archive in simple text format
    #include <boost/archive/text_oarchive.hpp>
    #include <boost/archive/text_iarchive.hpp>
    
    /////////////////////////////////////////////////////////////
    // gps coordinate
    //
    // illustrates serialization for a simple type
    //
    class gps_position
    {
    private:
        friend class boost::serialization::access;
        // When the class Archive corresponds to an output archive, the
        // & operator is defined similar to <<.  Likewise, when the class Archive
        // is a type of input archive the & operator is defined similar to >>.
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & degrees;
            ar & minutes;
            ar & seconds;
        }
        int degrees;
        int minutes;
        float seconds;
    public:
        gps_position(){};
        gps_position(int d, int m, float s) :
            degrees(d), minutes(m), seconds(s)
        {}
    };
    
    int main() {
        // create and open a character archive for output
        std::ofstream ofs("filename.txt");
    
        // create class instance
        const gps_position g(35, 59, 24.567f);
    
        // save data to archive
        {
            boost::archive::text_oarchive oa(ofs);
            // write class instance to archive
            oa << g;
        	// archive and stream closed when destructors are called
        }
    
        // ... some time later restore the class instance to its orginal state
        gps_position newg;
        {
            // create and open an archive for input
            std::ifstream ifs("filename.txt");
            boost::archive::text_iarchive ia(ifs);
            // read class state from archive
            ia >> newg;
            // archive and stream closed when destructors are called
        }
    	cin.get();
        return 0;
    }
    Actually copied the code from the website, and edited it a little bit. I've tried cout'ing the variable too, but it didn't worked for some reason. The important thing is, that i get around 70-80 percent on how it works, after Elysia (thanks!) explained it in one of the posts how it works. 70-80 percent, because I do not understand, what is not working correctly now.

    It outputs this to the .txt file: 22 serialization::archive 7 0 0 35 59 24.566999
    Also cout is not working for some reason...

  5. #35
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    You mean
    cout << newg?
    That wouldn't work because newg does not have any overloaded << operator.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #36
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Hmmm. Okay, I don't get how it works again, lol.

    I've tried this thing:
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    // include headers that implement a archive in simple text format
    #include <boost/archive/text_oarchive.hpp>
    #include <boost/archive/text_iarchive.hpp>
    
    /////////////////////////////////////////////////////////////
    // gps coordinate
    //
    // illustrates serialization for a simple type
    //
    class gps_position
    {
    private:
        friend class boost::serialization::access;
        // When the class Archive corresponds to an output archive, the
        // & operator is defined similar to <<.  Likewise, when the class Archive
        // is a type of input archive the & operator is defined similar to >>.
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & degrees;
            ar & minutes;
            ar & seconds;
        }
        int degrees;
        int minutes;
        float seconds;
    public:
        gps_position(){};
        gps_position(int d, int m, float s) :
            degrees(d), minutes(m), seconds(s)
        {}
    };
    
    int main() {
    	string d;
        // create and open a character archive for output
        std::ofstream ofs("filename.txt");
    	int dd;
        //// create class instance
        //const gps_position g(35, 59, 24.567f);
    
        //// save data to archive
        //{
        //    boost::archive::text_oarchive oa(ofs);
        //    // write class instance to archive
        //    oa << g;
        //	// archive and stream closed when destructors are called
        //}
    	boost::archive::text_oarchive one(ofs);
    	int var = 231;
    	one << var;
    	std::ifstream ifs("filename.txt");
    	boost::archive::text_iarchive two(ifs);
    	two >> dd;
        // ... some time later restore the class instance to its orginal state
        //gps_position newg;
        //{
        //    // create and open an archive for input
        //    std::ifstream ifs("filename.txt");
        //    boost::archive::text_iarchive ia(ifs);
        //    // read class state from archive
        //    ia >> d;
        //    // archive and stream closed when destructors are called
        //}
    	cout << dd;
    	cin.get();
        return 0;
    }
    Getting an error about bad allocated memory on boost::archive::text_iarchive two(ifs); line.

  7. #37
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    filename.txt is probably still open by ofs; thus ifs fails to open it and thus boost throws an error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #38
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Hmmm. That's what i thought actually, until the memory error came up. Wasn't sure, what exactly that meant. Anyway, I've read the tutorials and reference given in the Serialization website. A quick read actually. But i was unable to find some one-line command, to close the file. It isn't possible using Serialization, is it? You must write classes, like it was done in the tutorial, right? But how to cout something then? You cannot cout the class or function... The thing, that is in main() loop and looks like a function, not sure, how to call that exactly, haven't met such before.

  9. #39
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    To close a file explicitly, call close() on the file object.
    You can serialize anything you want, from mere ints to user-defined objects to vectors!
    You can cout stuff, but this is unrelated to boost. You must overload operator << with a left-hand argument of ostream and right-hand argument of whatever yo want to print.
    You can't print a function, obviously.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #40
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Hmmm. Okay, I probably should know more about classes first, but I'm going to ask this anyway (classes was my second thing to learn deeper, after the save/load thing.):

    Do i have to work with this class, and do everything somehow in class? Can't I make variables somehow to change outside the class, using a function instead?

  11. #41
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    C++ isn't Java; you are not limited to OOP.
    You can create and modify variables outside classes. You can also create functions outside classes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #42
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Hmmm. Okay. But how am I supposed to use that neccesary class from Serialization with outside functions then? I don't even know, what this means:

    Code:
        gps_position(){};
        gps_position(int d, int m, float s) :
            degrees(d), minutes(m), seconds(s)
        {}
    It is not a function, functions doesn't have : in the end of the declaration, and functions do have variable type in the beginning. So I don't have an idea, what it is. It has no declarations, whatsoever.

  13. #43
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    They are constructors. The ":" is an initializer list.
    They are not necessary for Serialization to work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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