I have a question about pet critter program

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

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

#31 albert003   User is offline

  • D.I.C Addict

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

Re: I have a question about pet critter program

Posted 06 October 2019 - 09:14 PM

The example I used to learn about inheritance in c++ had everything in public. He does say he will change it later in later discussions. I was going to read some more articles on inheritance tomorrow (My day off) and change whats needed. I've seen a few examples where the programmer used protected. I wanted to read more about inheritance in detail.

I took the code provided by others and made a simple program using only int main. I put everything in it (functions, variable types, etc). I was able to make it work and from there I began to build the program out. Modi123 suggested I change from struct to oop (Which I have) I have also used what everyone suggested in my oop.

This is actually the biggest program I have written in C++. I still need to add things to the program. I was going to add a class for the pets age, by having a young pet will age faster than an older pet. That was actually something I haven't figured out yet. My thoughts were to have two functions, one that ages every other loop for the young pet and then another function with a loop which will loop normally.

I was also trying to figure out whether it was better to use a enum for the different pets the player can choose from or whether it was better to use an array.
Was This Post Helpful? 0
  • +
  • -

#32 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15358
  • View blog
  • Posts: 61,577
  • Joined: 12-June 08

Re: I have a question about pet critter program

Posted 06 October 2019 - 10:08 PM

I feel obligated to stop you right there and have you assess the tree you are barking up.

Age doesn't really fit well for defining a whole new class for a critter, does it? Seems like that would be an attribute of a critter.. like health or happiness.

Tracking the attribute of an age seems reasonable do do for an integer, but you indicate you want to define descriptions of ranges of ages.

Why not have an integer for the age, and a function that returns a string of age description..

INT age;

FUNCTION string AGE_DESCRIPTION
  IF age < 6 THEN
    RETURN "young"
  IF age < 20 THEN
    RETURN "adult"
  ELSE
    RETURN "old"
END FUNCTION

Was This Post Helpful? 0
  • +
  • -

#33 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,670
  • Joined: 25-December 09

Re: I have a question about pet critter program

Posted 07 October 2019 - 08:57 AM

Quote

I feel obligated to stop you right there and have you assess the tree you are barking up.

I agree, you need to stop and back up.

You don't seem to understand the basics of classes/structures so I really question why you've all of a sudden jumped on inheritance. You need to learn to walk before you can run. Learn the basics first.

Quote

I took the code provided by others and made a simple program using only int main.

This is a big big part of your problem. You need to write your own code, not throw together a bunch of code you "found" somewhere. You're not helping yourself to learn to program by trying to adapt code, that you don't fully understand, to solve your problems.

Quote

This is actually the biggest program I have written in C++.

Okay, so show us this "big" program. You've only seemed to have posted small incomplete snippets not a large "working" program. But really would probably be better would be if you posted your design documents, ie: your class tree, the "rules" for the game you're trying to make, your original Pseudo code, etc.

Quote

Modi123 suggested I change from struct to oop (Which I have) I have also used what everyone suggested in my oop.

Please show me exactly where it was suggested to change from a struct to oop? What post exactly? By the way just changing a stuct to a class doesn't magically cause things to be oop? And you need to remember in C++ not everything must be part of a class and oop is not the magic bullet that will make everything work, sometimes procedural methods are better than trying to force the program into one single paradigm such as oop.

Quote

I was also trying to figure out whether it was better to use a enum for the different pets the player can choose from or whether it was better to use an array.

I suggest for now that you forget that you ever heard of an enum or array (Better yet std::vector).


Again show us the "rules".
And I really recommend you start with one and only one critter and player until you get the basic logic for your game working. Once you have a basic game working you can start adding complexity. And by the way I recommend you consider implementing the game logic as a series of non-class functions.

I would still recommend having a simple Critter class (no need for inheritance yet) and possibly a simple Player class. Keep things simple, IE: start with the most simple Critter, no age effects (this will come later) one major attribute (maybe a "hungry" attribute) anything else can come later.

