Link List Help

trying to put dictionary file into linked list

Page 1 of 1

8 Replies - 1063 Views - Last Post: 06 March 2009 - 10:20 PM Rate Topic: -----

#1 zeitgeist21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-February 09

Link List Help

Posted 04 March 2009 - 09:17 PM

Hello. I am trying to read a dictionary file into a linked list. I can't figure out why I am getting an error on line 28, I don't quite know what it means.

The error reads:

DictionaryLinkList.cpp:28: error: expected primary-expression before . token

Here's the code


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

struct dictionary{
	int num;
	dictionary* next;
};

int main()
{
	int length=0;
	ifstream fin;
	fin.open ("dictionary.txt");
	
	// If input of dictionary.txt file fails
	for (int x=0; x<=length; x++)
	if (fin.fail())
		{
			cout << "Cannot find dictionary file" << endl;
		}
		
	dictionary* start = new dictionary;
	dictionary* now = start;
	
	for(int i=0; !fin.eof(); i++){
		int temp;
		fin.getline(dictionary[i],length) = now->num;
		now->next = new dictionary;
		now = now->next;
	}
	fin.close();

	while(start->next!=NULL){
		cout << "Number is " << start->num << endl;
		start = start->next;
	}
}





Thanks for the help!

Is This A Good Question/Topic? 0
  • +

Replies To: Link List Help

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Link List Help

Posted 04 March 2009 - 10:08 PM

Are you sure it wasn't "... before ‘[’ token"?

This line:
		fin.getline(dictionary[i],length) = now->num;

has a few problems. First of all, the signature for this getline call should be:
getline( [buffer], size ) where [buffer] is a (pointer to a) char[] array, and size is size_of_buffer - 1 (or less, i.e. the maximum number of chars you want to read).

The getline call will read a line of text from fin and store it in buffer.
I don't know what you had in mind by putting dictionary[i] there instead of a char[] buffer. dictionary is a datatype, not a variable, and not an array.

I also don't know what you're trying to do with the "= now->num" at the end of the line, but you can't do that. If you're trying to assign something to now->num, then now->num has to be on the left side of the =, not the right. But what is the "something" that you're trying to assign?

This post has been edited by r.stiltskin: 04 March 2009 - 10:10 PM

Was This Post Helpful? 1
  • +
  • -

#3 zeitgeist21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-February 09

Re: Link List Help

Posted 05 March 2009 - 05:53 PM

Thanks for the help. I was trying to set up the linked list this way. I'm new at this and don't think I am seeing everything that involves reading each line individually and put it in a linked list. Did I make the struct wrong? Should I have made it to hold a char not an int?

if I make

char word;

before my int main and change the 28 to

getline(word[i],length) = now->num;

would that work, or even be close.
Was This Post Helpful? 0
  • +
  • -

#4 zeitgeist21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-February 09

Re: Link List Help

Posted 05 March 2009 - 06:33 PM

scratch that, I think this is close though. I'm just having problems figuring the getline out on 30.

Here's the code:


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

struct node{
	char word[40];
	node* next;
};

node *start_ptr = NULL;

int main()
{
	int length=0;
	ifstream fin;
	fin.open ("dictionary.txt");
	
	// If input of dictionary.txt file fails
	for (int x=0; x<=length; x++)
	if (fin.fail())
		{
			cout << "Cannot find dictionary file" << endl;
		}
		
	node* start = new node;
	node* now = start;
	
	for(int i=0; !fin.eof(); i++){
		int temp;
		fin.getline(node *word[40]) >> now->word;
		now->next = new node;
		now = now->next;
	}
	fin.close();

	while(start->next!=NULL){
		cout << "Number is " << start->word << endl;
		start = start->next;
	}
}




This post has been edited by zeitgeist21: 05 March 2009 - 06:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Link List Help

Posted 05 March 2009 - 07:02 PM

this is how you can read a word into a new node:
node* temp = new node();
fin.getline( temp->word, 40 );


I'll have some more comments later but don't have time right now...
Was This Post Helpful? 0
  • +
  • -

#6 zeitgeist21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-February 09

Re: Link List Help

Posted 05 March 2009 - 07:35 PM

Cool. I will try this and wait for your further comments. Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Link List Help

Posted 05 March 2009 - 08:41 PM

OK, first of all, in my last post where I wrote the empty parentheses at the end of "new node()", they were actually unnecessary. I was writing a bunch of java code recently (where the () is required) & just got into that habit. In this case they're not harmful, but also not needed so you can just as well write
node* temp = new node;


I also wanted to suggest that when you create a new node, its next pointer should be initialized to NULL. You can add a constructor to the node definition to do that automatically when a node is instantiated:
struct node{
	char word[40];
	node* next;
	// Following is a constructor; you'll learn more about them when
	// you learn about classes (a struct is essentially a simple class)
	node() : next(NULL) {} 
};



Aside from that, I also suggest that you create a sort of front-end to your dictionary instead of just having a pointer to the first node. So define another struct and name it dictionary. Its data members will be two node* pointers: node* first and node* last. Initialize them both to NULL in the struct definition, using a constructor as described above, just using a comma to separate the two initializers -- i.e. "first(NULL), last(NULL) {}". In your main function, instantiate a dictionary like you would any other variable: "dictionary my_dict;" for example.

When you add the first node, set both of the dictionary's pointers to point to the new node. Then when you add another node, the "first" pointer doesn't change, but the "last" pointer gets reassigned to point to the new node, and so on. That way you always have immediate access to the beginning and the end of the list.
Was This Post Helpful? 1
  • +
  • -

#8 zeitgeist21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-February 09

Re: Link List Help

Posted 06 March 2009 - 08:05 PM

Hey thanks for the help! I've figured it out.

Here's the code:


// reads in dictionary file to linked list and prints it
#include <iostream>
#include <fstream>
using namespace std;

struct node{
	char word[100];
	node* next;
};

int main()
{
	node* start = new node;
	node* now = start;
	char* word[100];
	int length=0;
	ifstream fin;
	//change file name depending on what file you want to use, must be a file in same directory
	fin.open ("dictionary.txt");
	cout << fin;			
	// If input of dictionary.txt file fails
	if (fin.fail())
		{
			cout << "Cannot find dictionary file" << endl;
		}
	while(!fin.eof()){
		//node* temp = new node();
		fin.getline (now -> word, 100);
		//now->word;
		now->next = new node;
		now = now->next;
	}
	fin.close();
	while(start != NULL)
	{
		cout << start->word << endl;
		start = start->next;
	}
	return (0);
}



This post has been edited by zeitgeist21: 06 March 2009 - 08:06 PM

Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Link List Help

Posted 06 March 2009 - 10:20 PM

Well, that works if all you want to do is read in words from the file, print them and then exit. But if, instead, you wanted the program to do anything else after printing them, you're in trouble because you no longer have a pointer to the beginning of the list.

This post has been edited by r.stiltskin: 06 March 2009 - 10:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1