Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / I cant get char arrays to work

Author
Message
Dark Lord
18
Years of Service
User Offline
Joined: 19th Aug 2005
Location: Australia
Posted: 13th Dec 2005 06:03
This code doesn’t work...



What I am actually trying to do is to get a char array to display "the value of a is (and then the actual value of a)"

Can anyone please help?

I have been staring at my LCD for ages and I have a bad headache .
Sephnroth
21
Years of Service
User Offline
Joined: 10th Oct 2002
Location: United Kingdom
Posted: 13th Dec 2005 06:40 Edited at: 13th Dec 2005 06:42
its 5:30am here (these late nights are becoming common >< so im tired but at a quick glance i cant instantly see the problem with your strcat. I can, however, offer an alternitive which greatly reduces the amount of code, is more flexiable and as far as i know is the safest way to handle string combining and variable conversion to string.

sprintf() is the command I would suggest you use, to gain access include stdio.h (standard in-out).

The command will take a destination string as its first parameter and the second string will be a special string constant that can contain special tokens relating to variables you wish to put in there and also how to act on them (sounds complicated, easy when you see it, keep on reading) - the third, fourth, ..., infinity parameter/s are variables you wish to insert to the string. There isnt a limit (that im aware of) of the amount of variables you can pass. Also, you dont have to pass ANY variables.

Example, first example without passing any variables and just writing text to a string assuming szText is an exisiting char array:

sprintf(szText, "Some Text");

simple stuff, will just make szText == "Some Text".

Now in your case you want to add an integer value to the string which is stored in "a". For this you use the %d token in the string and pass the a variable, like this:

sprintf(szText, "The value of A is: %d", a);

assuming A == 1, szText would now read like this: "The value of A is: 1"

you can put %d anywhere in the string and thats where A would appear. if you had a second integer called b, you could do this (slightly more complex example):

sprintf(szText, "A = %d, B = %d, A + B = %d", a, b, a + b);

you see how that works? its important to keep the variables you pass to the function in the same order as what you refrence in your constant string.

other tokens you can use are %f for a float and %s for a string and there will be a ton of others that i cant remember off top of my head but you can look up. brief example:

sprintf(szText, "an int: %d, an float: %f, some text: %s", nNumber, fNumber, "Some Text");

its good because you can get everything in on one line and it produces a solid and safe string - just make sure that szText is big enough to contain all the text you paste into it!

and one last thing briefly, theres other more advanced things you can do with tokens - imagine for example you were writing a game and storing the players score. The players score is stored in nScore, it currently equals 15 and you want to output it to the screen - except you want the final number to be 5 digits, padded with zeros - you could do this:

sprintf(szScoreOutput, "%05d", nScore);

szScoreOutput would now look like "00015" instead of just "15" - if nScore become 250 then it would look like "00250" and for a score of 1500 it would be "01500" etc. Very usful, especially for digital clocks! you can change the number of zero's it pads to by changing the 5, so "%03d" would make it pad to 3 zero's.

Tons more stuff you can do like trimming some of the numbers off the decimal point of a float etc, do look up the command, its probably my most used command in c++.

Sorry if you knew all this and for the long winded explination, if you knew then this hasnt hurt you and if you didnt you should now know everything you need to

Finally, your code using sprinf:



i'm sure someone will be along soon to say why the strcat method was exploding on you

solo
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Israel
Posted: 13th Dec 2005 06:48
dark lord,
you append characters with 'strcat' to a memory that is not reserved
by you, which is illegal operation that causes undefined behaviour.

When you allocate an array without explicit size:
char szStringOne[] =...
then its size is calculated out of its initiate value:
meaning , 1 + strlen of "the value of a is "

you should not perform strcat on that array!
Dark Lord
18
Years of Service
User Offline
Joined: 19th Aug 2005
Location: Australia
Posted: 13th Dec 2005 08:17
Thanks Sephnroth for the explanation, as I wasn’t aware of that method.

@solo - You're right, however, it also says that strcat() has a return type other than void, which confused me.
OSX Using Happy Dude
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 13th Dec 2005 14:29
The return value of strcat can be ignored...

Paisleys finest
18
Years of Service
User Offline
Joined: 11th Nov 2005
Location: In a house
Posted: 30th Dec 2005 01:16
If you're using C++, and lets face it, why not, you could use the Standard Template Library std::string class instead...

much easier than the old style sprintf.. God I used that 10 years ago, time to move on, old bean!

Scott Tunstall
Author of the UNIFIED ZOMBIE MOD (www.unifiedzombiemod.wz.cz)
Current work in progress: Retro Remake of BRUCE LEE, by Datasoft (1984)
Dark Lord
18
Years of Service
User Offline
Joined: 19th Aug 2005
Location: Australia
Posted: 30th Dec 2005 01:45
I am using char arrays because all the output functions for the DarkSDK accept them and not strings. It would be good however if the DarkSDK print functions & load file functions accepted strings instead.
OSX Using Happy Dude
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 30th Dec 2005 01:47
No it wouldn't - every string function would then need to be duplicated.

Accepting CString's would be useful though.

Paisleys finest
18
Years of Service
User Offline
Joined: 11th Nov 2005
Location: In a house
Posted: 30th Dec 2005 01:56
How do you mean every string function would then need to be duplicated?

If you are referring to std::string, that's not true.

You can get the char * from a std::string by using the c_str() method of the class. Why bother with malloc, strcat etc when string class is much more readable?

And as for CStrings - yeech, MFC is a fat bloated pile of mince!!! I really look forward to using AFX_MANAGE_STATE macros in my DB SDK code .

Scott Tunstall
Author of the UNIFIED ZOMBIE MOD (www.unifiedzombiemod.wz.cz)
Current work in progress: Retro Remake of BRUCE LEE, by Datasoft (1984)
OSX Using Happy Dude
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 30th Dec 2005 11:30 Edited at: 30th Dec 2005 11:40
Using std makes things more readable ?

he he he he

It adds an extra layer of complexity to everything. And I hear its not terrible stable anyway.

When hopefully they start using VS 2005, most of the standard C routines have a security-protected equivilent - which will help against memory overflow & buffer errors.

Paisleys finest
18
Years of Service
User Offline
Joined: 11th Nov 2005
Location: In a house
Posted: 30th Dec 2005 12:37
It adds an extra layer of complexity? No it doesn't!! It's also a lightweight template class when compared to the bloated MFC CString you mention

I have been using the standard template library for approx 8, 9 years now and without any problems. I'd love to hear of your experiences!!

What's easier to understand:

char szMyString[255];
strcpy(szMyString, "Hello world");
char *szMyOtherString = (char *) malloc(strlen(szMyString) + 1);
strcpy (szMyOtherString,szMyString);

Or:

using namespace std;

string MyString("Hello World");
string MyOtherString();
MyOtherString = MyString;


I know which one I prefer

Scott Tunstall
Author of the UNIFIED ZOMBIE MOD (www.unifiedzombiemod.wz.cz)
Current work in progress: Retro Remake of BRUCE LEE, by Datasoft (1984)
OSX Using Happy Dude
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 30th Dec 2005 15:23 Edited at: 30th Dec 2005 15:25
Quote: "I'd love to hear of your experiences!!"

Never used it - and certainly dont intend to.

Quote: "It adds an extra layer of complexity? No it doesn't!!"

Yes it does - extra headers, extra libraries, adds an extra possibility for bugs. Is the problem in DirectX, std or DarkSDK ? What about if there is a problem with it ?

Paisleys finest
18
Years of Service
User Offline
Joined: 11th Nov 2005
Location: In a house
Posted: 30th Dec 2005 21:14
Now I've heard the lot

So you say that, introducing what is a simple template class, manages it's own memory heap, leaves no dangling pointers (I forgot in the previous example to free the malloc'd memory, easy mistake to make), is part of the common C++ specification, non Win32 specific.... it will add an extra layer of complexity and new libraries?!! Oh, come on

