Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2004
    Location
    Canada
    Posts
    1,234

    Increment integer, then reset it // C++

    I'm currently teaching myself the fundamentals of C++ programming from a book and am building a program to use all that I've read up to and including the classes chapter. It's sort of a tamagotchi-like program where you're given a cat and have to interact with it. Fairly basic. I have a reputation system in place to determine the end of the game (if the cat doesn't like you because you pet it too much or didn't feed it, it will run away). I have a variable MAXPET which increments as you interact. When it reaches 10, it calls to Scratch() which increments from there on the variable hatePLUS. What i need it do to is reset after a timeframe or upon the selection of a new interaction, but not quite sure how to do that yet. Any help would be appreciated.


    Source main.cpp:

    Code:
    #include <iostream>
    #include "cat.h"
    using namespace std;
    
    void MenuFunction(int &MAXPET, int &lovePLUS, int &hatePLUS);
    
    Cat Frisky;
    
    int main()
    {
        int itsAGE;
        int lovePLUS = 0;
        int hatePLUS = 0;
        int MAXPET;
    
        cout << "Meet Frisky. He is " << Frisky.GetAge() << " years old." << endl << endl;
        Frisky.Purr(++lovePLUS);
        Frisky.SetLOVEHATE(lovePLUS, hatePLUS);
    
        cin.get();
    
        MenuFunction(MAXPET, lovePLUS, hatePLUS);
    
    
        return 0;
    }
    
    void MenuFunction(int &MAXPET, int &lovePLUS, int &hatePLUS) {
        int choice;
    
        for (choice =0; choice < 4; choice = 0) {
    
        cout << "MAXPET: " << MAXPET << " lovePLUS: " << lovePLUS << " hatePLUS: " << hatePLUS << endl << endl;
    
        cout << "***** Interact with Frisky! *****" << endl << endl;
        cout << "[1] - Pet Frisky!\n[2] - Play with Frisky!\n[3] - Feed Frisky!" << endl << endl;
        cin  >> choice;
    
            switch(choice) {
                case 1:
                Frisky.Pet(++MAXPET, lovePLUS, hatePLUS);
                break;
            }
        }
    }
    Source cat.h:

    Code:
    #ifndef CAT_H_INCLUDED
    #define CAT_H_INCLUDED
    using namespace std;
    
    class Cat {
    public:
    // Constructor and Destructor
         Cat();
        ~Cat() {}
    // Public Functions
        void Meow()const { cout << "Meow!" << endl; }
        void Play();
        void Pet(int &MAXPET, int &lovePLUS, int &hatePLUS);
        int  Scratch(int &hatePLUS);
        void Purr(int &lovePLUS);
        void Eat();
        void Drink();
    // Accessor Functions
        int  GetAge()const                             { return AGE; }
        void SetAge(int itsAGE)                        { AGE = itsAGE; }
        void SetLOVEHATE(int &lovePLUS, int &hatePLUS)  { LOVE = lovePLUS; HATE = hatePLUS; };
        void SetREPUTATION()                           { REPUTATION = LOVE - HATE + 5; }
        int  GetREPUTATION()                           { return REPUTATION; }
        int  GetLOVE()                                 { return LOVE; }
        int  GetHATE()                                 { return HATE; }
    
    private:
    // Member variables (private)
        int AGE;
        int LOVE;
        int HATE;
        int REPUTATION;
        int MAXPET;
    };
    
    // Constructor definition
    Cat::Cat() {
        AGE  = 1;
    }
    
    void Cat::Purr(int &lovePLUS) {
    
            cout << "Frisky purrs!" << endl << endl;
    }
    
    void Cat::Pet(int &MAXPET, int &lovePLUS, int &hatePLUS) {
        if (MAXPET < 10) {
            Cat::Purr(++lovePLUS);
        }
        else
            Cat::Scratch(++hatePLUS);
    }
    
    int Cat::Scratch(int &hatePLUS) {
        cout << "Frisky is mad! You've been scratched." << endl <<endl;
        return hatePLUS;
    }
    
    #endif // CAT_H_INCLUDED
    Intel Core i3 2120 running @ 3.30 Ghz && 8GB Corsair Vengeance RAM @ 1333Mhz && XFX Radeon HD 6850 @ 1920x1080 && 600w PSU && 2TB In-Case Storage (Boot drive: OCZ Vertex 4 128GB SSD) && ASUS P8H77-V LE Mobo && ThermalTake Tsunami Dream Case && Black Widow Mechanical Keyboad && Thermaltake Black Gaming Mouse

  2. #2
    Join Date
    Sep 2005
    Posts
    158
    I dont understand, do you want to reset MAXPET after it scratches?

    Also just throwing this out there, might simplify things, but you could just use a single variable called PetsLove, if you do something good in increment it, if you do something bad it decrements, if its a certain -value, it leaves.

    Doing it this way would actually add more complexity to the cat cause in a situation it would normally leave, it forgives you due to its current love for you.
    Last edited by AlysiumX; 01-19-2012 at 12:01 PM.

  3. #3
    Join Date
    Jan 2004
    Location
    Canada
    Posts
    1,234
    I want to reset MAXPET after a set time passes by or upon selection of a new interaction (Play, Feed, etc). And thanks for the suggestion, I appreciate any help outside of the help I ask for since I'm surely doing this in an inefficient manner here and there.
    Intel Core i3 2120 running @ 3.30 Ghz && 8GB Corsair Vengeance RAM @ 1333Mhz && XFX Radeon HD 6850 @ 1920x1080 && 600w PSU && 2TB In-Case Storage (Boot drive: OCZ Vertex 4 128GB SSD) && ASUS P8H77-V LE Mobo && ThermalTake Tsunami Dream Case && Black Widow Mechanical Keyboad && Thermaltake Black Gaming Mouse

  4. #4
    Join Date
    Sep 2005
    Posts
    158
    Quote Originally Posted by b1nary View Post
    I want to reset MAXPET after a set time passes by or upon selection of a new interaction (Play, Feed, etc). And thanks for the suggestion, I appreciate any help outside of the help I ask for since I'm surely doing this in an inefficient manner here and there.
    If you talking set time, you have to start accounting for time which might add some complexities you don't want, but on the flip side, you then can bind other things to time, for instance the cats age. Here an article i found dealing with time in C++.

    http://www.cplusplus.com/reference/clibrary/ctime/
    http://www.cplusplus.com/reference/clibrary/ctime/time/

    On the other side of it. You can do a couple things first is the simplest.

    Add a ResetMAXPET() function which check if the MAXPET variable is 10, if it is make it 0 and add that toowhich ever action you would like.

    A little more complicated would be to track actions which is creating an int actionCount variable and incrementing it upon doing an action. Finally you add that ResetMAXPET() function back in and instead of checking if MAXPET is 10, you say if actionCount is 10 reset MAXPET to 0.

    From there you can then hook things into the actionCount variable. It really depends how you want it to work, I don't know the specifics of how you wanted the game to progress, so all I can really do is just give you options really.

    Sorry if I am being confusing.
    Last edited by AlysiumX; 01-19-2012 at 12:34 PM.

  5. #5
    Join Date
    Jan 2004
    Location
    Canada
    Posts
    1,234
    Well basically here's what I would prefer:

    You're given three interactions. Pet, Play and Feed.

    MAXPET (or actionCount) is incremented each time one of the above interactions is selected, but ONLY so long as it is selected in repetition (feeding the cat 10 times in a row, for example).

    Each interaction increments MAXPET to a different threshold via individual if (MAXPET > x) statements that would call to Scratch(). So long as you keep repeating the interaction, MAXPET/actionCount ( > x) continues to call to Scratch(++actionCount).

    What I would like is for the program to reset actionCount if the user waits X amount of seconds or selects a different interaction.
    Intel Core i3 2120 running @ 3.30 Ghz && 8GB Corsair Vengeance RAM @ 1333Mhz && XFX Radeon HD 6850 @ 1920x1080 && 600w PSU && 2TB In-Case Storage (Boot drive: OCZ Vertex 4 128GB SSD) && ASUS P8H77-V LE Mobo && ThermalTake Tsunami Dream Case && Black Widow Mechanical Keyboad && Thermaltake Black Gaming Mouse

  6. #6
    Join Date
    Jul 2006
    Location
    San Diego, CA, USA
    Posts
    897
    This doesn't address any of your questions but I was curious why all of the class definitions were inside the header file bounded by the inclusion guard (#ifndef/#define/#endif)? Headers do not get compiled into object code like .cpp files do. The methods do get compiled but inside main.cpp where they are #included.

    As a general rule avoid putting code that gets compiled in a header. Every time that header changes, all other files that include it have to get recompiled.

    The exception here is template classes and the like. Their declarations and definitions must all be in the header.
    Code Fearlessly!

  7. #7
    Join Date
    Oct 2005
    Location
    Seattle, WA
    Posts
    501
    I think I get what you are after. I would start by using an enum to define your set of actions clearly. That way, no matter what part of the code you are looking at you understand the meaning. A good place to do this would be just before your Cat class definition in the cat.h header.

    Code:
    #ifndef CAT_H_INCLUDED
    #define CAT_H_INCLUDED
    using namespace std;
    
    
    enum eCatInteractions {
        ACTION_NONE,
        ACTION_PET,
        ACTION_PLAY,
        ACTION_FEED,
        ACTION_MAX
    };
    
    class Cat { ...
    Defining it here makes sense because it is relevant to the class, however you want it to be accessible to stuff outside the class. Adding ACTION_NONE and ACTION_MAX will give you something to test against for out-of-bounds choices.
    Code:
    void MenuFunction(int &MAXPET, int &lovePLUS, int &hatePLUS) {
        int choice;
    
    
        for (choice = ACTION_NONE; choice < ACTION_MAX; choice = ACTION_NONE) {
    
    
        cout << "MAXPET: " << MAXPET << " lovePLUS: " << lovePLUS << " hatePLUS: " << hatePLUS << endl << endl;
    
    
        cout << "***** Interact with Frisky! *****" << endl << endl;
        cout << "[1] - Pet Frisky!\n[2] - Play with Frisky!\n[3] - Feed Frisky!" << endl << endl;
        cin  >> choice;
    
    
            switch(choice) {
                case ACTION_PET:
                Frisky.Pet(++MAXPET, lovePLUS, hatePLUS);
                break;
    
                case ACTION_PLAY:
                ....
                break;
    
                case ACTION_FEED:
                ...
                break;
            }
        }
    }
    Once that is in place, I would add a variable to your Cat class to remember the last action taken. Something like:
    Code:
    class Cat {
    ...
    private:
       int lastAction;
    ...
    This can be initialized in the constructor to ACTION_NONE. Then when an action is executed you can update the last action variable to that action. Then inside Cat::Pet() you can check to see that the last action was. If it was ACTION_PET then increment MAXPET and continue on with your code. If it was something else then reset MAXPET to zero.

  8. #8
    Join Date
    Jan 2004
    Location
    Canada
    Posts
    1,234
    Quote Originally Posted by smjones View Post
    This doesn't address any of your questions but I was curious why all of the class definitions were inside the header file bounded by the inclusion guard (#ifndef/#define/#endif)? Headers do not get compiled into object code like .cpp files do. The methods do get compiled but inside main.cpp where they are #included.

    As a general rule avoid putting code that gets compiled in a header. Every time that header changes, all other files that include it have to get recompiled.

    The exception here is template classes and the like. Their declarations and definitions must all be in the header.
    Code::Blocks did this automatically. Thanks for the heads up, I didn't know that regardless!

    And Aholio thanks, that should work perfectly!
    Intel Core i3 2120 running @ 3.30 Ghz && 8GB Corsair Vengeance RAM @ 1333Mhz && XFX Radeon HD 6850 @ 1920x1080 && 600w PSU && 2TB In-Case Storage (Boot drive: OCZ Vertex 4 128GB SSD) && ASUS P8H77-V LE Mobo && ThermalTake Tsunami Dream Case && Black Widow Mechanical Keyboad && Thermaltake Black Gaming Mouse

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •