Need some string help!
OK, ok, you people are getting sick of me I know. I've actually gotten pretty far after bombing out with CString. Now I have two more questions:
One
===
I'm using CharNext() to get the next and then the next character in the string. The problem is, what function do I use to extract the first character? If I do this:
char text[6] = "hello";
char *str = CharNext(text);
TextOut(gdc, 10, 10, str, sizeof(str));
then I get "e". How do I get "h"??
Two
===
This is related to the last one - how can I gt rid of the garbage at the end of a string? for isntance, if I were to do this:
char myString[30];
sprintf(myString, "Hello there kiddies!");
char *str = CharNext(myString);
for (int x=0; x<=sizeof(myString); x++)
{
TextOut(gdc, x*10, 10, str, 1);
str = CharNext(str);
}
I'd get a printout of:
ello there kiddies!|||||||||||
How can I get rid of the junk (the "|") at the end of the string. I tried memset, that didn't work. Any suggestions? Thanks.
==============================
\\// live long and prosper; \||/ die short and rot.
==============================
Edited by - Gaiiden on 10/1/00 2:06:21 PM
Drew Sikora
Executive Producer
GameDev.net
Ok Gaiiden. I first want to say that I have never used the CString class (i only used MFC once, during college), BUT I do have the MSDN help files installed on my computer. So I simply typed in ''CString'' to the index - double clicked on the main entry - then double clicked on "CString Class Members" to bring up a list (with explanations) of the CString member functions.
For the first question (about CharNext). It appears to me that you should be using either the function GetAt() of the overloaded operator[]() both of which do the same thing.
For the second question (about the junk). You are using the wrong function for size. To get the string length use the function Length() .. i assume size must return the length of the allocated buffer or something.
Well ... good luck ... and BTW .. if you do not have both the MSDN library and the Borland C++ Builder Help files ... get them .. they rock ... MSDN is a (MUCH) better reference ... Borland is a better teacher (index is useless - but sections that desribe usage are wonderful - and great sample code embedded directly in help topic).
For the first question (about CharNext). It appears to me that you should be using either the function GetAt() of the overloaded operator[]() both of which do the same thing.
For the second question (about the junk). You are using the wrong function for size. To get the string length use the function Length() .. i assume size must return the length of the allocated buffer or something.
Well ... good luck ... and BTW .. if you do not have both the MSDN library and the Borland C++ Builder Help files ... get them .. they rock ... MSDN is a (MUCH) better reference ... Borland is a better teacher (index is useless - but sections that desribe usage are wonderful - and great sample code embedded directly in help topic).
If you insist on using CharNext(), you could just do:
If you''re trying to output a whole string with GDI, you don''t need a for loop to output every individual character. You can do the whole string at once, like this:
Alex
bigshot@austin.rr.com
The only way to get on your feet is to get off your ass.
char *str = CharNext(text-1);
If you''re trying to output a whole string with GDI, you don''t need a for loop to output every individual character. You can do the whole string at once, like this:
TextOut(gdc, 10, 10, myString, strlen(myString));
Alex
bigshot@austin.rr.com
The only way to get on your feet is to get off your ass.
Alexbigshot@austin.rr.comFoolish man give wife grand piano. Wise man give wife upright organ.
BigShot was right ... the help file says a CString can be used freely in place of char* and const char* values. So just pass the string directly in place of the str variable.
Thanks a lot guys!! Xai, I didn''t need any info on CString, so the GetAt() function was useless. I try to stay away from MFC. But the Length() function gave me the idea to use strlen(), which does work, thanks.
As for Mr. Bigshot, he sorta screwed me up for a minute or two. For anyone else peeking in, this is the correct way to get the first character using CharNext():
Also, I know I can print text like that, I just did the TextOut() so I knew I was getting every character out of the string with no errors (like the garbage at the end), but thanks for the concern.
Anyways, thanks to all. Now I shall resume my programming. Expect another question within a half-hour. Haha, just kidding.... maybe.
==============================
\\// live long and prosper; \||/ die short and rot.
==============================
As for Mr. Bigshot, he sorta screwed me up for a minute or two. For anyone else peeking in, this is the correct way to get the first character using CharNext():
char *str = (CharNext(text)-1);
Also, I know I can print text like that, I just did the TextOut() so I knew I was getting every character out of the string with no errors (like the garbage at the end), but thanks for the concern.
Anyways, thanks to all. Now I shall resume my programming. Expect another question within a half-hour. Haha, just kidding.... maybe.
==============================
\\// live long and prosper; \||/ die short and rot.
==============================
Drew Sikora
Executive Producer
GameDev.net
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement