1

good day. i have a problem with my program. this is designed for small clinics to basically

record the patients information for an appointment. so here is my problem, at first it was

working but whenever im going to input infos for the 3rd patient(3rd node), after inputting

the name it will go on an error. this is a queue. `

#include <iostream>
using namespace std;
struct clnc_hrs 
{

std:: string name;
std:: string symptms;
  int age;
  int cont_num;
  clnc_hrs *next;



};

class List{
private:
    clnc_hrs *rear;
    clnc_hrs *front;
public:
    List();
    void BookAppointment();
    void DeletePrevious();
    void DisplaySchedule();
};
List::List(){
rear = NULL;
front = NULL;
}

void List::BookAppointment ()
{
   std:: string N;
   std:: string S;
   int A;
   int CN;




clnc_hrs *newnode = (clnc_hrs*)malloc(sizeof(clnc_hrs));

 fflush(stdin);

 cout<<"enter name of the patient:";
 std::getline(cin, N);
 newnode->name = N;
 fflush(stdin);

 cout<<"enter age of the patient:";
 cin>>A;
 newnode->age = A;
 fflush(stdin);

 cout<<"enter contact number of the patient:";
 cin>>CN;
 newnode->cont_num = CN;
 fflush(stdin);


 cout<<"enter the complaints or symptoms of the patient:";
 std::getline(cin, S);
 newnode->symptms = S;



 newnode->next = NULL;

if(front == NULL)
{
      fflush(stdin);
      front = newnode;
      fflush(stdin);
}
else
{
    fflush(stdin);
    rear->next = newnode;
    fflush(stdin);
}

rear = newnode;   

}

void List::DisplaySchedule ()
{
 clnc_hrs *disp = (clnc_hrs*)malloc(sizeof(clnc_hrs));
 disp = front;


 if(front == NULL)
 {
          cout<<"there's no appointment for today"<<endl;
 }
 else
 {
     while(disp != NULL)
     {
                cout<<endl;
                cout<<"name:"<<disp->name<<endl;
                cout<<"age:"<<disp->age<<endl;
                cout<<"contact number:"<<disp->cont_num<<endl;
                cout<<"symptoms/complaints:"<<disp->symptms<<endl;
                cout<<endl;


                disp = disp->next;
     }
 }

}    

void List::DeletePrevious()
{
 clnc_hrs *newnode = (clnc_hrs*)malloc(sizeof(clnc_hrs));

 if(front == NULL)
 {
          cout<<"no appointments today"<<endl;
 }
 else
 {
     newnode = front;
     front = front->next;
     cout<<"The previous patient is: "<<endl;
     cout<<newnode->name<<endl;
     cout<<newnode->age<<endl;
     cout<<newnode->cont_num<<endl;
     cout<<newnode->symptms<<endl;


     delete newnode;
 }

}

int main ()
{
List list;
int ans;

while(true)
{       
cout<<"press 1 for booking an appointment\npress 2 to delete previous patients info   \npress 3 for display"<<endl;
cin>>ans;

if(ans == 1)
{
 list.BookAppointment();
}
else if(ans == 2)
{
list.DeletePrevious();
}

else if(ans == 3)
{
 list.DisplaySchedule();
}


}

system("pause");
}     

`

i hope someone here can help me. i am using dev c++ 4.9.9.2

4

2 回答 2

3

As you say this in not homework then just use STL - i.e. http://www.cplusplus.com/reference/queue/queue/

All the bits'n'bobs are done for you

于 2013-09-14T09:14:36.573 回答
2

Don't use malloc in a C++ program, it doesn't construct C++ objects correctly, use new instead.

clnc_hrs *newnode = new clnc_hrs();

You might have other errors but this is the first one.


cout<<"enter name of the patient:";
std::getline(cin, N);
newnode->name = N;

is not wrong, but just a bit wasteful. This is simpler

cout<<"enter name of the patient:";
std::getline(cin, newnode->name);

same for all you other inputs.


fflush(stdin);

is implementation defined behaviour. fflush only has defined behaviour on output streams not input streams. You seem to be using fllush(stdin); as some sort of magic incantation that you hope will solve your problems. Avoid that kind of thinking. Learn what the code you write really does. In this case you should remove all calls to fflush(stdin);.

于 2013-09-14T09:02:43.480 回答