• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

Strings and Linked lists in C++

Joined
May 19, 2007
Messages
7,662 (1.16/day)
Location
c:\programs\kitteh.exe
Processor C2Q6600 @ 1.6 GHz
Motherboard Anus PQ5
Cooling ACFPro
Memory GEiL2 x 1 GB PC2 6400
Video Card(s) MSi 4830 (RIP)
Storage Seagate Barracuda 7200.10 320 GB Perpendicular Recording
Display(s) Dell 17'
Case El Cheepo
Audio Device(s) 7.1 Onboard
Power Supply Corsair TX750
Software MCE2K5
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;
         }
     }
}

why if for example i do

Code:
insert(st1);

where st1 is a string do i get "the" when i do a readback

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
 
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)
 
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?
 
thx guys. i got ur pm kreij.

i tried tokenizing but to no avail
 
Back
Top