Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   trouble reading string into array (http://www.programmingforums.org/showthread.php?t=7494)

Intimidat0r Dec 10th, 2005 5:40 PM

trouble reading string into array
 
so i've got this string called code, of type std::string, which has this inside it

Quote:

1
2
3
4
and yes i've verified this by using cout. now im trying to read each line into a seperate element of the array statements, which is also of type std::string. when i run the below code however:

:

int last_flushed = 0;
std::string statements[1000];
int array_pos = 0;
for (int i = 0; i <= code.size(); i++)
{
        if (code[i] == '\n')
        {
                statements[array_pos] = string_copy(code, last_flushed, i - last_flushed);
                cout << statements[array_pos];
                array_pos++;
                last_flushed = i;
        }
}


it just prints out this:

Quote:

1

2
any idea why this is happening? ive been staring at this for a day or so, i feel really stupid right now. thanks in advance for your help.

DaWei Dec 10th, 2005 5:47 PM

You are using a lot of things there which you neither show nor define nor explain. Crystal balls are nice, but we don't all have one. Would you care to think about your post, think about your problem, think about your audience, and elucidate?

Ooble Dec 10th, 2005 5:50 PM

I have no idea what you're trying to do here. Can you post some more code?

coldDeath Dec 10th, 2005 5:51 PM

I tried reading through that code and i hit dead ends that could be vital to explaining whats wrong.
For example code.size() in the for loop.

Intimidat0r Dec 10th, 2005 6:08 PM

I'm sorry, I forgot to post string_copy()'s definition:

:

string string_copy(string source, int startpos, int length)
{
        string final;
        final = "";
       
        for (int i = startpos; i <= length; i++)
                final += source[i];
       
        return final;
}


the idea with that function is to return a 'length' substring of 'source', starting at 'startpos'.

the idea of all this was to take a string containing several lines, each seperated by a newline (\n) and put each line into a seperate element of the array statements.

the variable last_flushed is so it puts a substring of 'code', between last_flushed (the end of the last string put into the array) and i (the position of the next \n) and not everything from the beginning to whatever newline it encounters.

@coldDeath: size() is a member of std::string. code.size() returns the length of the string 'code'.

coldDeath Dec 10th, 2005 6:10 PM

Ty, I couldn't see any variables called code, so i got a little confused.

Ooble Dec 10th, 2005 6:17 PM

Instead of string_copy, why not just do this?
:

std::string myNewString = myOldString.substr(offset, length);
They think of these things, y'know. ;)

Intimidat0r Dec 10th, 2005 6:28 PM

coldDeath: np

Ooble: thanks, i got it working. yay.


All times are GMT -5. The time now is 8:55 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC