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

Discussion in 'C++' started by Tim Mierzejewski, Jun 26, 2003.

  1. 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.
    Tim Mierzejewski, Jun 26, 2003
    #1
    1. Advertising

  2. Tim Mierzejewski

    Sin Guest

    > CreatureName = "Earth Elemental"; // Line 247

    strcpy(CreatureName, "Earth Elemental");

    > CreatureName = "Slime"; // Line 254


    strcpy(CreatureName, "Slime");

    Alex.
    Sin, Jun 26, 2003
    #2
    1. Advertising

  3. Tim Mierzejewski

    ma3x Guest

    Try to put CreatureName into Creature class.
    ma3x, Jun 26, 2003
    #3
  4. 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
    Thomas Matthews, Jun 26, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rod
    Replies:
    3
    Views:
    356
    Karl Seguin
    Dec 13, 2004
  2. John Rivers

    Public ASP.NET Web Form Demonstration

    John Rivers, Feb 17, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    353
    =?Utf-8?B?YnJpYW5zW01DU0Rd?=
    Feb 17, 2006
  3. Stefan Ram

    Demonstration against XML

    Stefan Ram, Jul 22, 2008, in forum: XML
    Replies:
    1
    Views:
    376
    Hans-Werner Hilse
    Jul 22, 2008
  4. KiwiBrian

    Table demonstration page

    KiwiBrian, Aug 13, 2008, in forum: HTML
    Replies:
    14
    Views:
    665
    Blinky the Shark
    Aug 16, 2008
  5. Barak, Ron
    Replies:
    0
    Views:
    289
    Barak, Ron
    Feb 23, 2009
Loading...

Share This Page