Link to home
Start Free TrialLog in
Avatar of greyfacemagilicuty
greyfacemagilicuty

asked on

Linked List Error

My menu drive system works and I am able to enter the information about the appointment that the menu asks for, however; when I try to "Print all appointments" I get an error. I think an infinite loop is the problem or that I am not entering the information (cin>>) correctly.  Thanks for your help. There are 500 point resting on your ability to answer this question.


//NOTE: app_book.txt is automatically read as the program starts.  Data is saved in saved.txt when the program ends.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct app_book
{
     string author;
     string subject;
     string location;
     int date;
     string description;    
     string start;
     string finish;
     app_book* next;
     app_book(string, string, int, string, string, string, app_book*);
};

app_book::app_book(string tempSubject, string tempLocation, int tempDate, string tempDescription, string tempStart, string tempFinish, app_book* tempNext)
:subject(tempSubject), location(tempLocation), date(tempDate), description(tempDescription), start(tempStart), finish(tempFinish), next(tempNext)
{}

typedef app_book* app_bookPtr;

void getline(istream &stream, string &str, char delimiter)
{     char temp[500];

stream.get(temp, 500, delimiter);
stream.ignore(500, delimiter);
str = temp;
}

void getline(istream &stream, int &num, char delimiter)
{     int temp;

stream >> temp;
stream.ignore(500, delimiter);
num= temp;
}

void readFile(app_bookPtr &root);
void insert (app_bookPtr &root);
void delSubject(app_bookPtr &root);
app_bookPtr locateNode(app_bookPtr temp, string tit);
void delDate(app_bookPtr &root);
app_bookPtr locateNodeDate(app_bookPtr temp, string isb);
void searchDate(app_bookPtr temp);
void printList(app_bookPtr temp);
void printLocation(app_bookPtr temp);
void saveFile(app_bookPtr temp);
int countNodes(app_bookPtr temp);
void printSubject(app_bookPtr temp);
app_bookPtr locateNodeSubject(app_bookPtr temp, string sub);

int main()
{
     int choice;
     app_bookPtr root = NULL;
     readFile(root);

     do
     {
          cout << "Menu: Select your option\n\n";
          cout << "(1) Add an appointment\n";
          cout << "(2) Delete an appointment based on Subject\n";
          cout << "(3) Print appointment(s) based on Subject\n";
          cout << "(4) Print all appointments\n";
          cout << "(5) Quit.\n\n";
          cout << "Enter your choice ---> ";

          cin >> choice;

          if (1 <= choice && choice <= 4)
          {    
               switch (choice)
               {
               case 1:
                    insert(root);
                    break;
               case 2:
                    delSubject(root);
                    break;
               case 3:
                    printSubject(root);
                    break;
               case 4:
                    printList(root);
                    break;              
               default:
                    cout << "Invalid choice.  Enter again.\n\n";
                    break;
               }
          }    
     }
     while (choice != 5);
     saveFile(root);
     return 0;
}

void readFile(app_bookPtr &root)
{
     int numAppts, date;
     string sub, loc, srt, fin,desc;
     ifstream infile ("app_book.txt", ios::in);
     infile >> numAppts;
     infile.ignore(500,'\n');
     for (int count = 0; count < numAppts; count++)
     {
          getline(infile, sub, '\n');
          getline(infile, loc, '\n');
          getline(infile, date, '\n');
          getline(infile, desc, '\n');
          getline(infile, srt, '\n');
          getline(infile, fin, '\n');

          root = new app_book (sub, loc, date, desc, srt, fin, root);
     }
}

void insert (app_bookPtr &root)
{
     string sub, loc, srt, fin, desc;
     int date;

     cout << "Subject:\t\t\t";
     cin.ignore(500,'\n');
     getline(cin, sub, '\n');
     cout << "Location:\t\t\t";
     getline(cin, loc, '\n');
     cout << "Date:\t\t";
     getline(cin, date, '\n');
     cout << "Description:\t\t";
     getline(cin, desc, '\n');
     cout << "Start:\t\t\t";
     getline(cin,srt, '\n');
     cout << "Finish:\t\t\t";
     getline(cin, fin, '\n');

     root = new app_book (sub, loc, date, desc, srt, fin, root);
}

