just wanted to say heya to everyone first, ok im doing a C++ assigment on linked list but im stuck. i need to write a program that reads records from a text file and then stores them in a linked list. i can read the data from a text file but i can only enter the first record into my linked list after that the program ends. here is the code i have so far.

#include <fstream>
#include <string>
#include <iostream>

using namespace std;

//Declareing The Structure

struct node {					
				char Name[25];
				int Year;
				double Last;
				double Next;
				node *nextp;
				};

int main()
{

//Initialising Pointers
	node *startp, *temp;


// Opens the Text File for reading

	ifstream infile ("info.txt" , ios :: in);

//if the file is open outputs error

			if (!infile)
			{
				cout<<"Error: Unable to open .txt file"<<endl;
				exit(1);
			}

			 else
//intialise startp to = new node

				startp = new node;
				temp=startp;


//begins data entry from file to structure
				
					infile>>temp->Name;
					infile>>temp->Year;
					infile>>temp->Last;
					infile>>temp->Next;
					temp->nextp=NULL;
				

// Set up link to this node
					 if (startp != NULL)
					 {while (startp->nextp !=NULL)
						temp=startp->nextp;
					 }

// outputs error if there is still data in the file that hasnt been
// put into the structure/linked list
				if (infile.get() != EOF)
				{
					cout<<"something Wrong with input::data still remaining"<<endl;
				}

//close the text file
		infile.close();

//begins a loop to output data from the linked list
	cout <<"data Entered from file: "<<endl;
	
	
	temp = startp;
do
  {  if (temp == NULL)
       cout << "End of list" << endl;
     else
       {  // Display details for what temp points to
          cout << "Name : " << temp->Name << endl;
          cout << "Year : " << temp->Year << endl;
          cout << "Previous : " << temp->Last << endl;
		  cout << "Next : " << temp->Next << endl;
          cout << endl;       // Blank line
		  cout << "--- Memry information---"<<endl;
		  cout << "memory Address of startpointer : "<< &startp <<endl;
		  cout << "memory Address of temp pointer : "<< &temp<<endl;
		  cout << "memory Address of Name : "<< &temp->Name<<endl;
		  cout << "memory Address of Year : "<< &temp->Year<<endl;
		  cout << "memory Address of Last : "<< &temp->Last<<endl;
		  cout << "memory Address of Next : "<< &temp->Next<<endl;
		  cout << "memory Address of Nextp : "<< &temp->nextp<<endl;
		  cout << endl;

          // Move to next node (if present)
          temp = temp->nextp;
       }
  }
while (temp != NULL);
cout<<"press Enter To Close";
cin.get();


	
					
	return 0;

}

any help would be much appreciated.

Recommended Answers

All 2 Replies

Often the flow would go like this:

use startp to keep track of where to start
use temp to search list
use newNode to hold data to be inserted into list

use stream to read from file
use a loop to read all records from file

to determine if there is a record to be read try to read a name from file
if can't read a name stop input from file
if can read a name read rest of record
once entire record read in add it to end of list (or whereever desired)

pseudocode:
declare appropriate variables 
associate file with stream
check if stream useable

declare a newNode to hold file input before reading from file 

while input into newNodes name is successful  //assume data for full struct follows
  input rest of newNode 

  place temp at start of list
  
  if temp is NULL  //list is empty
    assign newNode to temp
  else
     //use a loop to find end of list
     while temp next not NULL
         assign temp next to temp

    assign newNode to temp next
    assign a new address to newNode to store information for next new node

when input loop is done determine why loop stopped
if EOF found
   whole file read in, clap real loud
   be sure to clear stream if you want to reuse it
else
   error reading file occurred or file corrupt, sorry

im still not to sure what im doing wrong, i have gone through my code, i have got rid of the message saying there is still unread data in the file but im not sure whether my loop to output data is wrong or if i still havent entered all the data from the file this is my code so far.

#include <fstream>
#include <string>
#include <iostream>

using namespace std;

//Declareing The Structure

struct node {					
				char Name[25];
				int Year;
				double Last;
				double Next;
				node *nextp;
				};

int main()
{

//Initialising Pointers
	node *startp, *temp;


// Opens the Text File for reading

	ifstream infile ("info.txt" , ios :: in);

//if the file is open outputs error

			if (!infile)
			{
				cout<<"Error: Unable to open .txt file"<<endl;
				exit(1);
			}

			 else


				

//begins data entry from file to structure
				
	while (! infile.eof())
{				
		//intialise startp to = new node
					startp = new node;
					temp=startp;

					infile>>temp->Name;
					infile>>temp->Year;
					infile>>temp->Last;
					infile>>temp->Next;
					temp->nextp=NULL;
			

// Set up link to this node
					 if (temp != NULL)
					 {while (temp->nextp !=NULL)
						temp=temp->nextp;
					 }
}
// outputs error if there is still data in the file that hasnt been
// put into the structure/linked list
				if (infile.get() != EOF)
				{
					cout<<"something Wrong with input::data still remaining"<<endl;
				}

	else 

//close the text file
		infile.close();

//begins a loop to output data from the linked list
	cout <<"data Entered from file: "<<endl;
	
	
	temp = startp;
do
  {  if (temp == NULL)
       cout << "End of list" << endl;
     else
       {  // Display details for what temp points to
          cout << "Name : " << temp->Name << endl;
          cout << "Year : " << temp->Year << endl;
          cout << "Previous : " << temp->Last << endl;
		  cout << "Next : " << temp->Next << endl;
          cout << endl;       // Blank line
		  cout << "--- Memry information---"<<endl;
		  cout << "memory Address of startpointer : "<< &startp <<endl;
		  cout << "memory Address of temp pointer : "<< &temp<<endl;
		  cout << "memory Address of Name : "<< &temp->Name<<endl;
		  cout << "memory Address of Year : "<< &temp->Year<<endl;
		  cout << "memory Address of Last : "<< &temp->Last<<endl;
		  cout << "memory Address of Next : "<< &temp->Next<<endl;
		  cout << "memory Address of Nextp : "<< &temp->nextp<<endl;
		  cout << endl;

          // Move to next node (if present)
          temp = temp->nextp;

		 
       }
  }
while (temp != NULL);
cout<<"press Enter To Close";
cin.get();


	
					
return 0;

}

thanks again

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.