![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2006
Posts: 11
Rep Power: 0
![]() |
String Vector Error
I am trying to convert a large string with multiple lines to a vector of strings
----------- int spot=0; vector<string> board; while ( text.npos != text.find('\n',spot)) { int pos = text.find('\n'); board.push_back (text.substr(spot,pos)); spot=pos; } ------------ Was what I as trying but it doesn't seem to be working? Any suggestions? lets say text equals 20 BLUE apple GREEN square GREEN apple BLUE apple BLUE square Last edited by Jinx; Oct 15th, 2006 at 8:48 PM. Reason: Asked to |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Post more code, that's insufficient. Put it in code tags, please, and read the forum's faq/rules regarding that issue, and more.
__________________
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 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 879
Rep Power: 4
![]() |
When you find the pos again inside the loop, you are looking from the start again. Change:
int pos = text.find('\n');int pos = text.find('\n', spot);also, you need to start looking after the current \n, so change: spot=pos; spot=pos+1; As DaWei said, use code tags and if it still doesn't work, say what is going wrong. |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 879
Rep Power: 4
![]() |
Oh, and in this line
board.push_back (text.substr(spot,pos)); |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C# corruption!!! | Kilo | C++ | 32 | May 21st, 2006 9:44 PM |
| Array issues :( | Alo Tsum | Java | 10 | Nov 26th, 2005 6:45 PM |
| A standards question, optional inputs into Methods | Arla | C# | 4 | Apr 27th, 2005 11:38 PM |
| replace space with ' * ' | TecBrain | C++ | 15 | Apr 13th, 2005 1:32 PM |
| function | solomon_13000 | Java | 6 | Apr 3rd, 2005 12:42 AM |