Hi all,

I wish to construct an text editor with linear link list. Now I am writing a function to add a sentence into it and display all. My problem is I can only input a string as it will not take whitespace as input.
And I don't understand what is load a text file in stx format..

Thanks!

Here is my header file

#ifndef EDITORHEAD
#define EDITORHEAD

#include "ListException.h"
#include "ListIndexOutOfRangeException.h"

typedef string ListItemType;

class InnerList {
	public:
		//Constructor
		InnerList ();
		//Copy Constructor
		InnerList (const InnerList& aList);
		//Destructor
		~InnerList ();
		
		bool isEmpty() const;
		int getLength() const;
		
		void insert(int index, const ListItemType& newItem)
		throw(ListIndexOutOfRangeException, ListException);

	private:
		//A structure which holds the data for each node
		struct Node {
			//Declare the item type in the list
			ListItemType item;
			//Give a pointer to the next node in the list
			Node* next;
		};
		int size;
		//A pointer which will permanently point to the start of the list
		Node* head;
};

#endif

My function

#include <iostream>    // Only for displaying copy constructor
#include <fstream>     // for file I/O
#include <cstddef>     // for NULL
#include <new>         // for bad_alloc
#include "EditorHead.h"     // header file
using namespace std;

InnerList::InnerList() : size( 0 ), head(NULL) { }

InnerList::~InnerList() {
	while (!isEmpty())
	remove(1);
}  // end destructor


InnerList::InnerList(const InnerList& aList)
	: size(aList.size)
{
   cout << "Copy Constructor" << endl;
   if ( aList.head == NULL )
   {
      head = NULL;  // original list is empty
   }
   else
   {
      // copy first node
      head = new Node;
      head->item = aList.head->item;

      // copy rest of list
      Node *newPtr = head;  // new list pointer
      // newPtr points to last node in new list
      // origPtr points to nodes in original list
      for (Node *origPtr = aList.head->next;
	   origPtr != NULL;
	   origPtr = origPtr->next)
      {
         newPtr->next = new Node;
         newPtr = newPtr->next;
	 newPtr->item = origPtr->item;
      }  // end for

      newPtr->next = NULL;
   }  // end if
}  // end copy constructor

bool InnerList::isEmpty() const
{
   return size == 0;
}  // end isEmpty

int InnerList::getLength() const
{
   return size;
}  // end getLength

void InnerList::insert(int index, const ListItemType& newItem)
   throw(ListIndexOutOfRangeException, ListException)
{
	int newLength = getLength() + 1;
	
	if ( (index < 1) || (index > newLength) )
		throw ListIndexOutOfRangeException("ListIndexOutOfRangeException: insert index out of range");
	else {
		// try to create new node and place newItem in it
		try
		{
			Node *newPtr = new Node;
			size = newLength;
			newPtr->item = newItem;
			// attach new node to list
	 		if (index == 1)		{  
				// insert new node at beginning of list
				newPtr->next = head;
				head = newPtr;
			}
			else {
				Node *prev = find(index-1);
				// insert new node after node
				// to which prev points
				newPtr->next = prev->next;
				prev->next = newPtr;
			}  // end if
		}  // end try
		catch (bad_alloc e)	{
			throw ListException(
				"ListException: memory allocation failed on insert");
		}  // end catch
	}  // end if
}  // end insert

And this is my main function

#include <iostream>
#include <fstream>
#include <iomanip>
#include "EditorHead.h"
using namespace std;

int main ()
{
    InnerList aList;
    ListItemType ch, content;
    char choice;
    bool done = false;

    do
    {
        system("cls");
        cout << endl << endl << endl;
        cout << "          2. Add a Paragraph " << endl;
        cout << "          8. Display ALL Records" << endl;
        cout << "        => ";
        cin >> choice;
        choice = toupper(choice);
        
        string ch, yesorno;
        switch( choice )
        {
            case '2' :
                cout << "Character to add to back => ";
                cin >> ch;
                aList.insert(aList.getLength() + 1,ch);
                break;

            case '8' :
            	cout << "Data for All Records" << endl;
                cout << "--------------------------------" << endl;
                for (int i=1; i<=aList.getLength(); ++i)
                {
                    aList.retrieve (i, content);
                    cout << content;
                }
                cout << endl;
                cout << "--------------------------------" << endl;
                break;
                
            case 'Q' : 
            	done = true;
            	break;

            default : cout << "Invalid choice" << endl;
        }

        system("pause");
    } while ( !done );
}

The problem may be that you have forgotten to remove the skipws (skip white space) flag from the stream. e.g.

std::cin.unsetf(std::ios::skipws);

If that is the case, then please also remember to return the stream to the state
you had previously. This is facilitated by setf() and unsetf() because both return
the state of the flags before the call e..g

std::ios::fmtflags flagIO=std::cin.unsetf(std::ios::skipws);

actually I am thinking using getline() instead of cin.. but if getline then i need to change my whole thing in array?? become array base link list??

i used getline. thanks for the reply~

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.