🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

cahar only allows a max of 5 characters...

Started by
5 comments, last by Fractal 22 years, 8 months ago
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"?)
Could I be any dumber?(What do you mean, "No"?)
Advertisement
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!
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.
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"?)
Could I be any dumber?(What do you mean, "No"?)
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...
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!]
Null and Void: thanks.

This topic is closed to new replies.

Advertisement