Hi guys :)

Thanks first for the help, yesterday.. I really did well in my exam :), thanks a lot again :)

Well, I guess I need a little bit help in this

I have a file, and I want the whole paragraph in the file to be inserted in the LinkList, How can I do it?

This is my code *I'll explain what I did below it*

//######################## Begin Function Main ###########################
int main()
{
	string lib;
	LinkList game;

	cout << "Please, Read the paragraph Carefully.\nAfter the you'll have to fill the suitable word in the suitable place.\n";
	cout << "For example, for each Keyword; name, adverb..etc, Fill in the suitable word.\n\n\n";

	// Ofstream constructor opens file
	ifstream fin( "madlib.txt" );

	// exit program if the file not opened
	if( !fin )
	{
		cerr << "File couldn't be opened.\n";
		exit(1);
	} // end if statement

	while( getline( fin, lib, '\n' ) )
	{
		game.insert( lib );
		cout << lib;
	} // end while loop

	cout << endl;

	return 0;


}
//######################## End Function Main ###########################

Well, first of all I called the file, and I input the data so that It'll output on the screen

then I tried to add this function *game.insert( lib )* I passes it to the LinkList as a Char, but it didn't worked!! and is that right what I did to pass it as a Character?

the compiler said
*error C2664: 'insert' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char*

Well, how I can insert the paragraph in the text file to my LinkList class?


thanks in advance, sorry for taking from your time :)

If you have LinkList::insert( const char * lib ); You have to use it with std::string like

while( getline( fin, lib, '\n' ) )
{
    game.insert( lib.c_str() ); // <- passes the data as const char *
    cout << lib;
}

Whether it really works as intended depends on what the insert() function actually does.

What's LinkList? Is that some compiler-specific library/class? Something you wrote yourself? Same question for the insert function. Where is it? Did you write it yourself? If it's something you wrote yourself, please post it. If not, please explain where it's from. I for one have never heard of it.

Hi guys :)

Thanks first for the help, yesterday.. I really did well in my exam :), thanks a lot again :)

Well, I guess I need a little bit help in this

I have a file, and I want the whole paragraph in the file to be inserted in the LinkList, How can I do it?

This is my code *I'll explain what I did below it*

//######################## Begin Function Main ###########################
int main()
{
	string lib;
	LinkList game;

	cout << "Please, Read the paragraph Carefully.\nAfter the you'll have to fill the suitable word in the suitable place.\n";
	cout << "For example, for each Keyword; name, adverb..etc, Fill in the suitable word.\n\n\n";

	// Ofstream constructor opens file
	ifstream fin( "madlib.txt" );

	// exit program if the file not opened
	if( !fin )
	{
		cerr << "File couldn't be opened.\n";
		exit(1);
	} // end if statement

	while( getline( fin, lib, '\n' ) )
	{
		game.insert( lib );
		cout << lib;
	} // end while loop

	cout << endl;

	return 0;


}
//######################## End Function Main ###########################

Well, first of all I called the file, and I input the data so that It'll output on the screen

then I tried to add this function *game.insert( lib )* I passes it to the LinkList as a Char, but it didn't worked!! and is that right what I did to pass it as a Character?

the compiler said
*error C2664: 'insert' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char*

Well, how I can insert the paragraph in the text file to my LinkList class?


thanks in advance, sorry for taking from your time :)

Sorry I didn't post the whole code, I thought it'll be known for you, sorry again :\

this is the full code

#include <iostream>
#include <fstream>
#include< string>
using namespace std;

// Begin Class Node
class Node
{
public:
	char data;
	Node *link;

	Node ( char x )
	{
		data = x;
		link = NULL;

	} // end constructor for class Node

}; // end class Node


// Begin Class LinkList
class LinkList
{
private:
	Node *head;
	Node *last;
	Node *tmp;
	Node *trav;
	int count;

	void printBack( Node *x )
	{
		if( x )
		{
			printBack( x -> link );
			cout << x -> data << "  ";

		}

	} // end printBack

public:
	LinkList()
	{
		head = last = tmp = NULL;
		count = 0;

	} // end constructor

	bool insert ( char x )
	{
		if ( !(tmp= new Node(x) ) )
			return false;

		count++;

		if ( !head )
		{
			head = last = tmp;
			return true;

		}

		last -> link = tmp;
		last = tmp;
		tmp = NULL;
		return true;

	} // end function insert

	bool remove ( char &x )
	{
		if ( !head )
			return false; // if statement for checking if it is empty return false

		if ( count == 1 )
		{
			x = head -> data;
			delete head;
			head = last = NULL;
			count--;
			return true;

		} // end if statement to check for the single node

		x = head -> data;
		tmp = head;
		head = head -> link;
		delete tmp;
		tmp = 0;
		return true;

	} // end function to remove item from the linklist

