Page 1 of 1

String functions in C++

Posted: Thu Dec 03, 2009 7:52 pm
by jcvamp
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.

Re: String functions in C++

Posted: Thu Dec 03, 2009 8:20 pm
by vuurrobin
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;
}

Re: String functions in C++

Posted: Fri Dec 04, 2009 4:54 pm
by elnanni
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.

Re: String functions in C++

Posted: Tue Dec 08, 2009 12:52 am
by jcvamp
Thanks for the responses. '#include <cstdio>' and '#include <string>' cause errors. I'll try the other example.

Re: String functions in C++

Posted: Tue Dec 08, 2009 9:04 am
by vuurrobin
make sure you named the extention .cpp and not .c