String functions in C++

Post Reply
jcvamp
Posts: 6
Joined: Sat Nov 21, 2009 7:55 pm

String functions in C++

Post by jcvamp » Thu Dec 03, 2009 7:52 pm

The string variable type isn't included in the NDS library. I don't know whether the DS supports it. Anyway, all the examples I've seen use Char arrays instead. Anyway, I'm trying to make a function where a string is output, but I can't work out how to do it.

Can anyone help?

Thanks.

User avatar
vuurrobin
Posts: 222
Joined: Fri Jul 11, 2008 8:49 pm
Location: The Netherlands
Contact:

Re: String functions in C++

Post by vuurrobin » Thu Dec 03, 2009 8:20 pm

all c++ libraries are supported, but most of them should be avoided because they are bloated.

anyway, this works for me:

Code: Select all

#include <cstdio>
#include <string>

#include <nds.h>

int main()
{
	consoleDemoInit();

	const char* test = "this is a test";
	std::string test2 = "\nthis to is a test";

	iprintf(test);
	iprintf(test2.c_str());

	while(true)
	{
		swiWaitForVBlank();
	}

	return 0;
}

elnanni
Posts: 23
Joined: Sat Nov 14, 2009 1:41 am

Re: String functions in C++

Post by elnanni » Fri Dec 04, 2009 4:54 pm

sprintf :D to change the char array :D. Hope it helps, any way, as I used it, I had to set the lenght at the begining, but it's posible to change the lenght during execution:


char sText[30] = "Hola";

...

sprintf(sText, "Text");

...

You can also write variables in it:

char sText1[30] = "Hola";
char sText[30] = "";
...
sprintf(sText, "%s %d", sText1, 10);
...

And to change its size just read about the malloc function.

Hope it helps, regards.

jcvamp
Posts: 6
Joined: Sat Nov 21, 2009 7:55 pm

Re: String functions in C++

Post by jcvamp » Tue Dec 08, 2009 12:52 am

Thanks for the responses. '#include <cstdio>' and '#include <string>' cause errors. I'll try the other example.

User avatar
vuurrobin
Posts: 222
Joined: Fri Jul 11, 2008 8:49 pm
Location: The Netherlands
Contact:

Re: String functions in C++

Post by vuurrobin » Tue Dec 08, 2009 9:04 am

make sure you named the extention .cpp and not .c

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests