Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8446247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:52:46+00:00 2026-06-10T09:52:46+00:00

-Let me give some background first- My assignment is to take a given senario

  • 0

-Let me give some background first- My assignment is to take a given senario (my dog buddy sees a frog in the backyard and if he is hungry he eats it, if not he will play with it, if he’s already eaten two he will let it go. If he sees a cat or a squirrel he will bark at it, if another dog he chases it, if a coyote he will cry for help, any other animal he will watch it). Then we are to have it count the number of animals in a given night and record it into another file along with Buddy’s reactions to said animals. A person is to be able to enter a date into the recorded file and pull up the animals and interactions for said date.-

Here is the code as I have it currently:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

class animal{
public:
   animal();
   ~animal();
   virtual string interactWithBuddy()//all derived classes use this
   {
        return "Buddy ";
   } 

};
animal::animal()
{
}
class frog: public animal
{

 public:
         string interactWithBuddy()
         {
              return "Buddy \n";
         }
         static int ID()
         {
             return 1;//ID assigned to frog for randomization purposes
         }         
};
class dog: public animal
{                
   public:
         string interactWithBuddy()
         {
              return "Buddy chased the dog\n";
         }
         static int ID()
         {
             return 2;//ID assigned to dog for randomization purposes
         }

};
class cat: public animal
{
  public:
         string interactWithBuddy()
         {
              return "Buddy barked at the cat \n";
         }
         static int ID()
         {
             return 3;//ID assigned to cat for randomization purposes
         }
};
class coyote: public animal
{
  public:
         string interactWithBuddy()
         {
              return "Buddy cried for help when he seen the coyote \n";
         }
         static int ID()
         {
             return 4;//ID assigned to coyote for randomization purposes
         }
};
class squirrel: public animal
{
  public:
         string interactWithBuddy()
         {
                return "Buddy barked at the squirrel \n";
         } 
         static int ID()
         {
             return 5;//ID assigned to squirrel for randomization purposes
         }
};
class otherAnimal: public animal
{
  public:
         string interactWithBuddy()
         {
                return "Buddy watched the animal \n";
         } 
         static int ID()
         {
             return 6; //ID assigned to otherAnimal for randomization purposes
         }
};
int main ()
{
srand(time(0)); //intializes the random seed
int number;
animal * a; // pointer to animal 
  std::cout << (rand() % 6 + 1) <<std::endl;  //return random number between 1-6

 // loop to assign the random number output a proper animal ID
  if (number == frog::ID()) 
  {
        a = new frog;
        a->interactWithBuddy();

  }
  else if (number == dog::ID())
  {
       a = new dog;
       a->interactWithBuddy();
  }
  else if (number == cat::ID())
  {
       a = new cat;
       a->interactWithBuddy();
  }
  else if (number == coyote::ID())
  {
       a = new coyote;
       a->interactWithBuddy();
  }
  else if (number == squirrel::ID())
  {
     a = new squirrel;
     a->interactWithBuddy();
  }
  else if (number == otherAnimal::ID()) 
  {
      a = new otherAnimal;
      a->interactWithBuddy();
  }

return 0;
}

Compiles without errors but when I code check it for the output I get an error that reads “Line 100: warning: ‘number’ is used uninitialized in this function”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:52:47+00:00Added an answer on June 10, 2026 at 9:52 am

    For your first question, this may take a few tries.

    First, suppose you just wanted to choose an animal randomly:

    srand(time(0)); //initializes the random seed
    int number = rand() % 6 + 1;
    animal *a;
    if(number == 1)
    {
      a = new frog;
    }
    if(number == 2)
    {
      a = new dog;
    }
    ...
    
    a->interactWithBuddy();
    delete(a); // Don't forget to delete what you create with "new".
    

    That works, but the ID numbers are “hard-coded” here, it doesn’t use the ID() function you wrote. If you want to use ID(), you could have one of each animal and see which one matches number:

    frog Kermit;
    dog Ralph;
    cat Felix;
    coyote Loki;
    squirrel Sparticus;
    
    if(number == kermit.ID())
    {
      kermit.interactWithBuddy();
    }
    if(number == Ralph.ID())
    {
      Ralph.interactWithBuddy();
    }
    ...
    

    You have to have each animal beforehand, because you can’t ask the animal for ID() until the animal exists. But there is a way to code ID() so that you can ask for the ID of a type of animal before you have one of that animal, by using “static“:

    class animal{
    public:
      animal();
      ~animal();
      void interactWithBuddy();
    };
    
    class frog: public animal
    {
    public:
      ...
      static int ID()
      {
        return 1;
      }
    };
    
    ...
    
    int main()
    {
      ...
      if(number == frog::ID())
      {
        a = new frog;
      }
      ...
    }
    

    This also solves your second problem because you no longer have the problematic OD() in animal.

    Is that sufficient? There are other possibilities.

    EDIT:

    You forgot int number = rand() % 6 + 1;.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me give you some background on my scenario. I got a multi language
Might be complicated, so let me give you some background: I have a spreadsheet
Let me first give some example table table which will make my question easier
I've created a bunch of projects in Eclipse 3.7.2. Let's give some of them
Ok, the question might not be crystal clear. Let me give some details: Let's
(Let me give you some context) I am currently designing an application that is
OK, apologies for the verbose title. Let me give the background in a bit
Let me give some code so you can see what I'm doing with the
Let me give you some examples: Firefox 13: Chrome (latest): As you can see,
I know my question is too inaccurate to answer but let me give you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.