But really most important, stop trying to use code you don't 100% understand, and even if you do understand the code realize that that foreign code may not be "right" for your program. You really need to use your own knowledge and write your own code and learn to use you're own "logic", then perhaps you may be able to actually get somewhere.

Lastly other than looking up documentation for standard functions and classes I really recommend you stop trying to rely on the internet to provide solutions for your problems. You need to learn to work through problems and provide your own solutions. And stop trying to throw code at a problem, step back and "think before you act" before you even start writing a single line of code.

Jim



Jim
Was This Post Helpful? 0
  • +
  • -

#34 albert003   User is offline

  • D.I.C Addict

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

Re: I have a question about pet critter program

Posted 07 October 2019 - 10:03 AM

modi123
Sorry I missed your previous post. I honestly thought just using one class was a bad idea. I know exactly how I would the program if I was using python just using a single class and a bunch of functions.

Jim
Good point. I'll read more about inheritance.

What I meant was that when I got all of the advice from everyone I simplified the program, put everything into the int main function and then made it work.

The big problem was that when I fed the pet it wouldn't subtract from the total. So the loop will start at zero and then each time it loops it adds one. When the pet is fed it subtracts from the loop so instead of the pets hunger level being a 5 it will be 4. I sort of figured out how to do it and it works but it takes a loop for it to subtract. Which is where I am trying to figure out how to fix.

Back in post 9 modi123 suggested I use oop instead of using a struct.


Good point I'll start with just one critter with the one attribute and then work my way to add other things.
Was This Post Helpful? 0
  • +
  • -

#35 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,670
  • Joined: 25-December 09

Re: I have a question about pet critter program

Posted 07 October 2019 - 10:15 AM

Quote

Jim
Good point. I'll read more about inheritance.

What point are you reading? My suggestion is that you stop playing with advanced features like inheritance until after you understand the language basics.

Quote

Back in post 9 modi123 suggested I use oop instead of using a struct.

NO, not what he said:
From post #9.

Quote

I highly recommend rereading on OOP.


Using OOP doesn't necessarily mean using inheritance and putting everything into some random class.

Quote

Good point I'll start with just one critter with the one attribute and then work my way to add other things.

Okay, but don't forget to post your design graphs, and especially the rules for the game you're trying to create.

And this is very important: NOT EVERYTHING MUST PART OF SOME CLASS!

Jim
Was This Post Helpful? 0
  • +
  • -

#36 albert003   User is offline

  • D.I.C Addict

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

Re: I have a question about pet critter program

Posted 07 October 2019 - 10:43 AM

Good point.

I misread that post. I apologise.


What do you mean by not everything must be part of some class?
Was This Post Helpful? 0
  • +
  • -

#37 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,670
  • Joined: 25-December 09

Re: I have a question about pet critter program

Posted 07 October 2019 - 10:55 AM

Exactly what I said. In C++ you can have functions that are not part of any class. For example (a complete C++ program):

#include <iostream>

void print_value(int some_value); // A global ("free") function not part of any class.

int main()
{
   // This function can never be part of any class or inside any namespace (other that the "global" namespace), it must be a "free" function outside of any other function or class and must be in the global namespace.

   print_value(100);
}

// A "free" function created in the global namespace, not part of any class.
void print_value(int some_value)
{
   std::cout << some_value << '\n';
}


By the the way this issue is part of the reason why I said you should learn the basics before trying to learn more advanced features. This is a very very basic part of the language.

Jim
Was This Post Helpful? 0
  • +
  • -

#38 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15358
  • View blog
  • Posts: 61,577
  • Joined: 12-June 08

Re: I have a question about pet critter program

Posted 07 October 2019 - 11:00 AM

Quote

What do you mean by not everything must be part of some class?

I am taking that as your miss-diagnosed spots to put things. Like wanting to make a whole new class for different 'critter' ages, or pets inheriting from critter.

Diagnosing what is an attribute or what needs a full fledged class is ground work OOP information.
Was This Post Helpful? 0
  • +
  • -

#39 albert003   User is offline

  • D.I.C Addict

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

