![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | ||
|
Hobbyist Programmer
|
trouble reading string into array
so i've got this string called code, of type std::string, which has this inside it
Quote:
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:
__________________
Children in the dark cause accidents, and accidents in the dark cause children. http://www.ronincoders.org |
||
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I have no idea what you're trying to do here. Can you post some more code?
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
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.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
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'.
__________________
Children in the dark cause accidents, and accidents in the dark cause children. http://www.ronincoders.org |
|
|
|
|
|
#6 |
|
Expert Programmer
|
Ty, I couldn't see any variables called code, so i got a little confused.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Instead of string_copy, why not just do this?
std::string myNewString = myOldString.substr(offset, length); ![]() |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
|
coldDeath: np
Ooble: thanks, i got it working. yay.
__________________
Children in the dark cause accidents, and accidents in the dark cause children. http://www.ronincoders.org |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|