#include <iostream>
#include <string>
#include <cassert>//added
#include "Animal.h"
#include "stringManipulators.h"
#include "cow.h"
#include "greeniguana.h"
#include "komododragon.h"
#include "manatee.h"
#include "panther.h"
#include "rattleSnake.h"
#include "sparrow.h"
#include "spermwhale.h"
#include "random.h"
usingnamespace std;
bool AskYesNoQuestion(const string& prompt);
void playAnimalRecognizer(Animal*animal[], unsigned n_animals, unsigned secret_animal_index);
int main()
{
Random<int> random_generator;
constunsigned n_animals = 8;
Animal **animal = new Animal*[n_animals];
/*
fill in missing code
*/
string prompt = "Do you want to play animal recognizer? ";
while(AskYesNoQuestion(prompt))
{
int secret_animal = random_generator.next(0, n_animals - 1);
playAnimalRecognizer(animal, n_animals, secret_animal);
prompt = "Do you want to play again? ";
}
system("pause");
return 0;
}
bool AskYesNoQuestion(const string& prompt)
{
cout << prompt;
string answer;
getline(cin, answer, '\n');
answer = StringManipulators::trim(answer);
answer = StringManipulators::toUpper(answer);
return answer == StringManipulators::empty_string || answer[0] == 'Y';
}
void playAnimalRecognizer(Animal*animal[], unsigned n_animals, unsigned secret_animal_index)
{
assert(0 <= secret_animal_index && secret_animal_index < n_animals);
if(animal[secret_animal_index]->IsMammal()) // having problem with it
cout << "I am a mammal" << endl;
else
cout << "I am not a mammal" << endl;
if(animal[secret_animal_index]->IsMeatEater()) //having problem with it
cout << "I am a meat eater" << endl;
else
cout << "I am not a meat eater" << endl;
if(animal[secret_animal_index]->HasFourLegs()) // having problem with it
cout << "I have four legs" << endl;
else
cout << "I do not have four legs" << endl;
cout << "Which animal do you think I am?" << endl;
cout << "Am I a Cow?" << endl;
cout << "Am I a Green Iguana?" << endl;
cout << "Am I a Komodo Dragon?" << endl;
cout << "Am I a Manatee?" << endl;
cout << "Am I a Panther?" << endl;
cout << "Am I a Rattle Snake?" << endl;
cout << "Am I a Sparrow?" << endl;
cout << "Am I a Sperm Whale?" << endl;
cout << "You think the animal's name is ";
string your_animal_name;
getline(cin, your_animal_name, '\n');
your_animal_name = StringManipulators::trim(your_animal_name);
string secret_animal_name = secret_animal->name();
if(your_animal_name == secret_animal_name)
cout << "You win" << endl;
else
{
cout << "You lose" << endl;
cout << "I am not a " << your_animal_name << endl;
}
cout << "I am a " << secret_animal_name << endl;
}
Animal should be an abstract class.
Some Animal's methods should be virtual.
You shouldn't ignore case when including files. It works on Windows, but that's not the only OS in the world.
animal[0] = new /* the animal you want */;
animal[1] = new /* another animal you want */;
animal[2] = new /* you get it */;
But that only works if you make Animal functions virtual. Otherwise, the functions called will be those defined in Animal, not in any of its subclasses.
you mean this
animal[0] = new "Cow";
animal[1] = new "Green Iguana";
animal[2] = new "Komodo Dragon";
animal[3] = new "Manatee";
animal[4] = new "Panther";
animal[5] = new "Rattle Snake";
animal[6] = new "Sparrow?";
animal[7] = new "Sperm Whale";
thank you for replying and helping
I have to do this way because random_generator generates random number then animal[n_animals]
animal[0] = new Cow;
animal[1] = new GreenIguana;
animal[2] = new KomodoDragon;
animal[3] = new Manatee;
animal[4] = new Panther;
animal[5] = new RattleSnake;
animal[6] = new Sparrow;
animal[7] = new SpermWhale;
That's because you are not overwriting one or more methods declared as abstract on Animal. Make sure you used the same signature (return type, name, number of parameters, parameter types). Your compiler should point you out the ones you didn't.