![](smile.gif)
Cannot convert from char[38] to char[100]
Hey guys,
I''m programming a simple text game and there is a structure where each location on the maps data is stored. In it is a char array called description to store a descripiton of that location in. When I try to set the description it gives a error message that says
"C:\Windows\Desktop\Text_Game_C.cpp(71) : error C2440: ''='' : cannot convert from ''char [38]'' to ''char [100]''"
Here is the code for the struct and the statement to set the variable :
struct:
struct location {
char description[100];
int exits[4];
}map[2];
variable:
map[1].description = "A dark room with an exit to the east\0";
map[1].exits[E] = 1;
map[2].description = "Another dark room with an exit to the west\0";
map[2].exits[W] = 1;
Please, any help on this matter will be greatly appreciated
Thx in advance
![](smile.gif)
Hi Armed,
The problem with this is that you are trying to assign a string of only 38 characters to one that was declared as one for 100 characters. C/C++ does not like this straight assignment and I am not entirely clear on why so.
But there is a resolution and it goes like this:
instead of this:
map[1].description = "A dark room with an exit to the east\0";
do this:
strcpy (map.description,"A Dark room with an exit to the east");
this would work fine. You see, strcpy() is a function that should be in your compiler''s library (It just has to be) it stands for
string copy and it''s prototype is somewhat like this
int strcpy (char *dest, char *source);
it copies the source string into the destination string. I think it returns positive value if successful, but its always successful. I don''t think it requires you to send a null terminated string because it does it for you. Try it, it should work. Or you can write your own string copier which copies character by character from source to destination until null terminator is met but using strcpy is easier.
Be sure to include string.h file at the top of your C++ file because this function belongs to that part of the library... check out what other string functions are available, they are very useful.
Hope this helped in anyway
Dark Star
The problem with this is that you are trying to assign a string of only 38 characters to one that was declared as one for 100 characters. C/C++ does not like this straight assignment and I am not entirely clear on why so.
But there is a resolution and it goes like this:
instead of this:
map[1].description = "A dark room with an exit to the east\0";
do this:
strcpy (map.description,"A Dark room with an exit to the east");
this would work fine. You see, strcpy() is a function that should be in your compiler''s library (It just has to be) it stands for
string copy and it''s prototype is somewhat like this
int strcpy (char *dest, char *source);
it copies the source string into the destination string. I think it returns positive value if successful, but its always successful. I don''t think it requires you to send a null terminated string because it does it for you. Try it, it should work. Or you can write your own string copier which copies character by character from source to destination until null terminator is met but using strcpy is easier.
Be sure to include string.h file at the top of your C++ file because this function belongs to that part of the library... check out what other string functions are available, they are very useful.
Hope this helped in anyway
Dark Star
---------------------------------------------You Only Live Once - Don't be afriad to take chances.
Or if it is C++ you could change
char description[100];
to
std::string description;
I think you are already seeing how inefficient hardcoding 100 is. (you example strings don''t have nearly 100 chars in them).
Also strcpy will not detect when you try to copy in a string that is greater than 100 chars and will do funky things (may crash, may not).
DSutherland
char description[100];
to
std::string description;
I think you are already seeing how inefficient hardcoding 100 is. (you example strings don''t have nearly 100 chars in them).
Also strcpy will not detect when you try to copy in a string that is greater than 100 chars and will do funky things (may crash, may not).
DSutherland
Also, there is no reason to put a ''\0'' at the end of your strings. A quoted string already has a null terminator in it, thus making occupy the length of the string + 1.
Why don''t you store all your strings one on top of another in a big char array and simply have a pointer to each one from your map struct?
Why don''t you store all your strings one on top of another in a big char array and simply have a pointer to each one from your map struct?
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
The reason you get the error is because a string literal is basically a pointer to a string. Your description variable is declared as an array, and that means you can''t change the location of the memory it points to. Your code is trying to reassign the location the array is pointing to, which you can''t do because arrays are constant pointers, so your compiler is complaining and giving up.
You would have no problem if description was just a pointer to char, since the string literal is compiled into the executable and loaded into memory, so you don''t have to worry about the size of the string, you can just reassign the pointer to point to any string already in memory.
Anyway, strcpy() does, as mentioned, suffer from the problem that if you don''t have a properly allocated buffer of sufficient size then you can get access violations or overwrite other variables. An alternative to using strcpy() is sprintf(), which is just like the printf() function except it prints to a string, which you specify as the first parameter. sprintf() also requires a properly allocated buffer and can cause serious problems if you don''t provide one, but there is a similar function called _snprintf() which is a safe version of sprintf(), avoiding critical errors. (If you''re using TCHARS there''s another version, _sntprintf() ).
Harry.
You would have no problem if description was just a pointer to char, since the string literal is compiled into the executable and loaded into memory, so you don''t have to worry about the size of the string, you can just reassign the pointer to point to any string already in memory.
Anyway, strcpy() does, as mentioned, suffer from the problem that if you don''t have a properly allocated buffer of sufficient size then you can get access violations or overwrite other variables. An alternative to using strcpy() is sprintf(), which is just like the printf() function except it prints to a string, which you specify as the first parameter. sprintf() also requires a properly allocated buffer and can cause serious problems if you don''t provide one, but there is a similar function called _snprintf() which is a safe version of sprintf(), avoiding critical errors. (If you''re using TCHARS there''s another version, _sntprintf() ).
Harry.
Harry.
Okay guys I went and put all the description strings into a lond string with a ''/'' denoting the end of each descripiton. I am testing with 2 descripitions currently. The second one starts at position 37 in the string. It compiles without errors but whenever the player.x_pos variable is set to 2 ie you move east it crashes with an illegal operation which leads me to believe there is a problem with a pointer, anyway here is the source just copy it into vc++ and hit compile hehe. Oh ya, the commands are q to quit, and e to move east and w to move west. right now ther is no detection of where you are so when it asks for a command you can only hit e to change player.x_pos to 2 otherwise if your at location one you step out of the array
Well heres the code:
#include
#include
const N=1;
const E=2;
const W=3;
const S=4;
struct location {
char *description;
int exits[4];
}map[2];
char descriptions[] = {"A dark room with an exit to the east/Another dark room with an exit to the west/"};
struct character {
int x_pos;
} player;
void NewGame(void);
void InitializeGame(void);
void PrintDescription(void);
bool GetCommands(void);
int main(void) {
char choice = ''l'';
printf("welcome to my C text game");
printf("make a choice: \n (P)lay \n (E)xit \n : ");
do {
scanf("%c", &choice);
switch(choice) {
case ''P'':
case ''p'':
NewGame();
break;
case ''E'':
case ''e'':
return 0;
break;
default:
printf("please enter a valid choice");
break;
}
}while(choice != ''P'' && choice != ''p'' && choice != ''e'' && choice != ''E'');
return 0;
}
void NewGame(void) {
bool exit = false;
InitializeGame();
//Game loop follows:
do {
PrintDescription();
exit = GetCommands();
}while(exit != true);
}
void InitializeGame(void) {
map[1].description = descriptions;
map[1].exits[E] = 1;
printf("%c", *map[1].description);
map[2].description = descriptions + 37;
map[2].exits[W] = 1;
player.x_pos = 2;
}
void PrintDescription(void) {
do {
printf("%c", *map[player.x_pos].description);
map[player.x_pos].description++;
}while(*map[player.x_pos].description != ''/'');
printf("\n");
}
bool GetCommands(void) {
char command = ''z'';
printf("enter a command q to exit: \n");
do {
scanf("%c", &command);
switch(command) {
case ''E'':
case ''e'':
player.x_pos += 1;
break;
case ''W'':
case ''w'':
player.x_pos -=1;
break;
default:
printf("please enter a valid command!\n");
break;
};
}while(command != ''E'' && command != ''e'' && command != ''W'' && command != ''w'' && command != ''q'' );
if (command == ''Q'' || command == ''q'') return true;
else return false;
}
![](wink.gif)
Well heres the code:
#include
#include
const N=1;
const E=2;
const W=3;
const S=4;
struct location {
char *description;
int exits[4];
}map[2];
char descriptions[] = {"A dark room with an exit to the east/Another dark room with an exit to the west/"};
struct character {
int x_pos;
} player;
void NewGame(void);
void InitializeGame(void);
void PrintDescription(void);
bool GetCommands(void);
int main(void) {
char choice = ''l'';
printf("welcome to my C text game");
printf("make a choice: \n (P)lay \n (E)xit \n : ");
do {
scanf("%c", &choice);
switch(choice) {
case ''P'':
case ''p'':
NewGame();
break;
case ''E'':
case ''e'':
return 0;
break;
default:
printf("please enter a valid choice");
break;
}
}while(choice != ''P'' && choice != ''p'' && choice != ''e'' && choice != ''E'');
return 0;
}
void NewGame(void) {
bool exit = false;
InitializeGame();
//Game loop follows:
do {
PrintDescription();
exit = GetCommands();
}while(exit != true);
}
void InitializeGame(void) {
map[1].description = descriptions;
map[1].exits[E] = 1;
printf("%c", *map[1].description);
map[2].description = descriptions + 37;
map[2].exits[W] = 1;
player.x_pos = 2;
}
void PrintDescription(void) {
do {
printf("%c", *map[player.x_pos].description);
map[player.x_pos].description++;
}while(*map[player.x_pos].description != ''/'');
printf("\n");
}
bool GetCommands(void) {
char command = ''z'';
printf("enter a command q to exit: \n");
do {
scanf("%c", &command);
switch(command) {
case ''E'':
case ''e'':
player.x_pos += 1;
break;
case ''W'':
case ''w'':
player.x_pos -=1;
break;
default:
printf("please enter a valid command!\n");
break;
};
}while(command != ''E'' && command != ''e'' && command != ''W'' && command != ''w'' && command != ''q'' );
if (command == ''Q'' || command == ''q'') return true;
else return false;
}
I think you new method is worse than the original. You''re gonna have to find out how far each string is into the char array, and if you change one you have to change them all. Also, the reason you''re crashing is that you''re overstepping your array bounds. If you say map[2], it means an array of two elements, but you access the first one as map[0], not map[1]. So when you put map[2] in, that probably cause the error.
Since you''re using a C++ compiler, i strongly suggest you use the std::string class instead of a char array. Like so:
struct:
typedef struct{
std::string description;
int exits[4];
} location;
variable
Here I''m assuming you enum''ed or defined E and W to be some meaninful integer between 0 and 3).
location map[2]
map[0].description = "A dark room with an exit to the east";
map[0].exits[E] = 1;
map[1].description = "Another dark room with an exit to the west";
map[1].exits[W] = 1;
Hamhed
Since you''re using a C++ compiler, i strongly suggest you use the std::string class instead of a char array. Like so:
struct:
typedef struct{
std::string description;
int exits[4];
} location;
variable
![](sad.gif)
location map[2]
map[0].description = "A dark room with an exit to the east";
map[0].exits[E] = 1;
map[1].description = "Another dark room with an exit to the west";
map[1].exits[W] = 1;
Hamhed
i agree .. go with the std::string .
{ Stating the obvious never helped any situation !! }
{ Stating the obvious never helped any situation !! }
how about this....
struct mystruct
{
char x[100];
};
mystruct array_of_struct[100];
have an array of structures then use "getline" to read in the description.
cin.getline(array_of_struct.x,99,''\n'');
something like that. sorry it''s L8 and time for bed.
-----------------------------
"There are ones that say they can and there are those who actually do."
"...u can not learn programming in a class, you have to learn it on your own."
struct mystruct
{
char x[100];
};
mystruct array_of_struct[100];
have an array of structures then use "getline" to read in the description.
cin.getline(array_of_struct.x,99,''\n'');
something like that. sorry it''s L8 and time for bed.
-----------------------------
"There are ones that say they can and there are those who actually do."
"...u can not learn programming in a class, you have to learn it on your own."
![](http://www.crosswinds.net/~druidgames/resist.jpg)
-----------------------------"There are ones that say they can and there are those who actually do.""...u can not learn programming in a class, you have to learn it on your own."
Hey guys thanx for all the help and suggestions, I solved the overstepping the bounds of the array problem so now the engine prints the description. Its alright for me to store the descriptions in a long string seperated by /''s tho cause it will be read in by file and I will keep track of assigning everything with the comp and my code so it will be all automatic: ) however I have run one more small problem that seems to elude me at the moment:
Whenever I print the first description of a location in the game, no matter what location I start it at it prints a little ''@'' in front of the first leter of the description. but after that if I print the same description again or any other description for that matter the ''@'' still shows up for some reason, any ideas?
thx for all the great help guys
Whenever I print the first description of a location in the game, no matter what location I start it at it prints a little ''@'' in front of the first leter of the description. but after that if I print the same description again or any other description for that matter the ''@'' still shows up for some reason, any ideas?
thx for all the great help guys
![](smile.gif)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement