Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » Renegade Discussions » Mod Forum » It don't mean a thing, if you aint got that string! (C++ help needed)
It don't mean a thing, if you aint got that string! [message #421101] Sun, 28 February 2010 06:23 Go to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
I have a std::string (oh noes, not std!). And I want to tell if the first character in that string is an ampersand (&).
The std::String variable is strd.

This conditional returns true always, even if the first character is not an ampersand:

if(strd[0] == '&'){


So I thought I'd try this:

const char *character = (const char *)strd.at(0);
printf("%s\n",strd.c_str());
printf("%c\n",strd[0]);
printf("%c\n",character);
if(strstr(character, "~")){


The printf's print the following:

Quote:


#lobby
#
#



Then at the strstr it crashes (I guess because it's supposed to be comparing strings, not characters).

Can someone suggest an appropriate way to check if the first character of a std::string equal &, please?



Re: It don't mean a thing, if you aint got that string! [message #421103 is a reply to message #421101] Sun, 28 February 2010 08:18 Go to previous messageGo to next message
BlueThen is currently offline  BlueThen
Messages: 2402
Registered: February 2006
Karma: 0
General (2 Stars)
I believe compare() is used for matching strings, http://www.cplusplus.com/reference/string/string/compare/

Re: It don't mean a thing, if you aint got that string! [message #421104 is a reply to message #421101] Sun, 28 February 2010 08:21 Go to previous messageGo to next message
Tunaman
Messages: 1190
Registered: January 2005
Karma: 2
General (1 Star)
Did you try using just a char instead of a pointer to a char?

http://img694.imageshack.us/img694/9055/tunamanlmao.png
Re: It don't mean a thing, if you aint got that string! [message #421105 is a reply to message #421101] Sun, 28 February 2010 08:23 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
if((strd.c_str())[0] == '&'){


Perhaps? (NB: I have not tried to compile that, but it *should* work in theory... the brackets around the strd.c_str() may or may not be required depending on the other of precedence.)

Basically you should get the underlying c_str and then get the first index of THAT array instead.


http://steamsignature.com/card/1/76561197975867233.png
Re: It don't mean a thing, if you aint got that string! [message #421113 is a reply to message #421105] Sun, 28 February 2010 10:41 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
danpaul88 wrote on Sun, 28 February 2010 10:23

if((strd.c_str())[0] == '&'){


Perhaps? (NB: I have not tried to compile that, but it *should* work in theory... the brackets around the strd.c_str() may or may not be required depending on the other of precedence.)

Basically you should get the underlying c_str and then get the first index of THAT array instead.



Cheers Dan, I did try: strd.at(0).c_str() which was my attempt at trying what you just posted, but it's obviously invalid. I'm at work so cannot try what you just wrote, but will give it a go when I get in.



Re: It don't mean a thing, if you aint got that string! [message #421139 is a reply to message #421101] Sun, 28 February 2010 17:12 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
Thanks, that worked. Smile


Re: It don't mean a thing, if you aint got that string! [message #421155 is a reply to message #421101] Sun, 28 February 2010 20:13 Go to previous message
Khyron263 is currently offline  Khyron263
Messages: 32
Registered: September 2009
Karma: 0
Recruit
damnit! now that songs stuck in my head!
Previous Topic: XCC Mixer?
Next Topic: Waterfall problems
Goto Forum:
  


Current Time: Mon Dec 30 03:30:49 MST 2024

Total time taken to generate the page: 0.00706 seconds