Hello everyone,

I need to load a multi-word strings from a file to a linked list in one of my programs, and i'm not sure how to do that.

my text file looks like this- (the name of the text file is: names.txt)

Linda
Martinho
Marla
Jose
Mary
Luka
Joseph
Thiago
Ruba

here is some of my code so far...

#include <iostream>
#include <cassert>
#include <String>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

template <class TYPE>
struct NODE
{
	TYPE data;											//Data Item
	NODE<TYPE> *next;									
};

template<class TYPE>
class multiword
{
protected:
	int count;											//Variable to store the values of the numbers
	NODE<TYPE> *first;									//pointer to the first NODE
public:	
	class NotFound {};
	void Initialize();									//Initialize the list with no values			
	bool Empty();										//Check if the list is empty
	void Destroy();
	void fillNames(ifstream& inFile, multiword<TYPE>& cList);		//Delete every value of the list
	int binarysearch(const TYPE& item);					//Binary search		
	void additem(const TYPE& add);						//Add a value
	void Insertion();									//Insertion
	void display();										//Display
	multiword();										//Constructor(no-arg)
	~multiword();										//Destructor
};

template<class TYPE>
void multiword<TYPE>::fillNames(ifstream& inFile, multiword<TYPE>& cList)
{
	string firstN;
	int i;

	TYPE temp;
	const int number = 10

	for(i = 0; i < number; i++)
	{
		inFile>>firstN;
		temp.setName(firstN);
		cList.insertAt(i, temp);
	}
}
	
template<class TYPE>									//Check if the list is empty
bool multiword<TYPE>::Empty()
{
	return(first == NULL);
}

template<class TYPE>									//Constructor
multiword<TYPE>::multiword()
{
	first = NULL;
	count = 0;
}

template<class TYPE>									//Destructor
multiword<TYPE>::~multiword()
{
	Destroy();
}
	
template<class TYPE>
void multiword<TYPE>::Insertion()
{
	nodeType<TYPE> *lastInOrder;
	nodeType<TYPE> *firstOutOfOrder;
	nodeType<TYPE> *current;
	nodeType<TYPE> *trailCurrent;

	lastInOrder = first;

	if(first = NULL)
		cerr << "Cannot sort an empty list." << endl;
	else
		if(first->link == NULL)
			cout << "The list of length1, it is already in order." << endl;
		else
			while(lastInOrder->link !=NULL)
			{
				firstOutOfOrder = lastInOrder->link;
				if(firstOutOfOrder->info < first->info)
				{
					lastInOrder->link = firstOutOfOrder->link;
					firstOutOfOrder->link = first;
					first = firstOutOfOrder;
				}
				else
				{
					trailCurrent = first;
					current = first->link;
					while(current->info < firstOutOfOrder->info)
					{
						trailCurrent = current;
						current = current ->link;
					}

					if(current != firstOutOfOrder)
					{
						lastInOrder->link = firstOutOfOrder->link;
						firstOutOfOrder->link = current;
						trailCurrent->link = firstOufOfOrder;
					}
					else
						lastInOrder = lastInOrder->link
				}
			}
}
 

template<class TYPE>
int multiword<TYPE>::binarysearch(const TYPE& item)
{
	int first = 0;
	int last = length -1;
	int mid;

	bool found = false;

	while(first <= last && !found)
	{
		mid = (first + last) / 2;

		if(list[mid] == item)
			found = true;
		else
			if(list[mid] > item)
				last = mid -1;
			else 
				first = mid + 1;
	}
	if(found)
		return mid;
	else 
		return -1;
}

template<class TYPE>									//Destroy every element on the list
void multiword<TYPE>::Destroy()
{
	NODE<TYPE> *temp;									//deallocates memory
	while(first != NULL)								//while there are value in the list
	{
		temp = first;									//set temp to the first value in the list
		first = first->next;							//put first to the next value
		delete temp;									//delete memory
	}
	count = 0;
}

template<class TYPE>									//Initialize the list
void multiword<TYPE>::Initialize()
{
	Destroy();											//If any values are found, remove them.
}

Thank you very much in advance,

Doug

Recommended Answers

All 5 Replies

You need to be just a little more careful with names
Insertion() is a bad name because it is not obvious that it sorts the list. InsertionSort() is a good name.

Destroy() is not a good name because it doesn't destroy your object; it only deletes every item in the list. Name it something like Clear() or MakeEmpty(). By the way, Initialize() has the same logical functionality: both clear the list if not already emtpy. I would get rid of Initialize() and just use Clear() or whatever you name it. Actual initialization already occurs properly in the constructor.

