Thread: Help needed, simple save function

  1. #1
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76

    Help needed, simple save function

    I have been writing a, seemingly, simple game,but I run into a problem once every two tries. The problem is that once out of two I get a garbage value(for my cell_energy variable)

    main.cpp
    Code:
    #include <iostream>
    
    #include "cells.h"
    #include "shop.h"
    
    using namespace std;
    
    bool quit = 1;
    char choice = ' ';
    
    int main(){
        #include "instances_creation.h"
        player.check_existance();
        while (quit == 1){
            //Show the game menu
            cout<<"Cells:\t"<<player.get_cells()<<"\n"
                <<"Energy:\t"<<player.get_energy()<<"/"<<player.get_max_energy()<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t"
                <<"[S]plit cells\n\t"
                <<"[E]at\n\t"
                <<"S[h]op\n\t"
                <<"[Q]uit\n\t"
                <<"Answer with a capital letter\n\t";
            //get the users choice
            cin>>choice;
            //Analize the information(of the choice)
            switch (choice) {
                case 'S':
                    player.split();
                    break;
                case 'E':
                    player.set_energy(player.get_max_energy());
                    break;
                case 'H':
                    mall.show_items(player.get_jaws(),player.get_claws(),player.get_horns());
                    break;
                case 'Q':
                    cout<<"Are you sure you want to quit? Y/N\n\t";
                    cin>>choice;
                    switch (choice){
                        case 'Y':quit = 0;break;
                        case 'N':quit = 1;break;
                        default: quit = 0;break;
                    }
                    break;
                default:
                    cout<<"Unusable answer!\n\t";
                    break;
            }
        }
    //    player.save();
    }
    cells.h
    Code:
    #ifndef CELLS_H_INCLUDED
    #define CELLS_H_INCLUDED
    
    #include <string>
    using namespace std;
    
    class cells{
        public:
            void new_game();
            void check_existance();
            void balance_stats();
            /** Set **/
            void split();
            void set_group_name(string to_set);
            void set_cells(int to_set);
            void change_energy(int change);
            void set_energy(int to_set);
            void set_max_energy();
            void set_title(int cells);
            /** Get **/
            string get_group_name();
            int get_cells();
            int get_energy();
            int get_max_energy();
            int get_jaws();
            int get_claws();
            int get_horns();
            /** In/Out put **/
            string get_string();
            int get_int();
            bool get_bool();
            void display(string to_show);
            void display(int to_show);
            /** Save/Load **/
            void save();
            void load();
    
        private:
            string cell_group_name;
            string cell_group_title;
            float cell_count;
            float cell_energy;
            float cell_max_energy;
            // Needed for shop
            short jaws;
            short claws;
            short horns;
    };
    
    #endif // CELLS_H_INCLUDED
    cells.cpp
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    #include "cells.h"
    
    using namespace std;
    /** New Game **/
    void cells::new_game(){
        cout<<"\nWe will now set other variables!\n";
        set_cells(1);
        set_max_energy();
        set_energy(get_max_energy());
        cout<<"Done!! You may now start playing...";
    }
    
    void cells::check_existance(){
        string to_check;
        ifstream file( "GAME_SAVE.SSG" );
    
        cout<<"What is the name of your cell group?\n\tIf it doesn't exist, yet, a new game will be started!\n\t";
        set_group_name(get_string());
        file >> to_check;
        if(to_check == get_group_name()){load();}
        else{new_game();}
        for(int counter=0;counter>25;counter++){cout<<'\n';}
    }
    
    /** Set **/
    void cells::split(){
        if(get_energy()>=20){
            cell_count = (get_cells()*2);
            change_energy(-20);
            set_max_energy();
        }
        else{cout<<"Could not split, insuficient energy!";}
    }
    void cells::set_cells(int to_set){cell_count = to_set;}
    void cells::set_group_name(string to_set){cell_group_name = to_set;}
    void cells::change_energy(int change){cell_energy = cell_energy+change;}
    void cells::set_energy(int to_set){cell_energy = to_set;}
    void cells::set_max_energy(){cell_max_energy = get_cells()*10+30;}
    void cells::set_title(int cells){
            if(get_cells()==1){cell_group_title = "unicelular";}
            else if(get_cells()==2){cell_group_title = "bicelular";}
            else if(get_cells()>=3 && get_cells()<1000){cell_group_title = "celular grouping";}
            else if(get_cells()>=1001 && get_cells()<1000000){cell_group_title = "big celular grouping";}
            else if(get_cells()>1000001){cell_group_title = "huge celular grouping";}
    }
    
    /** Get **/
    string cells::get_group_name(){return cell_group_name;}
    int cells::get_cells(){return cell_count;}
    int cells::get_energy(){
        return cell_energy;
    }
    int cells::get_max_energy(){return cell_max_energy;}
    int cells::get_jaws(){return jaws;}
    int cells::get_claws(){return claws;}
    int cells::get_horns(){return horns;}
    
    /** In/Out put **/
    string cells::get_string(){
        string input;
        getline(cin, input, '\n');
        cout<<'\n';
        return input;
    }
    int cells::get_int(){
        int input;
        cin>>input;
        cout<<'\n';
        return input;
    }
    bool cells::get_bool(){
        bool input;
        cin>>input;
        cout<<'\n';
        return input;
    }
    void display(string to_show){cout<<to_show;}
    void display(int to_show){cout<<to_show;}
    
    /** Save/Load **/
    void cells::save(){
        ofstream save_file ("GAME_SAVE.SSG");
        save_file << get_group_name() << " " << get_cells() << " " << get_energy();
    }
    void cells::load(){
        string name;
        int cells;
        int energy;
    
        ifstream file("GAME_SAVE.SSG");
        file >> name;
        file >> cells;
        file >> energy;
        set_cells(cells);
        set_title(get_cells());
        set_max_energy();
    
        if(energy > cell_max_energy){cell_energy = cell_max_energy;}
    }
    I think the problemis in cells::get_energy(), as that is what gives me the value.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    Where is the constructor? You're not initializing cell_energy, nor any of the other variables. They will take what happens to be in memory.

  3. #3
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    I am initialising cell_energy, in the load() function.

    As for the constructor, they're in instances_creation.h, I didn't think it was important to show:
    cells player;
    shop mall;

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    Oh, yeah I missed that. If I understood your logic correctly if a cell group exists you're loading from a file else you just initialize everything in new_game.

    When does your issue happens? When you start a new game or when you load the files from a previous one?

    Another thing..
    Code:
    int main(){
        #include "instances_creation.h"
    // ...
        set_energy(get_max_energy());
    Those are bad coding practices (to say the least).

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    A suggestion, many of your "get" member functions should be declared const as they do not modify the object rather they simply return a value...
    Code:
    class cells{
        ...
        string get_group_name() const;
        int get_cells() const;
        int get_energy() const;
        int get_max_energy() const;
        int get_jaws() const;
        int get_claws() const;
        int get_horns() const;
        ...
    };
    
    ...
    
    /** Get **/
    string cells::get_group_name() const {return cell_group_name;}
    int cells::get_cells() const {return cell_count;}
    int cells::get_energy() const {
        return cell_energy;
    }
    int cells::get_max_energy() const {return cell_max_energy;}
    int cells::get_jaws() const {return jaws;}
    int cells::get_claws() const {return claws;}
    int cells::get_horns() const {return horns;}
    ...
    Some of the other functions (get_string, get_bool, get_int) seem questionable as to whether they belong directly in the cell class or not.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Quote Originally Posted by sugarfree View Post
    Oh, yeah I missed that. If I understood your logic correctly if a cell group exists you're loading from a file else you just initialize everything in new_game.

    When does your issue happens? When you start a new game or when you load the files from a previous one?

    Another thing..
    Code:
    int main(){
        #include "instances_creation.h"
    // ...
        set_energy(get_max_energy());
    Those are bad coding practices (to say the least).
    It happens when I get the value from the file and then get the value(load() then get_energy())
    If the file has a garbage value saved in it, it gives a good value. If it's the right value it loads a garbage value. STRANGE!

    Do you mean to use a function to set the base number, or putting it there.

    Quote Originally Posted by hk_mp5kpdw View Post
    A suggestion, many of your "get" member functions should be declared const as they do not modify the object rather they simply return a value...
    Code:
    class cells{
        ...
        string get_group_name() const;
        int get_cells() const;
        int get_energy() const;
        int get_max_energy() const;
        int get_jaws() const;
        int get_claws() const;
        int get_horns() const;
        ...
    };
    
    ...
    
    /** Get **/
    string cells::get_group_name() const {return cell_group_name;}
    int cells::get_cells() const {return cell_count;}
    int cells::get_energy() const {
        return cell_energy;
    }
    int cells::get_max_energy() const {return cell_max_energy;}
    int cells::get_jaws() const {return jaws;}
    int cells::get_claws() const {return claws;}
    int cells::get_horns() const {return horns;}
    ...
    Some of the other functions (get_string, get_bool, get_int) seem questionable as to whether they belong directly in the cell class or not.
    thanks, I was wondering when to use const, exactly.
    The reason I put that ther is because if I need them they're there, and I can't use ones in another class, in the original class.
    Last edited by jerimo; 07-29-2010 at 07:41 PM.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    never seen an #include inside a main function

  8. #8
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    I just felt like it would make it nicer(and easier to find)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM