-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”
For your first question, this may take a few tries.
First, suppose you just wanted to choose an animal randomly:
That works, but the ID numbers are “hard-coded” here, it doesn’t use the
ID()function you wrote. If you want to useID(), you could have one of each animal and see which one matchesnumber: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 codeID()so that you can ask for the ID of a type of animal before you have one of that animal, by using “static“:This also solves your second problem because you no longer have the problematic
OD()inanimal.Is that sufficient? There are other possibilities.
EDIT:
You forgot
int number = rand() % 6 + 1;.