I mean, isn't that the case with <stdio.h> which you need to include to use strcpy, strcmp etc? Those string routines use their own libs too don't they? That's an extra layer of complexity isnt it.

You say how do you know a problem is in DirectX, DarkSDK or std? Well you can step into the STD libraries and place breakpoints - the source code comes for free. I would trust STL code before self-written string mgmt routines, for sure

Scott Tunstall
Author of the UNIFIED ZOMBIE MOD (www.unifiedzombiemod.wz.cz)
Current work in progress: Retro Remake of BRUCE LEE, by Datasoft (1984)
Paisleys finest
18
Years of Service
User Offline
Joined: 11th Nov 2005
Location: In a house
Posted: 30th Dec 2005 21:16
<I meant string.h>

Caffeine overdose

Scott Tunstall
Author of the UNIFIED ZOMBIE MOD (www.unifiedzombiemod.wz.cz)
Current work in progress: Retro Remake of BRUCE LEE, by Datasoft (1984)
OSX Using Happy Dude
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 30th Dec 2005 21:27 Edited at: 30th Dec 2005 21:37
Quote: "Those string routines use their own libs too don't they? That's an extra layer of complexity isnt it. "

They are in a library file - which means stl is totally not needed. Why start duplicating everything ?

Quote: "I would trust STL code before self-written string mgmt routines, for sure"

Trust STL if you want - certainly not for me.

Besides, there are much more important things to do.

andre
18
Years of Service
User Offline
Joined: 19th Dec 2005
Location:
Posted: 30th Dec 2005 23:32
Dude i have seen some of the tutorials. and i think no one can read those and not understand. the writters of those are very good.

DARK_VIRUS
Trisoft
-------------------------------------------------------------------if you worry you die and if you dont worry you die. f#%k worry
Smithy
19
Years of Service
User Offline
Joined: 8th Dec 2004
Location: Switzerland
Posted: 4th Jan 2006 08:39 Edited at: 5th Jan 2006 08:03
I use std:stringstream.
It works quite good in terms of formatting and converts.
(at least for me heh)

Something like this:
(I am not at home so this is an attempt of my "sick" brain to remember the correct syntax heh)

///////////////////////////
// include std stuff
//
#include <sstream> //string stream
#include <string> //strings
using namespace std;

...

//////////////////
//Helping macros here
//

//convert std::string to ( char*) used by the dark game functions.
#define STR2LPSTR( _text) (const_cast<LPSTR>( (_text).c_str()))
...
ostringstream _OutputText;
f32 _x = 10.12345;
u32 _y = 12345;
...
_OutputText
<<"The value of x is: "
<<_x<<" <- this is a floatin point value."
<<endl; //use "endl" to create "multiline" text output;

_OutputText
<<"The value of y is: "
<<_y
<<endl;
...
//add more lines to _OutputText if you want.
...
_OutputText<<ends; //use "ends" to close the string and ensure correct output with the textclass

//Used for output:
// a Text class based upon the thread "Text extension, Image extension, Primitives Extension"
//( http://forum.thegamecreators.com/?m=forum_view&t=57964&b=22 )
myTextDrawer->StartText();
myTextDrawer->Out( 0, 0, STR2LPSTR( _OutputText.str()), Align::LEFT);
myTextDrawer->EndText();
_OutputText.str( ""); //clear string stream
...

Note: This works for standard dgSDK functions too.
ex.:
ostringstream filename1;
filename1.str( "ex.x");

string filename2;
filename = "ex.x";

dbLoadObject( ID1, STR2LPSTR( filename1));
dbLoadObject( ID2, STR2LPSTR( filename2.str()));
Though, I dont know about the overhead produced by the std strings but I never created a stresstest app to find out if it's a cycle-thief.

//Awards: Best DM at NeverwinterConventionIII (NWCon3)
//Sys: Pentium IV 3200E/Prescott;800Mhz FSB;HT;WinXPPro;ATIR9700PRO;1024MB RAM(2x512MB"DualChanneled";VC++7.net;Delphi6;ADSL512;
Paisleys finest
18
Years of Service
User Offline
Joined: 11th Nov 2005
Location: In a house
Posted: 4th Jan 2006 12:25
Cycle thief? I doubt it. Templates are part of the C++ specification, and have been for 8, 9 years AFAIK.

Microsoft have had plenty of time to optimize their compiler tech, especially with simple thing char arrays

Well, let's hope

Scott Tunstall
Author of the UNIFIED ZOMBIE MOD (www.unifiedzombiemod.wz.cz)
Current work in progress: Retro Remake of BRUCE LEE, by Datasoft (1984)
Smithy
19
Years of Service
User Offline
Joined: 8th Dec 2004
Location: Switzerland
Posted: 4th Jan 2006 19:37
Heh... yeah, one never knows

//Awards: Best DM at NeverwinterConventionIII (NWCon3)
//Sys: Pentium IV 3200E/Prescott;800Mhz FSB;HT;WinXPPro;ATIR9700PRO;1024MB RAM(2x512MB"DualChanneled";VC++7.net;Delphi6;ADSL512;

Login to post a reply

Server time is: 2024-05-07 00:26:50
Your offset time is: 2024-05-07 00:26:50