I have a question about pet critter program

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

53 Replies - 2388 Views - Last Post: 15 October 2019 - 07:20 PM Rate Topic: -----

#16 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15300
  • View blog
  • Posts: 61,365
  • Joined: 12-June 08

Re: I have a question about pet critter program

Posted 29 September 2019 - 11:46 AM

Real talk - are you just flailing around doing what ever hoping to stumble on some correct steps? Your code progressively showing a larger lack of understanding or planning.

Example - WHY does a 'property' to get the name variable taking in two parameters for food and happiness?
WHY did you believe that was a reasonable course of action?
You don't even use them let alone need them!

031	    std::string GetName(int food,int happiness)
032	    {
033	        return Name;
034	    }



WHY is 'get happy text' taking in a value? Once again a non used parameter that is out of place considering the object has a variable 'happiness'.
044	    std::string GetHappyText(int happiness)//pets current mood



Same here:
057	    std::string GetFoodText(int food)//how hungry is your pet

Was This Post Helpful? 1
  • +
  • -

#17 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 780
  • Joined: 15-December 14

Re: I have a question about pet critter program

Posted 30 September 2019 - 01:39 AM

Jim
I was trying to show my plan on how I was going to build the program. The example I showed wasn't complete or correct. I just wanted to show an idea I had to you guys to see if I was headed in the right direction.

line 83 and 84 were taken from a simplified program I worked on trying to solve the program I am working on right now (The pet critter program). At the time I was using them both with cout to print the current number of the loop. That way I could visually see if what I was doing to add happiness or food to the pet was working. I was trying to figure out how to use them to add and subtract from the loop.

I did get a warning when I ran it. I just wanted to show a train of thought I had to solve the program.

modi123_1

Honestly, I figured it out on a separate smaller scaled program, got it to work, more or less and then got a brain storm. I copied the scaled down program and included the things needed from the program I am working on. But in my excitement that I believed I was on the right track to solve it, I stupidly put things that were not needed when I post my question. For that I apologise to you both.

I realised after working on it this weekend that I stupidly had the variable types in the paramatres of my program. I still need to work on the function Game.



I figured out how to add happiness to the pet, but for some reason it won't let me add food. There are other things I see that I need to fix, but for the moment, thats what I am focusing on.

This is the program so far after working on it on the weekend.

#include <iostream>
#include <string>

class Critters{
private:
    std::string Name;
    int Food = 1;
    int Happiness = 1;
public:
    //constructors
    void Game();
    Critters()
    {
        Name = "";
        Happiness++;
        Food++;
    }
    Critters(std::string name,int happiness,int food)
    {
        Name = name;
        Happiness = happiness;
        Food = food;
    }
    //properties
    std::string GetName()
    {
        return Name;
    }
    std::string PrintName()
    {
        std::cout << "What do you want to call your critter?" << std::endl;
        std::getline(std::cin,Name);
        Name[0] = toupper(Name[0]);


        return Name;
    }
    std::string GetHappyText()//pets current mood
    {
        std::string happymetre;

        if(Happiness <=0)
            happymetre = "Terrible mood";
        else if(Happiness <=5)
            happymetre = "Not happy";
        else
            happymetre = "I'm in a good mood";

        return happymetre;
    }
    std::string GetFoodText()//how hungry is your pet
    {
        std::string foodmetre;
        if(Food <=0)
            foodmetre = "Your pet critter dies";
        else if(Food <=5)
            foodmetre = "I'm hungry";
        else
            foodmetre = "I'm not hungry";

        return foodmetre;
    }
};
void Critters::Game()
{
    std::string options[4] = {".Feed",".Play",".Leave pet alone",".Groom"};
    while(Food <=10)
        {
            for(int x = 0; x < 4; x++)
            {
                std::cout << x + 1<< options[x] << std::endl;
            }
            std::cout << "What would you like to do?\n" << std::endl;
            int choice;
            std::cin >> choice;
            std::cout << std::endl;
            std::cout << Name << "'s mood" << std::endl;
            std::cout << GetHappyText() << std::endl;
            std::cout << GetFoodText() << std::endl;
            std::cout << Happiness << std::endl;
            std::cout << Food << std::endl;
            if(choice == 1 || choice == 4)
            {
                Food = Food - 2;
            }
            else if(choice == 2 || choice == 4)
            {
                Happiness = Happiness - 2;
            }
            else if(choice == 3)
            {

            }
            else
                std::cout << "\nChoose one of the options.\n" << std::endl;
            Happiness++;
            Food++;
        }
}

