I need to read in instructions from a file, then with those instructions, insert the names and scores below the instruction into a linked lists. I've tried using getline, and >> and I keep getting errors. Here is what I have so far:
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
class LinkedList
{
private:
struct Node
{
string name;
int number;
struct Node *next;
};
Node *head;
public:
LinkedList()
{head = NULL; }
void addLast(string, int);
void Insert(string, int);
void Delete(string, int);
void Display() const;
};
void LinkedList::addLast(string data1, int data2)
{
Node *newNode;
Node *nodePtr;
newNode = new Node;
newNode->name = data1;
newNode->number = data2;
newNode->next = NULL;
if (!head)
head = newNode;
else
{
nodePtr = head;
while (nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
void LinkedList::Insert(string data1, int data2)
{
int comp;
Node *newNode;
Node *nodePtr;
Node *prevNode = NULL;
newNode = new Node;
newNode->name = data1;
newNode->number = data2;
if(!head)
{
head = newNode;
newNode->next = NULL;
}
else
{
nodePtr = head;
prevNode = NULL;
while((nodePtr != NULL) && (nodePtr->name.compare(data1) < 0))
{
prevNode = nodePtr;
nodePtr = nodePtr->next;
}
if(prevNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else
{
prevNode->next = newNode;
newNode->next = nodePtr;
}
}
}
void LinkedList::Delete(string data1, int data2)
{
int comp;
Node *nodePtr;
Node *prevNode;
if(!head)
return;
if(nodePtr->name.compare(data1) == 0)
{
nodePtr = head-> next;
delete head;
head = nodePtr;
}
else
{
nodePtr = head;
while((nodePtr != NULL) && (nodePtr->name.compare(data1) != 0))
{
prevNode = nodePtr;
nodePtr = nodePtr-> next;
}
if(nodePtr)
{
prevNode->next = nodePtr->next;
delete nodePtr;
}
}
}
void LinkedList::Display() const
{
Node *nodePtr;
nodePtr = head;
while(nodePtr)
{
cout << nodePtr->name << nodePtr->number << endl;
nodePtr = nodePtr->next;
}
}
int main()
{
ifstream inFile;
const int SIZE = 51;
string nm, info[SIZE], input, num;
char ch;
int i = 0;
inFile.open("/home/sgilbert/Documents/scores.txt");
if(!inFile)
{
cout << "Error! File cannot open." << endl;
return 0;
}
else
cout << "File has been opened." << endl;
inFile >> input;
while(!inFile.eof())
{
if (input == "%INSERT")
{
while(input != "%DELETE")
{
inFile >> nm;
inFile >> num;
cout << nm << num << endl;
}
}
else if (input == "%DELETE")
{
cout << "delete" << endl;
}
else if (input == "%SEARCH")
{
cout << "search" << endl;
}
else if (input == "%PRINT")
cout << "print" << endl;
else if (input == "%END")
return 0;
cout << "File has been closed." << endl;
return 0;
I'm working on this program in steps, and when I output the values I just put in for the %INSERT function, I get %PRINT in a loop.
My program also skips over the first line of the file. It doesn't recognize it.
The File is:
%INSERT
MARK 29
DAVID 21
JOHN 44
JOHN 51
LARRY 39
MARK 21
DAVID 18
JOHN 28
MARK 35
DONALD 41
PHIL 26
%PRINT
%DELETE
MARK
DAVID
%PRINT
%SEARCH
JONE
DAVID
LARRY
%INSERT
LARRY 13
GARY 15
GARY 42
%PRINT
%INSERT
TERRY 23
%DELETE
GARFIELD
%SEARCH
PHIL
%PRINT
%END