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 / newbish question about chars and strings

Author
Message
bloodmage2
15
Years of Service
User Offline
Joined: 14th Jun 2009
Location:
Posted: 16th Aug 2009 20:51
i have a few questions about strings a char's. before i used c++, i used action script, where a string was a simple variable where you could add and subtract characters, but in c++, there are no strings, and chars are like arrays, but stranger.
1. is there any way to make a string of characters with an indefinite length, one that can be changed
2. is there any kind of way to see if a string is located within another, i.e. search a file name for an extension, if not, place the extension at the bottom
3. *pardon me for yelling* WHY THE **** IS THERE NO STRING TYPE?
4. is there any simple way of taking a string of numerical characters, and turning that into a int/float/double, and vice versa?
5. what is the difference between char and char * ?

-to the optimist, the glass is half full. to the pessimist, it is half empty, to the engineer, it is twice as big as it needs to be.
heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 16th Aug 2009 20:58 Edited at: 16th Aug 2009 20:59
There is a string class. Use:

and using the std class you can find other cool stuff like:

for file input/output.

And for your last question I'm not exactly sure in terms of programming but I know that a char has a set size and a char* doesn't, and a char* is more like a string but it still lacks things that the string class has.

Use Google first... it's not rocket surgery!
prasoc
16
Years of Service
User Offline
Joined: 8th Oct 2008
Location:
Posted: 16th Aug 2009 22:07
You need to #include <string> first


Your signature has been erased by a mod
heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 16th Aug 2009 22:11
Whoops forgot about that, and if you wanted to use the std::fstream you needed to do:


Use Google first... it's not rocket surgery!
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 16th Aug 2009 22:40
Quote: "WHY THE **** IS THERE NO STRING TYPE"


The reason there is no built in type is because no one string type would suit every situation. The standard template library provides one possible string type (That is the std::string).

C++ is a native language. The compiler cannot do things hidden to the programmer. The concept of a string is not one of the atomic types, and so it can't be a built in part of the language like it is in other programming languages.

Quote: "is there any simple way of taking a string of numerical characters, and turning that into a int/float/double, and vice versa?"


Here are the functions to convert between strings and numeric types:


Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 17th Aug 2009 12:57 Edited at: 17th Aug 2009 13:06
Quote: "1. is there any way to make a string of characters with an indefinite length, one that can be changed"


If you use the std::string type, you don't need to worry about length much. The functions of this class take care of changing the length dynamically as needed.
(Note that in character arrays, the null terminator signifies the end of the string, so you can actually change the length by replacing the null terminator, although the maximum size of the allocated memory does not change in this case.)

Quote: "2. is there any kind of way to see if a string is located within another, i.e. search a file name for an extension, if not, place the extension at the bottom"


Yes, again the std::string class has several forms of "find" functions to search a substring and it also has "replace" and "append" functions to change or add extension, for example.

Quote: "5. what is the difference between char and char * ?"


- "char" is one character.
- "char[5]" is an array of 5 characters (which would include a usable length of 4 characters, by the way, because length needs to include the null terminator).
- "char *" is a pointer to a character, or to an array of characters. If you don't know what a pointer is, read a C++ language reference because the language relies very often on the use of pointers.

When you use Dark GDK, you sometimes need character pointers, for example to write text to the screen you use dbText which expects a char pointer. Where a "char*" is required, you can either:
- pass a constant string to the function, like "dbText(0,0,"write this");
- pass the name of a char array to the function, in this case it takes the pointer to the first character of the array,
- when using std::string, pass the c_str() parameter of the string which gives you the pointer, but in this case there is a need for typecast (easy) because c_str() is constant and the Dark GDK functions expect non-constant parameter,
- pass a char* variable to the function.

Note that you probably don't want to define char* variables yourself too much, because if you do that, you need to allocate the memory for your string, and also take care of de-allocating it when it is no longer needed. Using fixed-size char arrays or the std::string type, you don't have to worry about memory allocation. On the other hand, dynamic memory allocation can be useful if there are so many strings that they take up a lot of memory.

Also, using char arrays is not as horrible as it seems at first sight. There are built-in string handling functions (e.g. strcat, strcpy, strcmp) which work well with character arrays. It's just a question of getting used to the concept, and a question of style if you prefer char arrays or strings.