Be careful with your types
In general, you should not assume that TYPE has a member function setName(). If you are just making a linked list of strings, there is no real reason to create a new class just to hold it. Just use std::string. Also, in template declarations, use the word typename instead of class. (Yes, yes, I know... semantics. Even so...)

More on fillNames()
You are trying to do two things here: initialize *this from file and initialize some that (which you named cList). If you need two copies of the multiword list, make a copy constructor and copy it that way; Don't force your user to fill two multiword lists just to load from file.

Also, you are using a member function named insertAt() which you have not defined to exist in the multiword class.

I recommend that you get rid of the cList argument and just load words from file into *this's list.

Lastly, you should not hardcode the number of lines in your file. What if you later want to load a list of names 100 names long? Or two? Just read names and append them to the end of the list as long as there are lines in the file.


Oy, that's enough for now. I haven't looked over your sort or search algorithms... but you seem to have a pretty good start. Work on straightening up those things I mentioned and then come back with compiler errors or what is not working right when you use the class.

Good luck.

Hey,
Thank you for the help...
I will work on those ideas that you gave me.
Doug

Hey,

So I changed a couple things that you told me already, but I have no idea how to do what you told me on the fillname();

Right now it looks like this:

template<typename TYPE>
void multiword<TYPE>::fillNames(ifstream& inFile, multiword<TYPE>& cList)
{
	string firstN;
	int i;

	TYPE temp;
	const int number = 10

	for(i = 0; i < number; i++)
	{
		inFile>>firstN;
		temp.setName(firstN);
		cList.insertAt(i, temp);
	}
}

I'm not sure why I added setName and insertAt. Is there any other way to add the text file without calling other classes such as setname and insertat?

Thank you,

Doug

Think about the types of things you have.

You have a NODE class, which is used by the multiword class to store a list of some unspecified TYPE.

The fillNames() function shouldn't know or care what type of thing TYPE is. Just read it from file and store it.

Give it some thought...


Now, when it is time to use your linked list class, what type of thing are you storing in a list? Does it matter if the multiword class has any idea what type that is? Should I be able to say: multiword<int> my_int_list; or: multiword<bool> my_bool_list; or: multiword<Employee> my_employee_list; (where Employee is some class defined elsewhere with the >> and << I/O operators properly overloaded)

Think about it.

i have these problem and i need some one to answer it please.....


Develop menu driven C++ program to manage the College of Science Book Store. Your program should maintain three linked lists: one for books, another one for students and a third one for the loans transactions. When your program starts it should read the books information, students information and loans information from three separate files “books.txt”, “students.txt” and “loans.txt”. An example format of theses files is shown below. You need to:
1. Design and implement a Book structure to encapsulate a book object. Each book has title, author, ID and number of copies. Assume book IDs are unique.
2. Design and implement a BookList class to encapsulate a linked list of Book objects. Provide all necessary functions.
3. Design and implement a Student structure to encapsulate a student object. Each student has name and ID. Assume student IDs are unique.
4. Design and implement a StudentList class to encapsulate a linked list of Student objects. Provide all necessary functions.
5. Design and implement a Loan structure to encapsulate loan transactions in the book store. A Loan object has the Book ID, the Student ID, and the semester when the book is borrowed (e.g. SP10).
6. Design and implement a LoanList class to encapsulate a linked list of Loan objects. Provide all necessary functions.
7. Your program should provide a menu that would allow for the following functionalities:
a. Adding and deleting a book.
b. Adding and deleting a student.
c. Borrowing a book: this means creating a new node in the LoansList with the student ID and the Book ID.
d. Returning a book: this means deleting the node from the LoansList with the specific Student ID and Book ID.
e. List all books borrowed by a specific student.
f. List all students borrowing a specific book.
g. List all books ordered by ID, ascending and designingly. Use recursive functions.
h. List all students ordered by name.
8. Use operator overloading as necessary and other OOP rules and concepts.
9. Handle all errors using appropriate exception handling techniques. Errors include, but are not limited to, no copies to lend a book, no student with a given id/name, no book with a given id/name, missing or wrong information in the input files.
10. When the user chooses to quit the program, you should write the contents of all lists to data files.


Books.txt

Book 1 title
Book 1 author
Book 1 ID
Book 1 copies
Book 2 title
Book 2 author
Book 2 ID
Book 2 copies

Students.txt

Student 1 name
Student 1 ID
Student 2 name
Student 2 ID

Loans.txt

Student 1 ID
Book 1 ID
Semester 1
Student 2 ID
Book 2 ID
Semester 2

/////
i hope some one help me.....

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.