Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Another Demonstration of My Lack of Ability to Program C++

Reply
Thread Tools

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

 
 
Tim Mierzejewski
Guest
Posts: n/a
 
      06-26-2003
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
}

--
The errors I get are:
(247) : error C2106: '=' : left operand must be l-value
(254) : error C2440: '=' : cannot convert from 'char [6]' to 'char [16]'
--
Sorry for bugging you again with a probably simple question, but I'm really
new at this. Thanks.
Tim M.


 
Reply With Quote
 
 
 
 
Sin
Guest
Posts: n/a
 
      06-26-2003
> CreatureName = "Earth Elemental"; // Line 247

strcpy(CreatureName, "Earth Elemental");

> CreatureName = "Slime"; // Line 254


strcpy(CreatureName, "Slime");

Alex.


 
Reply With Quote
 
 
 
 
ma3x
Guest
Posts: n/a
 
      06-26-2003
Try to put CreatureName into Creature class.


 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      06-26-2003
Tim Mierzejewski wrote:
> 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
> }
>
> --
> The errors I get are:
> (247) : error C2106: '=' : left operand must be l-value
> (254) : error C2440: '=' : cannot convert from 'char [6]' to 'char [16]'
> --
> Sorry for bugging you again with a probably simple question, but I'm really
> new at this. Thanks.
> Tim M.


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lack of ability to make up a wireless network WITHOUT an internet Dan171681 Wireless Networking 2 03-02-2009 01:52 AM
Public ASP.NET Web Form Demonstration John Rivers ASP .Net 1 02-17-2006 07:52 PM
Only a small number of users were able to access our ASP.NET application during a demonstration - why? Rod ASP .Net 3 12-13-2004 10:00 PM
Perfect SD9 color, a graphic demonstration (pics) George Preddy Digital Photography 54 01-20-2004 11:01 PM
Please help with photo demonstration... Kevin Miller Digital Photography 3 09-15-2003 05:34 PM



Advertisments