int main()
{
    Critters critters;
    std::cout << "Your ctitter's name is " << critters.PrintName() << std::endl;
    critters.Game();
}



This post has been edited by albert003: 30 September 2019 - 01:41 AM

Was This Post Helpful? 0
  • +
  • -

#18 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7092
  • View blog
  • Posts: 24,099
  • Joined: 05-May 12

Re: I have a question about pet critter program

Posted 01 October 2019 - 08:12 PM

All you did was move everything out of main() and move it into your Critters class. Recall the single responsibility principle.

Personally, I would set things up like this:
enum Food
{
    Snack,
    Meal
};

template <int min, int max>
class IntRange
{
public:
    IntRange(int n = min);
    void Increment(int delta = 1);
    void Decrement(int delta = 1);
    int Value() const;

private:
    int m_value;
};

class ICritterNotify
{
public:
    void OnNeedAttention();
    void OnDead();
};

class Critter
{
public:
    Critter(const std::string & name, ICritterNotify notify);
    void Feed(Food food);
    void Play();
    void Discipline();
    void Update(time_t currentTime);
    int GetHunger() const;
    int GetHappiness() const;
    int GetCitizenship() const;

private:
    IntRange<0, 10> m_happiness;
    IntRange<0, 10> m_hunger;
};

class Game : public ICritterNotify
{
public:
    void Run();

private:
    std::unique_ptr<Critter> m_critter();
    time_t m_currentTime;

    void ShowCareMenu();
    void ShowFoodMenu();
    void ShowCritterStatus();
    void PromptForCare();
    void PromptForName();
    void CreateCritter();

    // INotifyCritter event handlers:
    void OnNeedAttention();
    void OnDead();
};


Was This Post Helpful? 0
  • +
  • -

#19 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 780
  • Joined: 15-December 14

Re: I have a question about pet critter program

Posted 02 October 2019 - 09:53 AM

I did it that way because I wanted to breakdown the program to its simplest form and then I put everything in main. Once I got it working, more or less I used what modi123_1 suggested. Unfortunately, I couldn't figure out how to make the program the way he suggested so thats when I got the idea to use a non member function.

Interesting, it never would have occurred to me to use various classes. But it does make sense, but I do have a few questions for you.

1. In the class Critter on line 40 and 41. what are those functions? I tried looking online to see what they were and I couldn't find anything remotely similar.

2. You suggested using a const function I understand what const is and how to use it. But my question is why do those functions need const if all they are going to do is return a statement of the pets current mood or hunger, why do those functions need a const?
Was This Post Helpful? 0
  • +
  • -

#20 jimblumberg   User is online

  • member icon

Reputation: 5765
  • View blog
  • Posts: 17,651
  • Joined: 25-December 09

Re: I have a question about pet critter program

Posted 02 October 2019 - 10:18 AM

Quote

I couldn't figure out how to make the program the way he suggested so thats when I got the idea to use a non member function.

Where did you try to use a non member function?

Quote

1. In the class Critter on line 40 and 41. what are those functions?

They are not functions they are member variables of type IntRange.

Quote

2. You suggested using a const function I understand what const is and how to use it. But my question is why do those functions need const if all they are going to do is return a statement of the pets current mood or hunger, why do those functions need a const?

So really you don't understand what const is is this content. Using const member functions insure that you don't try to modify anything in the class inside those functions. Using const functions also allows the compiler to do some optimizations that may speed up the program because it knows that the function can not alter the state of the class.


Jim

This post has been edited by jimblumberg: 02 October 2019 - 10:20 AM

Was This Post Helpful? 0
  • +
  • -

#21 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 780
  • Joined: 15-December 14

Re: I have a question about pet critter program

Posted 02 October 2019 - 12:44 PM

I used a non member function on line 64. From what I understood member functions are when functions inside of the brackets and non member functions are functions outside of the functions.


Haven't seen intrange yet. Going to review what they do and how they work.

I understand how const work, I just didn't know you could use them in functions. I thought they could only be used in variable types.
Was This Post Helpful? 0
  • +
  • -