void delSubject(app_bookPtr &root)
{
     string sub;

     cout << "Appointment Subject:\t\t\t";
     cin.ignore(500,'\n');
     getline(cin, sub, '\n');

     app_bookPtr p = locateNode(root, sub);

     if (p == NULL)
          cout << "\nDeletion cannot be done.\n\n";
     else if (root == p)
          root = p->next;
     else
     {
          app_bookPtr q = root;
          while (q->next != p)
               q = q->next;
          q->next = p->next;
     }
     delete p;
}

app_bookPtr locateNode(app_bookPtr temp, string sub)
{
     while (temp != NULL)
     {
          if (temp->subject == sub)
          {
               return temp;
          }
          temp = temp->next;
     }
     return NULL;
}


app_bookPtr locateNodeSubject(app_bookPtr temp, string sub)
{
     while (temp != NULL)
     {
          if (temp->subject == sub)
          {
               return temp;
          }
          temp = temp->next;
     }
     return NULL;
}

void printList(app_bookPtr temp)
{
     while (temp != NULL)
     {
          cout << temp->subject << "\n";
          cout << temp->location << "\n";
          cout << temp->date << "\n";
          cout << temp->description << "\n";
          cout << temp->start << "\n";
          cout << temp->finish << "\n\n";
          temp = temp->next;
     }
     cout << "\n";
}

void printSubject(app_bookPtr temp)
{
     string aut;

     cout << "Subject name:\t\t\t";
     cin.ignore(500,'\n');
     getline(cin, aut, '\n');

     while (temp != NULL)
     {
          if (temp->author == aut)
          {
               cout << temp->subject << "\n";
               cout << temp->location << "\n";
               cout << temp->date << "\n";
               cout << temp->description << "\n";
               cout << temp->start << "\n";
               cout << temp->finish << "\n\n";
          }
          temp = temp->next;
     }
     cout << "\n";
}

void saveFile(app_bookPtr temp)
{
     int count = countNodes(temp);
     ofstream outFile("saved.txt",ios::out);

     outFile << count << "\n";
     while (temp != NULL)
     {
          outFile << temp->subject << "\n";
          outFile << temp->location << "\n";
          outFile << temp->date << "\n";
          outFile << temp->description << "\n";
          outFile << temp->start << "\n";
          outFile << temp->finish << "\n";
          temp = temp->next;
     }
     cout << "\n";
}

int countNodes(app_bookPtr temp)
{
     int countB = 0;
     while (temp != NULL)
     {
          countB++;
          temp = temp->next;
     }
     return countB;
}
Avatar of novitiate
novitiate

Look at this line
root = new app_book (sub, loc, date, desc, srt, fin, root);
in
void readFile(app_bookPtr &root) and
void insert (app_bookPtr &root)

1 - Its becoming a circular linked list and is pointing to same node.
2 - every new node is assigned to root.
      you should be connecting new nodes at the end of the list.
      some thing like this.
if(root == NULL){
      root = new app_book (sub, loc, date, desc, srt, fin, NULL);
}else{
      app_bookPtr tmp = root;
      while(tmp->next)
            tmp = tmp->next;
      tmp->next = new app_book (sub, loc, date, desc, srt, fin, NULL);
}

_novi_
Avatar of greyfacemagilicuty

ASKER

Thanks.  My next problem is surrounding the prompt from options 1, 2, 3, and 4.  For option 1, I would like the appointments to be organized by date in ascending order.  For option 2, I would like the use to input subject information to delete the appointment.  For option 3, I would like the user to input the appointment date so the program will print out all appointments with the entered dates.  For option 4, I would like the program to print all appointments by date in descending order.  Thanks and I can promise you *novitiate* those 500 points.  
I think its better for you to use stl collections, vector or list would do.

and then for sorting use std::sort from algorithm
_novi_
i dont understand exactly what you mean...if you could show or explain to me for each option that would be great.  
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial