473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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

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.
Jul 19 '05 #1
3 1895
Try to put CreatureName into Creature class.
Jul 19 '05 #2
Sin
> CreatureName = "Earth Elemental"; // Line 247

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


strcpy(CreatureName, "Slime");

Alex.
Jul 19 '05 #3
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

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: nospam | last post by:
THIS IS the DOTNETJUNKIES MESSAGE ------------------------- We're Sorry As many of you know we have recently launched SqlJunkies.com. We have overhauled our runtime and will be using it on...
354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
2
by: andy.dreistadt | last post by:
Hi all, I came across another problem that is probably pretty easy but, again, due to my rusty-ness with C, I'm a little stumped. I have a struct that looks like this: /* Instrument Data...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
188
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection -...
1
by: Daniel | last post by:
how to load a C# dll from another C# assembly if the path of the loaded dll is not known until runtime
8
by: Mike Caputo | last post by:
In VB.NET, need to be able to access certain properties on my main form from other forms. These are properties that may be changed by the user, so I have to be able to get to them throughout the...
3
by: Mark Ingram | last post by:
Hi, I'd like to know the best way of checking the current time during a demonstration product. At the minute i store the first run date, then compare that to the system time, but obviously a user...
0
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
0
by: Hans-Werner Hilse | last post by:
Hi, On 22 Jul 2008 01:28:15 GMT ram@zedat.fu-berlin.de (Stefan Ram) wrote: Though it would not have been too difficult to find. But then your point was what exactly? Informing us? Well,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.