1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Strings and Linked lists in C++

Discussion in 'Programming & Webmastering' started by [I.R.A]_FBi, Mar 19, 2011.

  1. [I.R.A]_FBi

    [I.R.A]_FBi New Member

    Joined:
    May 19, 2007
    Messages:
    7,664 (2.79/day)
    Thanks Received:
    540
    Location:
    c:\programs\kitteh.exe
    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
     
  2. Zyon

    Zyon New Member

    Joined:
    Mar 18, 2011
    Messages:
    264 (0.20/day)
    Thanks Received:
    29
    Location:
    Australia
    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)
     
    [I.R.A]_FBi says thanks.
  3. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.87/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    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?
     
    [I.R.A]_FBi says thanks.
  4. [I.R.A]_FBi

    [I.R.A]_FBi New Member

    Joined:
    May 19, 2007
    Messages:
    7,664 (2.79/day)
    Thanks Received:
    540
    Location:
    c:\programs\kitteh.exe
    thx guys. i got ur pm kreij.

    i tried tokenizing but to no avail
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page