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 / Decoding a string into smaller parts.

Author
Message
Freedom Fighters
15
Years of Service
User Offline
Joined: 2nd Nov 2009
Location:
Posted: 11th Apr 2010 07:58
ok im trying to work on map system that reads from a text file.
and from each bit i can assign to a variable
example data
/////////////////////
3|32|45|
23|32|23|
55|4|55|
12|3|77|
etc....
......
/////////////////////
so for example i read the first line and i want to break the string up into smaller part so Var[1] = 3, Var[2] = 32 and so on until it get the last part then reads a new line.

for the coding bit this is what i could come up with:


But i when i try it with a test with this code:



i get these errors:


can you point out to me how i can fix these errors and improve my code?

Problem Solution That Never Fails: "Build A Bridge And Get Over It"
Cuddle Bunniezzz 12
15
Years of Service
User Offline
Joined: 14th Jan 2009
Location: Buffalo, NY
Posted: 11th Apr 2010 08:18 Edited at: 11th Apr 2010 08:21
Um, when passing the "char breaker" argument, you need to use single quotes instead of double quotes. When you use double quotes with c style strings, C++ automatically adds that null terminator thingy ('\0');



If your afraid your going to use double quotes, use "char* breaker" instead of "char breaker" for the argument.

http://www.darkgdk.us/ <- You can now submit pages, upload images, yet were lacking content. We need your help!
Freedom Fighters
15
Years of Service
User Offline
Joined: 2nd Nov 2009
Location:
Posted: 11th Apr 2010 08:29
ok, i never had any idea that the double quotes and single quotes had any meaning to them. ill have to keep that in mind.

ok. well as for returning that part in the string what is the best way i can return it then convert it to a int?

im not sure my way or returning the part of the string is correct.

Problem Solution That Never Fails: "Build A Bridge And Get Over It"
Cuddle Bunniezzz 12
15
Years of Service
User Offline
Joined: 14th Jan 2009
Location: Buffalo, NY
Posted: 11th Apr 2010 17:47 Edited at: 11th Apr 2010 17:50
In the <cstdlib> (a.k.a. <stdlib.h> (that is the old "C" way of used that)), there is a function knows as "atoi()". It can take a string, that has a numerical value in it, and convert it into an integer, it's very useful. Usage example:


There are also things like "itoa()" which converts an int to a char*.

More on "atoi()":
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

And more on the <cstdlib>:
http://www.cplusplus.com/reference/clibrary/cstdlib/

http://www.darkgdk.us/ <- You can now submit pages, upload images, yet were lacking content. We need your help!
Serial Velocity
16
Years of Service
User Offline
Joined: 24th Aug 2008
Location:
Posted: 11th Apr 2010 20:02 Edited at: 11th Apr 2010 20:09
You could use the sscanf function from <stdio.h> like this:


It works the same was as sprintf, but it's the reverse. You could also do something like this:


and varName will equal "Variable" and value will equal 32.

sscanf reference:
http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/

Freedom Fighters
15
Years of Service
User Offline
Joined: 2nd Nov 2009
Location:
Posted: 12th Apr 2010 04:55
Quote: "You could use the sscanf function from <stdio.h> like this:
+ Code Snippet

char *str = "2|12|34";
int values[3];
sscanf( str, "%d|%d|%d", &values[0], &values[1], &values[2] );



It works the same was as sprintf, but it's the reverse. You could also do something like this:
+ Code Snippet

char *str = "Variable = 32";
char *varName;
int value;
sscanf( str, "%s = %d", &varName, &value );



and varName will equal "Variable" and value will equal 32.

sscanf reference:
http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/"


ill probably go with option. ill set some out side 'int' variables and then get my functions to decode the data into them.

Problem Solution That Never Fails: "Build A Bridge And Get Over It"
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 12th Apr 2010 07:54 Edited at: 12th Apr 2010 07:55
Check out str strtok_s() function (WIN32)...



Hope this helps,

JTK

Login to post a reply

Server time is: 2024-11-20 01:29:45
Your offset time is: 2024-11-20 01:29:45