1

良い一日。プログラムに問題があります。これは、基本的に小規模なクリニック向けに設計されています。

予約のために患者情報を記録します。ここに私の問題があります。最初は

動作していますが、入力後、3 番目の患者 (3 番目のノード) の情報を入力するたびに

エラーが発生する名前。これはキューです。`

#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");
}     

`

ここの誰かが私を助けてくれることを願っています。私はdev c++ 4.9.9.2を使用しています

4

2 に答える 2

3

あなたが宿題ではなくこれを言うように、STLを使用してください-つまり、http://www.cplusplus.com/reference/queue/queue/

すべてのビット&ボブはあなたのために行われます

于 2013-09-14T09:14:36.573 に答える
2

C++ プログラムでは使用しないでくださいmalloc。C++ オブジェクトが正しく構築されませんnew。代わりに使用してください。

clnc_hrs *newnode = new clnc_hrs();

他にもエラーがあるかもしれませんが、これが最初のエラーです。


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

間違っていませんが、少し無駄です。これはもっと簡単です

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

他のすべての入力についても同じです。


fflush(stdin);

実装定義の動作です。fflush入力ストリームではなく、出力ストリームに対する動作のみが定義されています。あなたはfllush(stdin);、問題を解決してくれる魔法の呪文のようなものを使っているようです。そのような考えは避けてください。あなたが書いたコードが実際に何をするかを学びましょう。この場合、 へのすべての呼び出しを削除する必要がありますfflush(stdin);

于 2013-09-14T09:02:43.480 に答える