1

This is for a simple game and I am trying to learn how to apply C++ in an existing project (that's how I learn best), I have experience programming in C# and other high level languages so it is quite new to me still. So I have this list of races:

const char* Races[] = {
   "Demon",
   "Human",
   "Elf",
   "Orc",
   "Aliens"
};

At one point I receive the registration of a user and a race is selected. I save away all the information of the user to a struct and I want to save the full name of the race to this struct as well.

struct User_t {
   unsigned int RaceID;
   char Race[16];
};

The following code is what I got to put the values inside the struct:

User_t User;

User.RaceID = 3;
strcpy(User.Race, Races[User.RaceID]);

This, however, does not work. This is part of a game and the exception handling is horrible (it basically crashes the game without any error visible). I am trying to figure out what I am doing wrong. Any suggestions, perhaps suggestions on other things as well?

5
  • 6
    If you are using C++, why not just use std::string ? Commented Aug 5, 2015 at 10:37
  • 2
    You'll find it much easier to use std::string and other classes rather than c-strings and structs. Commented Aug 5, 2015 at 10:37
  • 1
    @Roel What do you mean saying that "it does not work"? Commented Aug 5, 2015 at 10:38
  • The problem is definitely elsewhere. Commented Aug 5, 2015 at 10:43
  • As far as your shared code is considered, it is fine, i guess issue is somewhere else. Commented Aug 5, 2015 at 10:45

3 Answers 3

5

I am trying to figure out what I am doing wrong. Any suggestions, perhaps suggestions on other things as well?

You are trying to use C++ as if it was C (which is possible syntactically), and you do not know C nor C++ well enough to do that.

Do yourself a favor and do it in real C++:

std::string Races[] = {
   "Demon",
   "Human",
   "Elf",
   "Orc",
   "Aliens"
};

struct User {
  unsigned int RaceID;
  std::string Race;

  explicit User(unsigned int rid)
  : RaceID(rid)
  , Race(Races[rid])
  {}
};

User user(3);

std::cout << user.Race << '\n';
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest that you look in to enum class{}. It's easier to make your code less error-prone.

You could write your code like this:

enum class eRaces
{
    eDemon,
    eHuman,
    eElf,
    eOrc,
    eAlien,
};

struct User
{
    eRaces myRace;
};

User User;

User.myRace = eRaces::eElf;

Comments

-1

Assuming that scope of Races array is global you can simply avoid copying it and just do something like this:

struct User_t {
   unsigned int RaceID;
   const char* Race;
};

User_t User;

User.RaceID = 3;
User.Race = Races[User.RaceID];

You can also use std::string as mentioned in comments but you wont avoid copying it. Since you develop game i think that performance has higher priority over objectivity of c++ language.

4 Comments

Performance concerns about copying a ten-chars string once in the lifetime of a player ?
I actually decided not to copy the name over (performance related) and simply select the value from the char* array every time I need it. Thank you!
@Quentin If object User is copied somewhere, it won't be once in the lifetime of a player. Overhead might be very small but in my opinion it's always better to take performance into consideration.
Profiling is not really my domain, but this train of thought reeks of premature optimization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.