String To Array?

You must understand the Game Editor concepts, before post here.

String To Array?

Postby DilloDude » Thu Jun 22, 2006 10:17 am

I am trying to makke a solution to a problem lots of people have - makeing good conversations. I am trying to write a function that takes a string, and converts it into an array of individual lines. So far, it hasn't worked. So if I have a string which contains "Hello everyone!\nHow are you?\nI am a tree!", it will convert it into an array of strings which contain
[0]:"Hello everyone!"
[1]:"How are you?"
[2]:"I am a tree!"
[3]:""
[4]:""
...
Any suggestions?
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby Novice » Thu Jun 22, 2006 5:54 pm

I think something like this would work:
read text
use a for statement to scan for "\n"
old position=0;
if it runs in to a "\n" get curent position
copy letters from old position to curent position to array[0]
old position = curent position;
update array by 1
resume search at curent position +1
get next "\n" get new position
copy from old position to new position
...
if EOF is reached break out of the function.

This should work if you have the time and the nerve to get the code right. Hope it helped
Why do i always get stuck?
User avatar
Novice
 
Posts: 399
Joined: Mon Aug 29, 2005 10:54 am
Location: Relative
Score: 5 Give a positive score

Postby DilloDude » Thu Jun 22, 2006 11:22 pm

I can use the strncpy function tto copy n characters across, but how can I specify n characters starting from the xth character?
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby DilloDude » Fri Jun 23, 2006 8:02 am

Also, what's the best way to put it in an array? Return it as one or include one as a parameter and set it in the function. And what would be the best the best way to go about it?
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby DilloDude » Sun Jun 25, 2006 7:31 am

So how do I create an array of strings in script editor? I can't figure it out.
If I put
Code: Select all
char array[20][200]
I can get it to work if I say
Code: Select all
char ar[20][200]={"Yo Everyone", "How are you?", "I'm Fine"};

but not if I use the function:
Code: Select all
void str_to_array(char *str, char array[20][200])
{
    char string[200];
    char ch2[1];
    int ch;
    int line;
 
    strcpy(string, str);
 
    for (line = 0; line < 20; line ++)
    {
        strcpy(array[line], "");
        for (; ch < 200; ch ++)
        {
            if (string[ch] != '\n')
            {
                ch2[0] = string[ch];
                strcat(array[line], ch2);
            }
        }
    }
}

How can I fix this?
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby Novice » Sun Jun 25, 2006 8:08 am

I thik you have to use
Code: Select all
char *array [20][200];
Why do i always get stuck?
User avatar
Novice
 
Posts: 399
Joined: Mon Aug 29, 2005 10:54 am
Location: Relative
Score: 5 Give a positive score

Postby DilloDude » Sun Jun 25, 2006 10:49 am

No, doesn't work :(
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby makslane » Sun Jun 25, 2006 10:50 pm

Try:

void str_to_array(char *str, char **array)
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby DilloDude » Mon Jun 26, 2006 3:13 am

It lets me put it in, but when I run it it says there is a problem in line 12:
Code: Select all
strcpy(array[line], "");
it says "READ error in invalid memory area".
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby makslane » Mon Jun 26, 2006 3:32 am

If the array size is [20][200],
make sure line < 20 and the resulting string < 200
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby DilloDude » Mon Jun 26, 2006 8:26 am

Still doesn't work :(
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby makslane » Mon Jun 26, 2006 2:51 pm

Send me the game with subject "String To Array"
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby DilloDude » Tue Jun 27, 2006 1:38 am

Sent it.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby makslane » Tue Jun 27, 2006 2:06 am

To avoid the error, you need create the array, not a pointer.
You are using:

Code: Select all
char **arr;


The array point to nothing.
You need use:

Code: Select all
char arr[20][200];


Now, your str_to_array don't solve the problem.
This new code will works:

Code: Select all
void str_to_array(char *str, char array[20][200])
{
   int line = 0;
   char *start = str;

   while(*str)
   {
      if(*str == '\n')
      {
         //End of line found
         //Copy to array
         *str = 0;
         strcpy(array[line], start);
         *str = '\n';

         //Go to next string
         start = str + 1;
         line++;
      }

      str++;
   }

   //Copy last string
   strcpy(array[line], start);
}
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby DilloDude » Wed Jun 28, 2006 1:38 am

Thanks a lot!
Now I'm gonna figure out how to talk to someone, and I'm gonna post a tutorial here, because I have seen that some people want to do the same thing.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest

cron