Re: I have a question about pet critter program

Posted 08 October 2019 - 10:52 AM

I went back to basics and I am working on that program I mentioned earlier where everything was done in the main function. I have expanded it adding getname to it. My plan was once I figure that part out I will expand it further.

I have a question for you guys, when I feed the pet it will first subtract from the loop and then when I feed the pet a second time it adds back to the pets hunger level. Could I get a hint what I can do to remedy that bug?

This is what it does when I run the program

What do you want to name your pet?
scooby do
Your pets name is Scooby do
1.Feed
2.leave alone
3.Exit
Choose an option:
2
I am 10
1.Feed
2.leave alone
3.Exit
Choose an option:
2
I am 9
1.Feed
2.leave alone
3.Exit
Choose an option:
2
I am 8
1.Feed
2.leave alone
3.Exit
Choose an option:
2
I am 7
1.Feed
2.leave alone
3.Exit
Choose an option:
1
I am 6
1.Feed
2.leave alone
3.Exit
Choose an option:
1
I am 7
1.Feed
2.leave alone
3.Exit
Choose an option:


This is the program
#include <iostream>
#include <string>

std::string GetName(std::string name)
{
    std::string setname;
    std::getline(std::cin,setname);
    setname[0] = toupper(setname[0]);

    return setname;
}
int main()
{
    std::string arr[3] = {".Feed",".leave alone",".Exit"};
    std::cout << "What do you want to name your pet?" << std::endl;
    std::string name;
    std::string Nam = GetName(name);
    std::cout << "Your pets name is " << Nam << std::endl;
    int food = 10;
    while(food>0)
    {
        for(int x = 0; x< 3;x++)
        {
            std::cout << x + 1 << x[arr] << std::endl;
        }
        int choice;
        std::cout << "Choose an option:" << std::endl;
        std::cin >> choice;
        std::cout << "I am " << food << std::endl;//I have this here till I put the GetFoodText there
        if(choice == 1)
        {
            food +=2;
        }
        else if(choice == 3)
        {
            break;
        }
        food--;
    }
}


Was This Post Helpful? 0
  • +
  • -

#40 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15358
  • View blog
  • Posts: 61,577
  • Joined: 12-June 08

Re: I have a question about pet critter program

Posted 08 October 2019 - 11:15 AM

Quote

when I feed the pet it will first subtract from the loop and then when I feed the pet a second time it adds back to the pets hunger level.

Perhaps change where you are printing the contents out. Before you take action or after all actions are done.

		std::cout << "I am " << food << std::endl;//I have this here till I put the GetFoodText there


I am food? That doesn't make sense.
Was This Post Helpful? 0
  • +
  • -

#41 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,670
  • Joined: 25-December 09

Re: I have a question about pet critter program

Posted 08 October 2019 - 11:32 AM

Also this: std::cout << x + 1 << x[arr] << std::endl; should really be: std::cout << x + 1 << arr[x] << std::endl;

Jim
Was This Post Helpful? 0
  • +
  • -

#42 albert003   User is offline

  • D.I.C Addict

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

Re: I have a question about pet critter program

Posted 10 October 2019 - 11:30 AM

modi123
I put that there as a way to make sure when I was feeding my pet it was actually doing what I wanted it to do and watch as the numbers add and subtract making the pet less hungry. I will put in once I am sure the program works the way I want this line of code...

        std::string foodtxt = GetFoodText(food);
        std::cout << "I am " << foodtxt << std::endl;


Jim
Thanks.