The question of converting strings to numbers and back has already been answered above. You can do it with those macros, or it can also be done with string streams, or with sprintf / sscanf, or you can even write your own functions.
bloodmage2
15
Years of Service
User Offline
Joined: 14th Jun 2009
Location:
Posted: 17th Aug 2009 19:16 Edited at: 17th Aug 2009 19:55
ok, thanks for all the help. one last thing, if i put a string into one of the GDK functions that require a char pointer, will it spit an error back at me?

edit: sorry i didn't see that part of the post, but i tried the c_str(), but the compiler claims it doesn't exist.

-to the optimist, the glass is half full. to the pessimist, it is half empty, to the engineer, it is twice as big as it needs to be.
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 17th Aug 2009 19:17
Not if you pass .c_str()... then cast to char* because GDK isn't const correct in the slightest(throws poo at whoever wrote it).

bloodmage2
15
Years of Service
User Offline
Joined: 14th Jun 2009
Location:
Posted: 17th Aug 2009 20:06
ok, i found why it didn't accept .c_str() (forgot the ()). i tried casting, but i get the error "type char * is unexcpected". casting with char's is no different than casting with int's/float's, right? it would be char*(myString.c_str()), right?

-to the optimist, the glass is half full. to the pessimist, it is half empty, to the engineer, it is twice as big as it needs to be.
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 17th Aug 2009 20:17
(char*)myString.c_str()

You put the cast inside parenthesis, and it's optional to have the variable also in parenthesis.

Your_Health = (My_Mood == HAPPY) ? 100 : NULL;
bloodmage2
15
Years of Service
User Offline
Joined: 14th Jun 2009
Location:
Posted: 17th Aug 2009 20:22
oh, god,


what in fresh hell is this?

-to the optimist, the glass is half full. to the pessimist, it is half empty, to the engineer, it is twice as big as it needs to be.
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 17th Aug 2009 20:36
Project --> " " Properties --> Configuration properties --> C/C++ --> Code Generation

Set Runtime library to Multi-threaded(/MT).

I think that is it, let me know if its not.
bloodmage2
15
Years of Service
User Offline
Joined: 14th Jun 2009
Location:
Posted: 17th Aug 2009 20:50
it was already at multi-threaded. any other ideas?

-to the optimist, the glass is half full. to the pessimist, it is half empty, to the engineer, it is twice as big as it needs to be.
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 17th Aug 2009 20:55
According to those errors, it's not.

Make sure it's /MT and NOT /MTd

Your_Health = (My_Mood == HAPPY) ? 100 : NULL;
bloodmage2
15
Years of Service
User Offline
Joined: 14th Jun 2009
Location:
Posted: 17th Aug 2009 21:01
ahh, thank you. it works now.

-to the optimist, the glass is half full. to the pessimist, it is half empty, to the engineer, it is twice as big as it needs to be.
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 17th Aug 2009 22:14
i was once asking about string to char* and i was told that there is a problem with just casting with '(char*)string.c_str()'.
if i remember correctly it had something to do with memory leaks or so (?!)
i ended up with this function:


now is this useless and i should just convert with '(char*)string.c_str()' ?
Matty H
16
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 17th Aug 2009 22:23
Ha ha, Branflakes is the one who told me that when I was stuck.

There should be a trouble-shooting sticky because there are a handfull of small issues you see again and again
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 18th Aug 2009 04:45
Quote: "if i remember correctly it had something to do with memory leaks or so (?!)"


No, although your function will cause a memory leak if you directly pass that to any GDK function that takes a char* argument because your delete [] is never called(there's a return just before it) and you'd have no way of calling it unless you stored the return ptr and freed it yourself. GDK to my knowledge doesn't modify the contents of any char* you pass, it merely reads it or copies it thus casting away the const-ness is fine.

_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 18th Aug 2009 10:20
oh you're right, the 'delete' is never even called
ok, thanks, i'll be using just '(char*) my_string.c_str()' from now on...

Login to post a reply

Server time is: 2024-11-20 17:24:12
Your offset time is: 2024-11-20 17:24:12