• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# cahar only allows a max of 5 characters...

## Recommended Posts

Im making a text-based computer game in DOS and one of my lines of code goes like this - int element; char player; if (element == ''1'') {player = ''fire'';} else if (element == ''2'') {player = ''earth'';} else if (element == ''3'') {player = ''forest'';} else {player = ''air'';} the error occurs in the words ''earth'' and ''forest'', the compiler tells me that they have too many characters, but ''air'' and ''fire'' are just fine. How could my problem be solved? Could I be any more dumber? (What do you mean, "No"?)

##### Share on other sites
Using single quotes is like saying ''The value of''. For example, the letter A has a numerical value. You shouldn''t be able to use any more then a single character in single quotes, so I dont know why it let the 3 letter words through.

What I would do, is make #define''s. For example:
#define FIRE 1
This would let you use the word FIRE, and the compiler replaces it with the value of 1 in the program itself. This allows you to place values for every element without having to do any drastic changes. Basically (I suck at explaining), you can use the word FIRE instead of ''fire'' which doesn''t work, and it would always represent a predefined number.

You can also use constants, which may be better, I believe it makes debugging easier when you made an error that would be harder to track using #define.

You could also use a character string (like char player[6] and store entire elemental words, but this may not be the best method. You would need to use strcmp() and maybe even strcpy().

I hope this helps in some way, explaining isn''t my best skill

If you can read this, All your base are belong to us!

##### Share on other sites
I''ll expand SirSmoke''s method slightly.

Rather than trying to assign strings of different length to the player variable, use a static string table (elements below) and use the player value as an index to that table.
char elements[5][] = {  "Fire",  "Earth",  "Forest",  "Air"};int player; // notice that player is now an integer!//...// if we get element as the input from the playerplayer = element;//...// now when we want to display the what the player is/uses:cout << elements[player] << endl;

If the player is 1, you''d get "Fire" as your output.

##### Share on other sites
When I try this, I get two different errors, one goes like this:

"error C2117: ''Fire'' : array bounds overflow"

and the other like this:

"error C2087: '''' : missing subscript" which points at the "char elements[5][] = {" line.

Could I be any more dumber?
(What do you mean, "No"?)

##### Share on other sites
My bad. I think the only unspecified dimension permitted is the first. I''ll cook up another solution (when I get back later this evening). Meanwhile, you go read your documentation for once...

##### Share on other sites
Do it like this:
  char *elements[5] = { "Fire", "Earth", "Forest", "Air"};

I think the reason the compiler let 1/2/3 character strings through is that they can fit inside of a DWORD. I''m not sure if it''s meant to do that though, since the data would get truncated anyway...

[Resist Windows XP''s Invasive Production Activation Technology!]

##### Share on other sites
Null and Void: thanks.

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627654
• Total Posts
2978447

• 10
• 12
• 22
• 13
• 33