Advertisement

simple string problem

Started by September 05, 2000 06:05 AM
4 comments, last by gumball 24 years, 3 months ago
Hi, I am creating a generic Level Loading function, but am having trouble creating the appropriate string that matches the filename I want to load: I have a variable - m_iCurrentLevel (integer) and char* temp = 0; char LevelString[11] = "levelX.bmp"; I want to substitute the X with the value of m_iCurrentLevel I have tried: LevelString[5] = _itoa(m_iCurrentLevel,temp,10); and: _itoa(m_iCurrentLevel,temp,10); LevelString[5] = temp[0]; but they dont work. Please help.
Why doesn''t the second one work? What happens? What specifically happens when you try both of these? (The first wont work because it doesn''t return the integer).

try

char temp[2];char buffer[20];_itoa(m_iCurrentLevel,temp,10);strcpy(buffer, "level");strcat(buffer,temp);strcat(buffer, ".bmp");strcpy(LevelString, buffer); 


That should work

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-Chris Bennett of Dwarfsoft
"The Philosophers' Stone of Programming Alchemy"
IOL (The list formerly known as NPCAI) - A GDNet production
Our Doc - The future of RPGs
Thanks to all the goblins over in our little Game Design Corner niche
Advertisement
Hi,

You can also use something like this:

char szLevelString[MAX_PATH];
sprintf(szLevelString, "Level%02d", m_iCurrentLevel);

You problem could be because you level number is not a single character when converted to string (ie. 10 would become "10" and there take 2 char positions in the string).

Try this:

char sLevelNo[2];
char sLevelFile[20];

iLevelNo=10;
sprinf(sLevelFile, "Level%d.bmp", sLevelNo);




Wayne Drury
Swindon, ENGLAND
Wayne DrurySwindon, ENGLAND
I believe the problem is that your temp variable was a char* where what itoa does with that parameter is fill in whatever string address you passed it with the correct string.
i.e. you passed it a NULL, it probably tried to write the string to the base of memory (or did nothing or bombed out).
The function does not (as I suspect you thought it did) create a local variable and make the temp parameter point to it. The variable would go out of the scope of the function anyway. If you made temp or type char[2] as you''ve been shown already then it should work.

Just trying to explain why it didn''t.

Mike
Try this:
LevelString[5] = m_iCurrentLevel%10 + ''0'';
(the %10 is just to ensure you won''t have a character other than a numeric one)

You know, I never wanted to be a programmer...

Alexandre Moura

This topic is closed to new replies.

Advertisement