【问题标题】:Stacks and Queues堆栈和队列
【发布时间】:2013-09-18 22:52:40
【问题描述】:

美好的一天。我的程序有问题。这是专为小型诊所设计的,基本上

记录患者信息以进行预约。所以这是我的问题,一开始是

工作,但每当我要输入第三位患者(第三个节点)的信息时,输入后

这个名字会出错。这是一个队列。 `

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

`

我希望这里有人可以帮助我。我正在使用开发 c++ 4.9.9.2

【问题讨论】:

  • “一个错误”?您究竟遇到了什么错误?
  • 不要创建自己的集合类,使用ones already availablestd::vector
  • 如果可以避免,请不要使用指针。
  • 写一个Patient类,一个Appointment类,并使用std::queue&lt;Appointment&gt;?

标签: c++


【解决方案1】:

正如您在非家庭作业中所说的那样,只需使用 STL - 即http://www.cplusplus.com/reference/queue/queue/

所有的小事都为你准备好了

【讨论】:

  • 哦,我忘了说这是我的学校项目。
  • @KemuelFabic - 好的,这是一种学习体验 - 然后学习使用调试器
【解决方案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); 的所有调用。

【讨论】:

  • 这不是错误,只是一个最佳实践建议。 malloc() 在 C++ 中是完全合法的。
  • @rodrigo 在这种情况下,这是一个错误,如果您在建议中添加许多警告,只会让新手感到困惑。就目前而言,对于这个程序员来说,永远不要在 C++ 程序中使用 malloc 是合理的。
  • error这个词有很多含义。也许您的意思是这是一个错误,例如“信任您不认识的人是错误的”。但在编程中,它通常意味着:“你的代码无效,不能正常工作,以后会咬你”。这也令人困惑。
  • @rodrigo 但在这种情况下,OP 对 malloc 的使用在您的意思上是一个错误。他的结构包含 std::string 并且他使用 malloc 来分配它,这是一个错误。
  • @rodrigo:malloc 不调用构造函数。如果此人希望他们的对象(以及其中包含的std::strings)有效,则malloc 是一个错误
猜你喜欢
  • 2014-07-21
  • 2015-11-16
  • 2019-03-07
  • 2021-04-29
  • 1970-01-01
  • 2021-03-17
  • 2014-04-21
  • 2015-09-19
  • 1970-01-01
相关资源
最近更新 更多