![]() |
|
|
#1 |
|
Eligible for custom title
Join Date: May 2007
Location: c:\programs\kitteh.exe
Posts: 6,152 (2.80/day)
Thanks: 724
Thanked 556 Times in 461 Posts
|
Strings and Linked lists in C++
Working on a basic text editor in c++ using linked lists
i have a method that that accesses a class method called add that accepts words froma stringt for example "the aflac duck" gets broken into three items by insert and and get added to a link list by insert. running on the assumption so far that each word has 1 space between it. Code:
void insert(string w1)
{
string w2="";
char c1;
string::iterator it;
for (it=w1.begin(); it < w1.end(); it++)
{
c1 = *it;
if( c1 == ' ')
{
list.add(w2);
w2="";
}
else
{
w2 = w2 + c1;
}
}
}
Code:
insert(st1); as opposed to if i use [code]insert("the aflac duck"); i do a readback and get the whole string ... (the aflac duck) what am i doing wrong
__________________
Rig 1+1 Athlon XP 2200+, MSI KM2M Combo, ATI 9200SE 128 MB DDR, 2 X 512 MB DDR333, 250GB + 80 HDD? “try intel cpu, amd is only good for going to nude sites” -firehawkxd
“go for the 5850 now and play games while the nvidiots wait for the ceo to show an actual working product” -W1zzard
“An MSI logo? This offends my retina. I await your apology.” -MRCL
www.autolounge.com.jm |
|
|
|
|
|
#2 |
![]() Join Date: Mar 2011
Location: Australia
Posts: 246 (0.31/day)
Thanks: 6
Thanked 31 Times in 28 Posts
|
You don't have to check every character yourself until it is a space, try using stringstream from C++'s standard library:
#include <sstream> Create the stringstream object, initialise it with the input string, and then do a while loop as long as the stream has character (stringstream.good()) and perform a getline using ' ' (space) as delimiter, then add the string obtained from getline() to your linked list. I can give you a sample completed code if you need it. (Just to make it easier for you, getline() takes in input stream, string parameter to output line to, and optionally delimiter) |
|
|
|
| The Following User Says Thank You to Zyon For This Useful Post: |
|
|
#3 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,131 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
How are you telling it to display the separated string?
Could you inadvertantly be just displaying at the first string in the list and they are actaully all there? Did you get my PM about converting to c-string and tokenizing?
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
| The Following User Says Thank You to Kreij For This Useful Post: |
|
|
#4 |
|
Eligible for custom title
Join Date: May 2007
Location: c:\programs\kitteh.exe
Posts: 6,152 (2.80/day)
Thanks: 724
Thanked 556 Times in 461 Posts
|
thx guys. i got ur pm kreij.
i tried tokenizing but to no avail
__________________
Rig 1+1 Athlon XP 2200+, MSI KM2M Combo, ATI 9200SE 128 MB DDR, 2 X 512 MB DDR333, 250GB + 80 HDD? “try intel cpu, amd is only good for going to nude sites” -firehawkxd
“go for the 5850 now and play games while the nvidiots wait for the ceo to show an actual working product” -W1zzard
“An MSI logo? This offends my retina. I await your apology.” -MRCL
www.autolounge.com.jm |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linked or unlinked? | ste2425 | Overclocking & Cooling | 10 | May 13, 2010 01:28 PM |
| ASUS Lists AM3 CPU-supportive AM2 and AM2+ Motherboards | btarunr | News | 52 | Nov 16, 2008 07:39 AM |
| nvidia 8800gt sub id strings | Rebo&Zooty | NVIDIA | 0 | May 19, 2008 05:22 AM |
| Memory linked or unlinked? | CarolinaKSU | Motherboards & Memory | 2 | Mar 4, 2008 02:11 PM |
| AMD lists FX-60 and X2 5000+ | live2game2003 | News | 2 | Oct 26, 2005 01:56 AM |