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.