Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 15th, 2006, 8:09 PM   #1
Jinx
Newbie
 
Join Date: Oct 2006
Posts: 11
Rep Power: 0 Jinx is on a distinguished road
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
Jinx is offline   Reply With Quote
Old Oct 15th, 2006, 8:19 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Oct 15th, 2006, 8:59 PM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 879
Rep Power: 4 The Dark is on a distinguished road
When you find the pos again inside the loop, you are looking from the start again. Change:
int pos = text.find('\n');
to:
int pos = text.find('\n', spot);

also, you need to start looking after the current \n, so change:
spot=pos;
to
spot=pos+1;

As DaWei said, use code tags and if it still doesn't work, say what is going wrong.
The Dark is offline   Reply With Quote
Old Oct 15th, 2006, 9:01 PM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 879
Rep Power: 4 The Dark is on a distinguished road
Oh, and in this line
board.push_back (text.substr(spot,pos));
pos is the position in the string, starting from spot, so you would want to use pos-spot as the amount of characters to copy.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:12 AM.

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