#22 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7092
  • View blog
  • Posts: 24,099
  • Joined: 05-May 12

Re: I have a question about pet critter program

Posted 02 October 2019 - 01:10 PM

View Postalbert003, on 02 October 2019 - 03:44 PM, said:

Haven't seen intrange yet. Going to review what they do and how they work.

IntRange was a new class that I declared on lines 7-18. The intent of the class is to hold an integer value that is limited to a specific range. The reason why I declared a separate class instead of just letting the Critter class manage ensuring that the happiness and hunger scores are always within a specific range is because there would be a near duplication of code for doing maintaining those values within those ranges. So instead of littering the Critter class with code that micromanages the values, I let a separate class take care of it. All the Critter class would need to do is just increment or decrement the values.
Was This Post Helpful? 0
  • +
  • -

#23 jimblumberg   User is online

  • member icon

Reputation: 5765
  • View blog
  • Posts: 17,651
  • Joined: 25-December 09

Re: I have a question about pet critter program

Posted 02 October 2019 - 01:15 PM

Quote

I used a non member function on line 64.

No that is a Critters member function.

Quote

From what I understood member functions are when functions inside of the brackets and non member functions are functions outside of the functions.

No, you misunderstood. It is a member function that is defined inside the class on line 11. On line 64 you implemented this member function, notice the use of the scope resolution operator::(1) in the implementation. Perhaps

A non-class function is defined and implemented outside of any class (sometimes called a "free function").

Quote

Going to review what they do and how they work.

Okay where are you going to review? You do know that that class is not a standard class right? If you have questions about this class you may want to specifically ask the author of that class for more information.

Quote

I understand how const work, I just didn't know you could use them in functions. I thought they could only be used in variable types.

Like I said: So really you don't understand what const is is this content. . Many things have different meanings in C++ depending on content. Content is often key when determining the meanings.


(1) Perhaps you should look up that term to see most of the meanings of that operator?


Jim
Was This Post Helpful? 0
  • +
  • -

#24 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 780
  • Joined: 15-December 14

Re: I have a question about pet critter program

Posted 02 October 2019 - 08:13 PM

Ill be honest, I am completely lost. I went through the code line by line trying to make sense and I wrote down things I wasn't sure what they were to look up using my googlefu. I began to write a line of code and then go over the entire example and delete what I put down. Mostly because I've never written a code that big before so I'm going to be honest and admit that I have no idea where to start. I will start by asking questions on everything I either don't understand or know what that thing does.

I will look up and learn what it means to have a const next to a function.

1. I tried to look for example of IntRange online and couldn't find any examples. Looking at your example in that post my guess is that its a function that takes values range from 0 to 10 and that its similar to a max min.

2. I understand what enums are and how to use them. The way you have it at the moment Snack is = 0 and Meal = 2. I don't understand why I would need a enum in that program if IntRange already has the values of 0,10?

3. I'll be honest, I don't understand the need or function of many of the functions in the program or the need for the class IntRange.

4. Since I am overwhelmed by this program I am going to start with one part (The class Critter) and then slowly work my way to the other class. I am guessing takes inheritance from the class ICritterNotify because of the constructor on line 30.
Was This Post Helpful? 0
  • +
  • -

#25 jimblumberg   User is online

  • member icon

Reputation: 5765
  • View blog
  • Posts: 17,651
  • Joined: 25-December 09

Re: I have a question about pet critter program

Posted 02 October 2019 - 11:29 PM

Quote

I tried to look for example of IntRange online and couldn't find any examples.

I really don't understand what you expect to find online about anything to do with an IntRange. This is a class dreamed up and implemented by a single person, not really something to be Googled. Do you understand how template functions work?

Quote

I understand what enums are and how to use them. The way you have it at the moment Snack is = 0 and Meal = 2.

What? I hope that there is a typo in the above line, Meal is not 2 in the example code.

Quote

I don't understand the need or function of many of the functions in the program or the need for the class IntRange.

Do you understand that the code supplied is just an incomplete framework not really a program? None of the actual functionality has been coded, and it is even possible that there are still parts missing.

Quote

Since I am overwhelmed by this program I am going to start with one part (The class Critter) and then slowly work my way to the other class.