	bool delX ( char x )
	{
		char u;
		if ( !head )
			return false;

		if ( head -> data == x )
		{
			remove( u );
			return true;
		}

		for( tmp = head, trav = head -> link; trav; tmp = trav , trav = trav -> link ) // 3ashan enmmashy two pointers together
		{
			if( trav -> data == x )
			{
				tmp -> link = trav -> link;
				if( trav == last )
					last = tmp;
				delete trav;
				trav = NULL;
				return true;
			}

		}

		return false;

	} // end function delX

	void printLinkList()
	{
		if( !head )
			cout << "\nThe LinkList is empty!\n";

		for( trav = head; trav; trav = trav -> link )
			cout << trav -> data << "  ";

	} // end function print the link list

	void printBackward()
	{
		if( !head )
			cout << "\nThe LinkList is empty!\n";

		printBack( head );

	} // end function printBackward

	bool findX ( char x )
	{
		for( tmp = head; tmp; tmp = tmp -> link )
			if( tmp -> data == x )
				return true;

			return false;

	} // end findX

	~LinkList()
	{
		char u;

		for( int i = 0; i < count; i++ )
		{
			remove(u);
		}

		tmp = head = last = trav = 0;
		count = 0;

	} // end destructor

}; // end class LinkList

//######################## Begin Function Main ###########################
int main()
{
	string lib;
	LinkList game;

	cout << "Please, Read the paragraph Carefully.\nAfter the you'll have to fill the suitable word in the suitable place.\n";
	cout << "For example, for each Keyword; name, adverb..etc, Fill in the suitable word.\n\n\n";

	// Ofstream constructor opens file
	ifstream fin( "madlib.txt" );

	// exit program if the file not opened
	if( !fin )
	{
		cerr << "File couldn't be opened.\n";
		exit(1);
	} // end if statement

	while( getline( fin, lib, '\n' ) )
	{
		game.insert( lib );
		cout << lib;
	} // end while loop

	cout << endl;

	return 0;


}
//######################## End Function Main ###########################

and I want the paragraph in the file not to be changed so, do you advice me to use what you said
insert( const char * lib ) ???

I just have to output the updated paragraph on the screen, with no changes in the file

if you need more info, plz let me know :)

do you advice me to use what you said insert( const char * lib ) ???

Not anymore. You might change the type of the data member from char to std::string .
And make the necessary (similar) changes throughout the LinkList and Node classes.

The insert() function, for example, would change to LinkList::insert( const std::string & x ); And you would use it ...

while( getline( fin, lib, '\n' ) )
{
    game.insert( lib );
    cout << lib;
}

And the Node class would look like ...

// Begin Class Node
class Node
{
public:
    std::string data;
    Node *link;

    Node ( const std::string & x )
    {
        data = x;
        link = NULL;

    } // end constructor for class Node

}; // end class Node

mtrmkar
thanks for the reply and the BIG help

but please for (const string &x)
should I change the type of the parameters of the whole functions under LinkList to take (const string &x)?

and Why we did pass it as a reference? because I don't want the original data in the file to be changed!

Thanks, please more explanation will be more appreciated :)

I've changed the whole functions in LinkList class and Node Class to take this kind of parameter (const string &x)

and I ended up with two error messages from the compiler *they're the same*
: error C2678: binary '=' : no operator defined which takes a left-hand operand of type 'const class std::basic_string<char,struct std::char_traits<char>,cla

!!! what should I do?

for (const string &x)
should I change the type of the parameters of the whole functions under LinkList to take (const string &x)?

Use [B]const [/B]string & with any function that will not modify the argument it receives, e.g. the Node class's constructor and the insert() function.

Why we did pass it as a reference?

To avoid temporary string objects from being generated.

because I don't want the original data in the file to be changed!

Assuming you have the following constructor Node ( [B]const [/B]std::string [B]&[/B] x ); The following insert() works without changing x

bool insert ( [B]const[/B] std::string & x )
{
    // x comes in as const and hence its value
    // cannot be changed in this function

    if ( !(tmp= new Node(x) ) )
        return false;

    count++;

    if ( !head )
    {
        head = last = tmp;
        return true;

    }

    last -> link = tmp;
    last = tmp;
    tmp = NULL;
    return true;

} // end function insert

I've changed the whole functions in LinkList class and Node Class to take this kind of parameter (const string &x)

and I ended up with two error messages from the compiler *they're the same*
: error C2678: binary '=' : no operator defined which takes a left-hand operand of type 'const class std::basic_string<char,struct std::char_traits<char>,cla

!!! what should I do?

For example the remove() function cannot be passed in a [B]const [/B]string & , because the function modifies the argument. So you need to have ... bool remove ( std::string & x );

commented: Perfect member :) +1

That's awesome thanks a lot for your effort :D

I'll be right back, as I'm testing the program if it does work efficiently I'll let you know

also, If it doesn't I'll let you know :)

thanks a lot for the moment, until I come back

Last thing :) *Shy*

I'll use the program and add things to it, Please Please keep your eyes (and the other programmers) on this topic

tomorrow I'll be posting the result if it does work efficiently or not, as I'm adding other things, so I don't think that I can test the program now!

thanks in advance :)
God Bless

thanks mates, I did it :)

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.