I have a new question for you guys. I want to fix a bug ahead of time where the user will try to over feed their pet so instead of staying at 10 (The maximum) they would continue to feed their pet and it would accumulate to say for example to 20. I want it to stop at 10, but yet subtract each time the game loops and adds when the user feeds their pet. I worked on a mini program to solve the logic and I was able to figure it out but when I try to use it on my main program, where the loop would count down from 10, stop at 5 and stay on that number. When I tried to use that logic on the pet program, it won't subtract from 10 each loop. I've tried everything to make it work and I am out of ideas. This is what happens when the program runs..
What do you want to name your pet?
snoopy
Your pets name is Snoopy
1.Feed
2.leave alone
3.Exit
Choose an option:
2
10
I am I'm not hungry
1.Feed
2.leave alone
3.Exit
Choose an option:
2
10
I am I'm not hungry
1.Feed
2.leave alone
3.Exit
Choose an option:
2
10
I am I'm not hungry
1.Feed
2.leave alone
3.Exit
Choose an option:
2
10
I am I'm not hungry
1.Feed
2.leave alone
3.Exit
Choose an option:

Before anyone mentions it, yes I realise I have the line of code food as well as the line of code taking information from the string function GetFoodText. I put both there to visually make sure the counter was subtracting each loop and adding when feeding the pet as well as making sure it gave the correct text.

This is the program..

#include <iostream>
#include <string>




std::string GetName(std::string name)
{
    std::string setname;
    std::getline(std::cin,setname);
    setname[0] = toupper(setname[0]);

    return setname;
}
std::string GetFoodText(int food)
{
    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;
}
int main()
{
    std::string arr[3] = {".Feed",".leave alone",".Exit"};
    std::cout << "What do you want to name your pet?" << std::endl;
    std::string name;
    std::string Nam = GetName(name);
    std::cout << "Your pets name is " << Nam << std::endl;
    int food = 10;
    while(food>0)
    {
        for(int x = 0; x< 3;x++)
        {
            std::cout << x + 1 << arr[x] << std::endl;
        }
        int choice;
        std::cout << "Choose an option:" << std::endl;
        std::cin >> choice;

        if(choice == 1)
        {
            food +=1;
        }
        else if(choice == 3)
        {
            break;
        }
        else if(food == 9)
            food = 10;
        std::string foodtxt = GetFoodText(food);
        std::cout << food << std::endl;
        std::cout << "I am " << foodtxt << std::endl;
        food--;
    }
}


Was This Post Helpful? 0
  • +
  • -

#43 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15358
  • View blog
  • Posts: 61,577
  • Joined: 12-June 08

Re: I have a question about pet critter program

Posted 10 October 2019 - 11:40 AM

Quote

I put that there as a way to make sure when I was feeding my pet it was actually doing what I wanted it to do

This is wrong. Printing out information before the last of the actions happen gives you incorrect data, and a skewed view of what is happening. _EXACTLY_ as you are seeing happening now.

Fix that.

For the love of Peter.. why.. WHY is 'food' tacked on to a choice set of if and if else? I mentioned this was a bad idea posts ago, and it is frustrating to mention it again. Separation of duties and reasoning.

45        if(choice == 1)

49        else if(choice == 3)

53        else if(food == 9)


Was This Post Helpful? 0
  • +
  • -

#44 albert003   User is offline

  • D.I.C Addict

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

Re: I have a question about pet critter program

Posted 10 October 2019 - 12:04 PM

I honestly thought that's what you meant when you suggested I move that to before or after all of the actions.

I got that idea to put food as a choice while working on the mini program. I made it count down from 10, stop at 5 and not increment. I've been trying to figure out a way to use similar logic to my current program. The post I made was my best effort trying to figure out how to make it work. I couldn't think of any other way to do it.

This is the mini program I worked on trying to solve how to make it stay on a certain number and not decrement from the loop. I didn't have any if statements or a cin because at the time I was trying to figure out a way to make the loop stop at a certain number and stay on that number.

int main()
{
    int food = 10;
    while(food > 0)
    {
        std::cout << food << std::endl;
        if (food == 5)
            food = 6;
        food --;
    }
}

Was This Post Helpful? 0
  • +
  • -

#45 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15358
  • View blog
  • Posts: 61,577
  • Joined: 12-June 08

Re: I have a question about pet critter program

Posted 10 October 2019 - 12:08 PM

This is where a function or method would come in handy. You call said method to increment, but inside the method is an IF statement that only increments if the value is less than what ever max.
Was This Post Helpful? 0
  • +
  • -

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