i have created a program to take last names into a linked list, now i have everything working except one thing, i need to output the files to a txt file, but currently when i do that , its only outputing my "end" word in order to stop the program below is the code please let me know what i have done wrong
main.cpp
Code:
void main()
{
string name;
SortedList Roster;
ofstream output;
output.open("roster.txt");
while (name != "end" )
{
cout << "Please enter your last name -- Type END when finished" << endl;
cin >> name;
Roster.Insert(name);
output << name << endl;
output.close();
}
cout << "You entered:" << "\n" << "\n";
Roster.TraverseList();
}
linkedlist.h
Code:
struct NodeType
{
string info;
NodeType* link;
};
typedef NodeType* NodePtr;
// Variable DECLARATIONS
NodePtr head;
NodePtr ptr;
class SortedList
{
public:
SortedList();
void Insert(string);
void TraverseList();
private:
};
SortedList::SortedList( ) // Constructor
// Post: head == NULL
{
head = NULL;
}
// insert into the list
void SortedList::Insert( string item )
{
NodePtr currPtr;
NodePtr prevPtr;
NodePtr location;
location = new NodeType;
location->info = item;
prevPtr = NULL;
currPtr = head;
while ( currPtr != NULL && item > currPtr->info )
{ prevPtr = currPtr; // advance both pointers
currPtr = currPtr->link;
}
location->link = currPtr; // insert new node here
if ( prevPtr == NULL )
head = location;
else
prevPtr->link = location;
}