I have a question about pet critter program

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4

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

#46 albert003   User is offline

  • D.I.C Addict

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

Re: I have a question about pet critter program

Posted 12 October 2019 - 05:29 PM

Thank you, thank you, thank you, thank you!!!! I figured it out and the answer to use a function was right under my nose.


I have a new question for you and I honestly can't figure out what I am doing wrong. I tried everything I could think of to fix it but I just can't figure it out. When I feed my pet it will go to 11 and then when I try to feed it again it will be where its suppose to be at 10. Why?

This is a copy of what happens when I run the program.

What do you want to name your pet?
snoopy
Your pets name is Snoopy
1.Feed
2.leave alone
3.Exit
10
I am I'm not hungry
Choose an option:
2
1.Feed
2.leave alone
3.Exit
9
I am I'm not hungry
Choose an option:
2
1.Feed
2.leave alone
3.Exit
8
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
9
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
10
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
11
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
10
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
11
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
10
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
11
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
10
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
11
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
10
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
11
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
10
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
11
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
10
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
11
I am I'm not hungry
Choose an option:
1
1.Feed
2.leave alone
3.Exit
10
I am I'm not hungry
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;
}
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;
}
void AddFood(int& food)
{
    if(food < 11)
        food +=2;
}
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;
        }
        std::string foodtxt = GetFoodText(food);
        std::cout << food << std::endl;
        std::cout << "I am " << foodtxt << std::endl;
        int choice;
        std::cout << "Choose an option:" << std::endl;
        std::cin >> choice;
        if(choice == 1)
        {
            //food +=2;
            AddFood(food);
        }
        else if(choice == 3)
        {
            break;
        }
        food--;
    }
}

Was This Post Helpful? 0
  • +
  • -

#47 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15355
  • View blog
  • Posts: 61,552
  • Joined: 12-June 08

Re: I have a question about pet critter program

Posted 12 October 2019 - 05:41 PM

You are adding two.. what do you believe would happen by just checking x alone?
Was This Post Helpful? 0
  • +
  • -

#48 albert003   User is offline

  • D.I.C Addict

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

Re: I have a question about pet critter program

Posted 12 October 2019 - 06:23 PM

It wont increase the full level of the pet.

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


#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;
}
void AddFood(int& food)
{
    if(food < 11)
        food +=1;
}
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;
        }
        std::string foodtxt = GetFoodText(food);
        std::cout << food << std::endl;
        std::cout << "I am " << foodtxt << std::endl;
        int choice;
        std::cout << "Choose an option:" << std::endl;
        std::cin >> choice;
        if(choice == 1)
        {
            //food +=2;
            AddFood(food);
        }
        else if(choice == 3)
        {
            break;
        }
        food--;
    }
}


Was This Post Helpful? 0
  • +
  • -

#49 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15355
  • View blog
  • Posts: 61,552
  • Joined: 12-June 08

Re: I have a question about pet critter program

Posted 12 October 2019 - 06:29 PM

I do not understand your response.
Was This Post Helpful? 0
  • +
  • -

#50 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,220
  • Joined: 05-May 12

Re: I have a question about pet critter program

Posted 12 October 2019 - 06:44 PM

What our OP is failing to explain is that each cycle of the main menu reduces the amount of food by one. If AddFood() only adds 1, then it will have no effect since the bottom of his loop will just subtract 1.

I don't Understand why the OP feels compelled to always subtract 1 food for each cycle. Why not only subtract 1 food if the users picks any other option than feed the critter? Perhaps a side effect of his background of working with hardware that runs on a clock?
Was This Post Helpful? 0
  • +
  • -

#51 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,220
  • Joined: 05-May 12

Re: I have a question about pet critter program

Posted 12 October 2019 - 06:49 PM

In my opinion, this project, just like our OP's previous project, is deserving of drawing out a flowchart to map out the program logic first. Clearly designing at the keyboard is not working for our OP.
Was This Post Helpful? 0
  • +
  • -

#52 albert003   User is offline

  • D.I.C Addict

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

Re: I have a question about pet critter program

Posted 12 October 2019 - 10:12 PM

Skydiver understood what I was trying to accomplish with this program.

I actually figured out the bug after I reviewed my notes on loops.

skydiver

That was actually one of the original ideas I had for the program. I thought I could use clock() and use real time for the virtual pet. I got the free version of ms visual studio and was going to make it into an app for my nieces cell phone. But I think thats beyond my abilities at the moment so I was going to make it into a exe file and have it run in the back ground on my nieces laptop. I was thinking I could use windows task scheduler to have it run when she uses her laptop.

I have the flow chart on a piece of paper. I will take a picture of it and post it here when I get home from work.
Was This Post Helpful? 0
  • +
  • -

#53 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 981
  • View blog
  • Posts: 3,870
  • Joined: 13-June 14

Re: I have a question about pet critter program

Posted 13 October 2019 - 08:09 AM

View Postalbert003, on 13 October 2019 - 02:23 AM, said:

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

    return setname;
}



Why does this function take a parameter?

This post has been edited by ndc85430: 13 October 2019 - 08:10 AM

Was This Post Helpful? 0
  • +
  • -

#54 albert003   User is offline

  • D.I.C Addict

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

Re: I have a question about pet critter program

Posted 15 October 2019 - 07:20 PM

ndc85430 It was an oversight.

Skydiver
You actually made a valid point. I wanted to try and make it as realistic as possible. Just like a human or animal gets hungrier after each moment, I thought I could do the same with the while loop. Instead I'll use the clock feature to send a message each, say 10min to give a message to the user. The message will be from an array which will say "I'm bored","I'm hungry","Clean the litter box","Talk to me", etc.

I have a few questions and I hope I'm not over my head.

I know I can use the function clock() to get real time. Is it possible to use that function and make it work if I put it in as an app for my nieces cell?

I found an installed visual studios 19 so I could make the app (Since code blocks doesn't have the ability to do that)for my niece. All of the tutorials I found are for c#. My question is, whether its a bad idea to try and make this app in C++ or should I make it in C#?
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4