简体   繁体   中英

How do I make the loop for cataloging random variables in C++?

-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"

virtual int ID() //allows declared value in subclass
{
        return ("My ID number is\n");
}

The int means that the function returns an integer. But it actually returns a string ( const char * ). The compiler has no idea how to convert the const char * you returned into the int you were supposed to return.

As for the second question, you are returning a cstring from the function but you have declared the return type of the function as integer. Anything in between double quotes "" is considered as constant string. Change the return type of your function or either return an integer

Your function is declared to return an integer - you are returning a null terminated string

Instead in the base class maybe return an error type ID like

 virtual int ID()
 {
     return -1;
 }

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 ask for the ID of a type of animal before you have one of that animal, by using " static ": 使用“ static ”在拥有某种动物之前询问该动物的ID:

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.


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM