Jump to content

  • Log In with Google      Sign In   
  • Create Account


cahar only allows a max of 5 characters...


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Fractal   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 October 2001 - 01:56 AM

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"?)

Sponsor:

#2 Necrosis   Members   -  Reputation: 150

Like
Likes
Like

Posted 22 October 2001 - 03:14 AM

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!

#3 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 22 October 2001 - 03:26 AM

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 player
player = 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.

#4 Fractal   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 October 2001 - 10:09 AM

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"?)


#5 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 22 October 2001 - 10:37 AM

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...

#6 Null and Void   Moderators   -  Reputation: 1087

Like
Likes
Like

Posted 22 October 2001 - 10:44 AM

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!]

#7 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 22 October 2001 - 12:38 PM

Null and Void: thanks.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS