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 / Adding strings

Author
Message
Dimension
20
Years of Service
User Offline
Joined: 23rd Nov 2003
Location:
Posted: 23rd Aug 2009 02:53
In darkbasic you can add strings like follows "Countdown "+str$(i) how would you go about doing this in c++.

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 23rd Aug 2009 02:58
You can create a formatted string with sprintf.



Always make sure the output buffer is big enough to store the string.
heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 23rd Aug 2009 03:01 Edited at: 23rd Aug 2009 03:01
there are a few ways, but this is the most common I think:

that would display "Countdown: " then the value of i at position 0,0 on the screen.

%d - decimal
%i - integer
%f - float
%s - string

Games are like life, they should never stand still.
MFC Coder
15
Years of Service
User Offline
Joined: 5th Mar 2009
Location:
Posted: 23rd Aug 2009 16:55
This is probably WAY overkill for what you need but I thought if you ever find the need for some really complex formatting the stream classes for strings are quite versatile. I started using std::ostrstream with these types of outputs since I can use std::string in my code as well as the other types and format them all quite easily. Here is an example:



MFC Coder
Dimension
20
Years of Service
User Offline
Joined: 23rd Nov 2003
Location:
Posted: 25th Aug 2009 02:24
also I found
char source[256];
stdcat(source, "added");
does anybody now exactly what this does?

Mista Wilson
16
Years of Service
User Offline
Joined: 27th Aug 2008
Location: Brisbane, Australia
Posted: 25th Aug 2009 02:31
strcpy()
strcat()
strcmp()

and a bunch of others are C-String functions. They are designed to work with character array pointers and are basically native C functions. These are the ones I tend to use for debug text as they are quite quick the syntax is simple to write out and change over and over again... a simple usage might be :




If it ain't broke.... DONT FIX IT !!!
TheGroggyOne
15
Years of Service
User Offline
Joined: 11th Apr 2009
Location:
Posted: 25th Aug 2009 15:12
Use the string class. The include is sstream. The code is along the lines of...

int i = some integer;
std::stringstream str;
str << i;
std::string s = str.str();
std::string out("Countdown ");
out += s;
DnB
15
Years of Service
User Offline
Joined: 28th Feb 2009
Location:
Posted: 29th Sep 2009 19:35
Do something like this, it works.



That is called concatenation.
DnB
15
Years of Service
User Offline
Joined: 28th Feb 2009
Location:
Posted: 29th Sep 2009 19:38
Sorry lol, that was my code for DarkGDK

Here is a more C++ Approach..



Of course you could just save yourself a whole step and put the last line as

string FinalResult = buffer + " " + buffer3;
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 29th Sep 2009 20:09
Quote: "std::string buffer2 = dbGetDate ( );"


This is a bad idea, as you'll lose the pointer returned by dbGetDate and thus you'll get a memory leak.

Login to post a reply

Server time is: 2024-10-01 14:27:05
Your offset time is: 2024-10-01 14:27:05