Is this code correct?

#include"Hobbit.h"
Hobbit::Hobbit()
{
name="Bilbo";
}
Hobbit::~Hobbit()
{

}
void Hobbit::setName(string n)
{
name=n;

}
string Hobbit::getName() const
{
return name;
}
bool Hobbit::hasBeenSummoned(Wizard& w)
{
int coun;
coun=w.getNumberOfSpells();
if(coun<5)
{
w.setHasBeenSummoned(true);

}
return w.getHasBeenSummoned();
}
bool Hobbit::hasCompletedTraining(Wizard& w)
{


w.setNumberOfLossedSpells(0);
w.setHasBeenSummoned(false);
w.setHasCompletedTraining(true);

return w.getHasCompletedTraining();

}
void Hobbit::dropWizardSpells(Wizard& w)
{
string nam;
for(int i=0;i<w.getMaxNumberOfSpells();i++)
{
Spell UGE=w.getSpell(i);
int yea=UGE.getSkillLevel();
if(yea==0)
{
nam=UGE.getName();
w.deleteSpell(nam);
}

}
bool check=hasBeenSummoned(w);
}


#include"Spell.h"

Spell::Spell(string name, int difficultyLevel, int skillLevel)
{
setName(name);
setDifficultyLevel(difficultyLevel);
setSkillLevel(skillLevel);
}

Spell::Spell(const Spell& sp)
{
name=sp.name;
skillLevel=sp.getSkillLevel();
difficultyLevel=sp.difficultyLevel;
}
Spell:: ~Spell()
{

}
void Spell::setName(string n)
{
name=n;
}
string Spell::getName() const
{
return name;
}
void Spell::setDifficultyLevel(int d)
{
difficultyLevel=d;
}
int Spell::getDifficultyLevel() const
{
return difficultyLevel;
}
void Spell::setSkillLevel(int s)
{
if(s>=0)
{
skillLevel=s;
}
else
{skillLevel=0;
}
}
int Spell::getSkillLevel() const
{
return skillLevel;
}

void Spell::operator=(const Spell &ting)
{
name=ting.name;
skillLevel=ting.skillLevel;
difficultyLevel=ting.difficultyLevel;
}

Spell Spell::operator++()
{
++skillLevel;
return *this;
}
Spell Spell::operator++(int)
{
Spell temp(*this);
skillLevel++;
return temp;
}
Spell Spell::operator--()
{
--skillLevel;
setSkillLevel(skillLevel);
return *this;
}
Spell Spell::operator--(int)
{
Spell temp(*this);
skillLevel--;
setSkillLevel(skillLevel);
return temp;
}
ostream &operator << (ostream &strm, const Spell &tingg)
{
strm<<right<<setw(30)<<tingg.name;

strm<<setw(5)<<tingg.difficultyLevel;
strm<<setw(5)<<tingg.skillLevel;
return strm;
}
Spell Spell::operator -=( int ting)
{
setSkillLevel(skillLevel-ting);
return *this;
}

#include"Wizard.h"
#include<string>
#include<iostream>
#include<iomanip>
#include<new>
using namespace std;
Wizard::Wizard()
{
maxNumberOfSpells=10;
hasBeenSummoned=false;
age=20;
numberOfSpells=0;
spells=new Spell[maxNumberOfSpells];
numberOfLossedSpells=0;
hasCompletedTraining=true;
for(int i=0;i<maxNumberOfSpells;i++)
{
spells[i].setName("");
}
}
Wizard::Wizard(const Wizard& w)
{
maxNumberOfSpells=w.maxNumberOfSpells;
hasBeenSummoned=w.hasBeenSummoned;;
age=w.age;
numberOfLossedSpells=w.numberOfLossedSpells;
numberOfSpells=w.numberOfSpells;
hasCompletedTraining=w.hasCompletedTraining;

spells=new Spell[maxNumberOfSpells];
for(int i=0;i<maxNumberOfSpells;i++)
{
spells[i]=w.spells[i];
}

}



void Wizard::addSpell(const Spell& s)
{
if(numberOfSpells<maxNumberOfSpells)
{

for(int i=0;i<maxNumberOfSpells;i++)
{
string check=spells[i].getName();
if(check=="")
{
spells[i]=s;
//cout<<spells[i];
numberOfSpells++;
break;
}
}

}
else
{
++maxNumberOfSpells;
Spell *ting= new Spell[maxNumberOfSpells];
for(int x=0;x<maxNumberOfSpells-1;x++)
{
ting[x]=spells[x];
}
ting[maxNumberOfSpells-1]=s;
numberOfSpells++;
delete[] spells;
spells=new Spell[maxNumberOfSpells];
for(int x=0;x<maxNumberOfSpells;x++)
{
spells[x]=ting[x];
}
// cout<<(spells[numberOfSpells]);
}
}

void Wizard::deleteSpell(string name)
{
for(int i=0;i<maxNumberOfSpells;i++)
{
if(spells[i].getName()==name)
{
spells[i].setName("");
numberOfLossedSpells++;
numberOfSpells--;
break;
/*numberOfLossedSpells++;
spells[i]=Spell();
--numberOfSpells;*/
}
}
}

int Wizard::getNumberOfSpells() const
{
return numberOfSpells;
}
void Wizard::setMaxNumberOfSpells(int m)
{
maxNumberOfSpells=m;

}
int Wizard::getMaxNumberOfSpells() const
{
return maxNumberOfSpells;
}
void Wizard::setAge(int a)
{
age=a;
}
int Wizard::getAge() const
{
return age;
}
int Wizard::getNumberOfLossedSpells() const
{
return numberOfLossedSpells;
}
Spell& Wizard::getSpell(int index) const
{
return spells[index];
}
bool Wizard::getHasCompletedTraining() const
{
return hasCompletedTraining;
}
bool Wizard::getHasBeenSummoned() const
{
return hasBeenSummoned;
}

bool Wizard::operator > (const Wizard &ting)
{
int s1=0;
int s2=0;
for(int i=0;i<getNumberOfSpells();i++)
{
string comp1=spells[i].getName();
int rank1=spells[i].getSkillLevel();
for(int j=0;j<ting.getNumberOfSpells();j++)
{
string comp2=ting.spells[j].getName();
if(comp1==comp2)
{
int rank2=ting.spells[j].getSkillLevel();
if(rank1!=rank2)
{
if(rank1>rank2)
{

s1++;

}
else{s2++;}
}
}
}
}
if(s1>s2)
{
return true;
}
else{return false;}
}

bool Wizard::operator < (const Wizard &ting)
{

int s1=0;
int s2=0;
for(int i=0;i<getNumberOfSpells();i++)
{
string comp1=spells[i].getName();
int rank1=spells[i].getSkillLevel();

for(int j=0;j<ting.getNumberOfSpells();j++)
{
string comp2=ting.spells[j].getName();
if(comp1==comp2)
{
int rank2=ting.spells[j].getSkillLevel();
if(rank1!=rank2)
{
if(rank1<rank2)
{
s2++;
}
else
{ s1++;
}
}
}

}
}

if(s1<s2)
{
return true;
}
else
{return false;}
}


Wizard Wizard::operator -(const string UGE)
{
this->deleteSpell(UGE);
return *this;
}

Wizard Wizard::operator +(const Spell &ting)
{
this->addSpell(ting);
return *this;
}
void Wizard::setNumberOfLossedSpells(int UGE)
{
numberOfLossedSpells=UGE;
}
void Wizard::setHasCompletedTraining(bool ting)
{
hasCompletedTraining=ting;
}
void Wizard::setHasBeenSummoned(bool UGE)
{
hasBeenSummoned=UGE;
}
Wizard::~Wizard()
{
delete[] spells;

spells=NULL;

}
If the header files are required i will upload them for reference
Is this code correct?


What do you mean by "correct"?

If you mean "does it compile?", then try to compile it.

If you mean "does it do what I told it to do?", well then it probably is correct (assuming it compiles), because computers notoriously do exactly what they are told to do, whether or not it's what you want them to do.

If you mean "does it do what I wanted it to do?". then after you compile it, run it and see if it does what you wanted it to do.

When you see a problem, come back here and ask a more specific question, and you will get better help in trying to debug your code.
Line 45: What's the purpose of this line:
bool check=hasBeenSummoned(w);
The value of check goes out of scope immediately.

As doug4 said, you haven;t described what you want your program to do, so we have no way of knowing if it does what you want.

And yes, if you want us to compile and try your program, your headers are required.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Last edited on
Topic archived. No new replies allowed.