Okay, but start simple. Start by deciding what makes a Critter a Critter (no actual code writing needed). Then once you understand what a Critter is start implementing the desired functionality little by little.

Compile early and often, fix all warnings and errors before trying to add any additional functionality. Also test each step to insure it is doing what you expect as you write it. It is usually of little value to have a class/function/program that compiles without problems if said class/function/program is not producing the desired outcomes.

Try to keep the User Interface separate from the program logic.

As you go along remember that in C++ not everything has to be some member of some class.

Good Luck.

Jim

This post has been edited by jimblumberg: 02 October 2019 - 11:30 PM

Was This Post Helpful? 2
  • +
  • -

#26 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 780
  • Joined: 15-December 14

Re: I have a question about pet critter program

Posted 06 October 2019 - 08:26 PM

Yes that was a typo. I meant to put
Snack is = 0 and Meal = 1

I thought it was a snippet of a program. In the past I've gotten hints in the way of a very basic frame work with one or two things needed to make the code work and I was in charge of finding the missing things. I thought that was the case with his example.

I drew on a piece of paper and drew what I wanted to happen in the program in detail.

I took a step back, took a deep breath and broke down skydivers program into little pieces. I broke down the simple program I mentioned earlier and then started to work at making the program bigger. I read a few tutorials about inheritance and then used it in my program.

This is the program as I have it at the moment. Please give me an honest opinion
#include <iostream>
#include <string>
//base class
class Critter{
public:
    std::string m_name;
    int m_happiness;
    int m_food;
    Critter(std::string name = "",int happiness = 0, int food = 0)
    : m_name(name),m_happiness(happiness),m_food(food){}

    std::string GetName() const
    {
        return m_name;
    }
    int getHappiness() const
    {
        return m_happiness;
    }
    int getFood() const
    {
        return m_food;
    }
};

class Pet: public Critter{
public:
    std::string GetHappyText()
    {
        //returns how happy or sad pet is
    }
    std::string GetFoodText()
    {
        //return how full or hungry pet is
    }
    std::string PrintText()
    {
        //game loop
    }
};

int main()
{
    Pet pet;
    std::cout << "Your pets name is";
    std::cout << pet.GetName() << std::endl;

}


Was This Post Helpful? 0
  • +
  • -

#27 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15300
  • View blog
  • Posts: 61,365
  • Joined: 12-June 08

Re: I have a question about pet critter program

Posted 06 October 2019 - 08:31 PM

Why throw another class in there?
What benefit do you believe you are getting for having class pet extend class class critter?
Was This Post Helpful? 0
  • +
  • -

#28 jimblumberg   User is online

  • member icon

Reputation: 5765
  • View blog
  • Posts: 17,651
  • Joined: 25-December 09

Re: I have a question about pet critter program

Posted 06 October 2019 - 08:41 PM

Why are all of your class variables public?

Quote

I took a step back, took a deep breath and broke down skydivers program into little pieces.

Ah, okay. But what does your current code have anything to do with the code provided by others.

Quote

I broke down the simple program I mentioned earlier and then started to work at making the program bigger.

What? What I see is a much smaller program, nowhere near "larger".

Jim
Was This Post Helpful? 0
  • +
  • -

#29 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 780
  • Joined: 15-December 14

Re: I have a question about pet critter program

Posted 06 October 2019 - 08:48 PM

Back when I was learning python I remember a chapter where the author discussed inheritance using
is a
has a
I thought it would be cleaner to use the same method in this program by saying the first class 'is a' critter which contains the name,happiness,food,age (Ill add that later). The other class is the 'has a' which would be the pet class.

I also remembered working on a pygame program a while back that its better to have a class do a specific thing. So I thought I could have a class which would handle the pets details (name,happiness,food,age) and the other class would handle the game.
Was This Post Helpful? 0
  • +
  • -

#30 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15300
  • View blog
  • Posts: 61,365
  • Joined: 12-June 08

Re: I have a question about pet critter program

Posted 06 October 2019 - 08:56 PM

That's not what you have laid out. You don't need to have every class inherit from each other. You can have, as it was suggested, a 'critter' class that deals with all major interaction points with the critter object, and a 'game' class.

Though, in this case, the game class is just muddying the waters and you can simply have your 'main' do that until you get your feet under you on the whole classes and what not.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4