Another Demonstration of My Lack of Ability to Program C++

T

Tim Mierzejewski

I'm having trouble assigning a string to a character array. Here's the
applicable code:

#include <iostream>
#include "Creature.hpp" // Includes Creature class
using namespace std;
char CreatureName[16];
void EarthElemental(Creature &Player);
void Slime(Creature &Player);

int main()
{
Creature *Play1 = new Creature
int Selection;
cin << Selection;
switch (Selection)
{
case (1):
EarthElemental(*Play1);
break;
case (2):
Slime(*Play1);
break;
}
return 0;
}

void EarthElemental(Creature &Player)
{
Player->SetValue(2);
CreatureName = "Earth Elemental"; // Line 247
}

void Slime(Creature &Player)
{
Player->SetValue(3);
CreatureName = "Slime"; // Line 254
}
 
S

Sin

CreatureName = "Earth Elemental"; // Line 247

strcpy(CreatureName, "Earth Elemental");
CreatureName = "Slime"; // Line 254

strcpy(CreatureName, "Slime");

Alex.
 
T

Thomas Matthews

Tim said:
I'm having trouble assigning a string to a character array. Here's the
applicable code:

#include <iostream>
#include "Creature.hpp" // Includes Creature class
using namespace std;
char CreatureName[16];
void EarthElemental(Creature &Player);
void Slime(Creature &Player);

int main()
{
Creature *Play1 = new Creature
int Selection;
cin << Selection;
switch (Selection)
{
case (1):
EarthElemental(*Play1);
break;
case (2):
Slime(*Play1);
break;
}
return 0;
}

FYI: Parenthesis are not required for the case label.
Use "case 1:" instead of "case (1):".

FYI: Don't use "magic numbers", prefere named constants:
const unsigned int EARTH_ELEMENTAL_ID = 1;
const unsigned int SLIME_ID = 2;
//...
case EARTH_ELEMENTAL_ID:

void EarthElemental(Creature &Player)
{
Player->SetValue(2);
CreatureName = "Earth Elemental"; // Line 247
}

void Slime(Creature &Player)
{
Player->SetValue(3);
CreatureName = "Slime"; // Line 254
}

Arrays cannot be copied using the assignment operator.
Since you are dealing with text, I suggest you use the std::string
type:
#include <string>
using std::string

string CreatureName;

void EarthElemental(Creature &Player)
{
Player->SetValue(2);
CreatureName = "Earth Elemental";
// or to be explicit:
// CreatureName = string("Earth Elemental")'
}

By the way, you may want to read up on polymorphism.
class Creature
{
protected:
string name_; // Every creature has a name.
public:
void print_name(ostream& out) const
{ out << name_ << endl;}
};


class EarthElemental
: public Creature
{
public:
EarthElemental()
{name_ = "Earth Elemental"};
};


class Slime
: public Creature
{
public:
Slime()
{name_ = "Slime";}
};


int main(void)
{
Creature * Player1(NULL);

// The following code is one implementation of
// of the Factory design pattern. The Factory
// creates a creature based on the User's selection.

unsigned int Selection;
cin << Selection;
switch (Selection)
{
case 1:
// Set Player 1 to an Earth Elemental.
Player1 = new EarthElemental
break;
case 2:
// Set Player 1 to a Slime.
Player1 = new Slime;
break;
}

// Test the factory.
if (Player1 != NULL)
Player1->print_name(cout);
else
cout << "Invalid selection: " << Selection << endl;
return 0;
}

In the above example, every creature has-a name.
name is an attribute of the base class Creature.
There is also a method for printing out the name.

So, after a creature is created, the name is printed
out regardless of which instance was created by the
factory. The print method is a method of the base
class and doesn't depend on the type of the child
classes.

The base class Creature should contain members and
methods common to all creatures.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top