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

Kemuel Fabic
  • 29
  • 1
  • 4

2 Answers2

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

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
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);.

john
  • 85,011
  • 4
  • 57
  • 81
  • This is not an error, just a _best practice_ advice. `malloc()` is perfectly legal in C++. – rodrigo Sep 14 '13 at 09:04
  • @rodrigo In this case it is an error, if you add to many caveats to your advice it just confuses newbies. For now, for this programmer, never use malloc in a C++ program is reasonable. – john Sep 14 '13 at 09:06
  • 1
    The word _error_ has many meanings. Maybe you mean that it is an error, as in "it is an error to trust someone you don't know". But in programming, it usually means: "your code is not valid, will not work properly and will come later to bite you". And that's also confusing. – rodrigo Sep 14 '13 at 09:12
  • @rodrigo But in this case the OP's use of malloc is an error in the sense you mean. His struct contains std::string's and he's using malloc to allocate it, that's an error. – john Sep 14 '13 at 09:13
  • @rodrigo: `malloc` doesn't call constructors. If this person wants their objects (and the `std::strings` they contain) to be valid then `malloc` is an _error_. – Blastfurnace Sep 14 '13 at 09:14
  • @john: Oh, I see! That __is__ an error for any meaning of the word! – rodrigo Sep 14 '13 at 09:40
  • @john so without the use of my 'magic' fflush. what others should i use to fix the skipping part. when i remove it the "enter age" displays suddenly after the "enter name" part and i cant seem to input data in the name part. and thanks for enlightening me. i am thankful. – Kemuel Fabic Sep 14 '13 at 17:48
  • There's a few different techniques. One that works is `cin.ignore(INT_MAX, '\n');` which causes characters to be ignored from `cin` until a newline is read, (you might need to `#include ` to get the definition of INT_MAX). Another technique is simply to read everything with getline and then convert the string read to an integer when you need to. – john Sep 14 '13 at 18:17
  • Also see this recent question, http://stackoverflow.com/questions/18803871/basic-string-input – john Sep 14 '13 at 18:24
  • okay. i just finished my program. thanks to all of you for enlightening me. i am so thankful to all of you! – Kemuel Fabic Sep 15 '13 at 05:56