42_EXAM
42_EXAM copied to clipboard
[Contribution/Suggestion] Exam Rank 05
Hello! I just did the exam rank 05, but I kept failing the last exercise. I couldn't tell what was wrong because there were no traces. When I looked at the traces after the exam, I discovered a different output. I could not see the main, but I might determine the type of test I failed.
The output was like this:
Richard: This looks like another boring day.
Richard: I am Richard, Hello, I'm Richard the Warlock!!
Target Practice Dummy has been fwooshed!
Target Practice Dummy has been fwooshed!
Target Practice Dummy has been fwooshed!
Richard: My job here is done!
I guess the correct output, it's supposed to be like this:
Richard: This looks like another boring day.
Richard: I am Richard, Hello, I'm Richard the Warlock!!
Target Practice Dummy has been fwooshed!
Richard: My job here is done!
A possible main it's this:
int main()
{
Warlock richard("Richard", "foo");
richard.setTitle("Hello, I'm Richard the Warlock!");
ASpell *fwoosh = new Fwoosh;
richard.learnSpell(fwoosh);
richard.learnSpell(fwoosh);
richard.learnSpell(fwoosh);
ATarget* dummy = new Dummy;
richard.introduce();
richard.launchSpell("Fwoosh", *dummy);
richard.forgetSpell("Fwoosh");
richard.launchSpell("Fwoosh", *dummy);
richard.forgetSpell("Fwoosh");
richard.launchSpell("Fwoosh", *dummy);
richard.forgetSpell("Fwoosh");
richard.launchSpell("Fwoosh", *dummy);
delete dummy;
delete fwoosh;
}
Here is the SpellBook that passed the grademe exam, but not the exam itself:
#pragma once
#include <set>
#include <iterator>
#include "ASpell.hpp"
class SpellBook
{
public:
SpellBook();
~SpellBook();
void learnSpell(ASpell*spell);
void forgetSpell(std::string const &spellName);
ASpell *createSpell(std::string const &spellName);
private:
std::set<ASpell*> _loot;
SpellBook(const SpellBook &src);
SpellBook &operator=(const SpellBook &src);
};
#include "SpellBook.hpp"
SpellBook::SpellBook()
{
}
SpellBook::~SpellBook()
{
for (std::set<ASpell*>::iterator it = this->_loot.begin(); it != this->_loot.end(); it++)
delete (*it);
}
void SpellBook::learnSpell(ASpell*spell)
{
this->_loot.insert(spell->clone());
}
void SpellBook::forgetSpell(std::string const &spellName)
{
for (std::set<ASpell*>::iterator it = this->_loot.begin(); it != this->_loot.end(); it++)
{
if ((*it)->getName() == spellName)
{
ASpell *tmp;
tmp = *it;
this->_loot.erase(*it);
delete tmp;
break;
}
}
}
ASpell *SpellBook::createSpell(std::string const &spellName)
{
for (std::set<ASpell*>::iterator it = this->_loot.begin(); it != this->_loot.end(); it++)
{
if ((*it)->getName() == spellName)
{
return (*it);
}
}
return NULL;
}
SpellBook::SpellBook(const SpellBook &src)
{
*this = src;
}
SpellBook &SpellBook::operator=(const SpellBook &src)
{
(void)src;
return *this;
}
[Update]
I was able to pass the exam by using a map <std::string, ASpell *>.
Besides using a map, I still checked the SpellBook first to make sure the spell name wasn't already present before adding it.
On the Warlock Class in cpp_module_01 and TargetGenerator in cpp_module_02, the same was done.