1

This program is a small hospital management program. It works with C++ and uses binary files and classes. it is a simple prog that taken the input of patients details and saves in a binary file, and displays the patients details by searching regno.

The code does not run beyond : cin>>A1.PI.age; and starts printing endless loop of something.

PLEASE REPLY ME ON WHERE HAVE I GONE WRONG

here is the code:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>

class all
{ private:
struct address
{ int house;
  char street[30];
  char city[30];};
  struct patient_info
{ char name[40];
  address AD1;
  int age;
  int maritalstatus;
  int regno;
  char bldgrp[3];
  char sex;
}PI;
int task;
protected:
 void firstpinfo();
 void showpinfo();
 void enterpinfo();
public:
 void tasks();
 char ch;
 int serial;
}A1;

class date :public all
 { private :
   int day;
   int month;
   int year;
   public:
   void enterdate()
    {cout<<"enter day of date";
     cin>>day;
     cout<<"enter month";
     cin>>month;
     cout<<"enter year";
     cin>>year;
    }
   void showdate()
     { cout<<day<<"/"<<month<<"/"<<year;}
  }D1;



 //global variables
  int count,attempt;

void main()
{ count=0;
  cout<<"                                       HOSPITAL MANAGEMENT SOFTWARE                                      
  ";
  D1.enterdate();
  A1.tasks();
  getch();
  while(count==0)
   { A1.tasks();
     cout<<"press 0 to continue and 1 to exit";
     cin>> count;
   }
  getch();
 }


  void all::tasks()

 { attempt=0;
  D1.showdate();
  cout<<"select task"<<endl
  <<"1.show patient details"<<endl
  <<"2.enter details of a patient"<<endl
  <<"3.exit prog"<<endl;
  cin>>task;
  switch(task)
 {
    case 1: {cout<<"enter regno to display"<<endl;
    int search;
    cin>>search;
    fstream fon;
    fon.open("hospital.dat",ios::in|ios::binary);
    if(!fon)
     {cout<<"error in opening"<<endl;
      getch();
      exit(0);
      }
    else
     {fon.read((char*)&A1,sizeof(A1));
      if(A1.PI.regno==search)
       {cout<<"showing details";
       A1.showpinfo();}
      else
       {cout<<"regno not found";}
        fon.close();
        }
  break;}
case 2: {cout<<"adding a new patient";
  A1.enterpinfo();
  fstream fan;
  fan.open("hospital.dat",ios::in|ios::binary);
  if(fan)
  {fan.write((char*)&A1,sizeof(A1));}
  fan.close();
  break;}
 case 3: { cout<<"exiting...press any key";
   getch();
   exit(0);
   break;
   }
 default: {cout<<"error... press anykey to try again";
  getch();
  A1.tasks();
  };
 }}//END OF TASKS


 void all::showpinfo()
 {cout<<"patient regno\n"<<A1.PI.regno<<endl;
 cout<<"patient name\n"<<A1.PI.name<<endl;
 cout<<"address of patient\n"<<A1.PI.AD1.house<<" "<< PI.AD1.street<<" "<<PI.AD1.city<<endl;
 cout<<"blood group"<<A1.PI.bldgrp<<endl;
 cout<<"sex"<<A1.PI.sex<<endl;
 cout<<"data extracted";
 }

 void all:: enterpinfo()
 {

  cout<<"enter unique registration number";
  cin>>PI.regno;
  cout<<"enter patient name"<<endl;
  cin.getline(A1.PI.name,50);
  cout<<"enter address( house, street, city)"<<endl;
  cin>>A1.PI.AD1.house;
  cin.getline(A1.PI.AD1.street,30);
  cin.getline(A1.PI.AD1.city,30);
  cout<<"enter age in years"<<endl;
  cin>>A1.PI.age;
  cout<<"enter 1 for married and 0 for otherwise"<<endl;
  cin>>A1.PI.maritalstatus;
  cout<<"enter blood group"<<endl;
  cin>>A1.PI.bldgrp;
  cout<<"enter M for male and F for female";
  cin>>A1.PI.sex;

  }
  • `#include` is not correct for modern `c++` is this turbo c++? – drescherjm Dec 25 '19 at 14:04
  • I believe this may be the problem: [https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – drescherjm Dec 25 '19 at 14:09
  • count is not incremented it stay zero ans call A1.tasks() repeatedly – Build Succeeded Dec 25 '19 at 14:13
  • 1
    No error during compilation means *nothing* as far as correctness goes. That only means that your code was syntactically valid. It may still contain Undefined Behaviour, logic errors and many other types of bugs. – Jesper Juhl Dec 25 '19 at 16:30
  • yes this is turbo c++.. and about count.. it was to make a menu driven program – Harshaa Jaishankar Dec 26 '19 at 06:08

2 Answers2

2

This happens because you have mixed cin and cin.getline. when you enter a value using cin, cin not only captures the value, it also captures the newline. So when we enter 2, cin actually gets the string “2\n”. It then extracts the 2 to variable choice, leaving the newline stuck in the input stream. Then, when getline() goes to read the input, it sees “\n” is already in the stream, and figures we must have entered an empty string! Definitely not what was intended.

A good rule of thumb is that after reading a value with cin, remove the newline from the stream. This can be done using the following :

std::cin.ignore(32767, '\n'); // ignore up to 32767 characters until a \n is removed

I hope this will solve your problem.

PyPotato
  • 36
  • 4
  • Thank you so much for the advice, but we haven't used 'std' based codes as it does not cover under our syllabus and i cannot go out it .Can you suggest another way of getting input for my strings ? – Harshaa Jaishankar Dec 26 '19 at 06:12
  • 1
    you can just use cin.ignore(32767, '\n'); then. It seems your are using turboc++.Turboc++ works even if you don't use std namespace using std::cin and just using cin is same. Turboc++ just adds (using namespace std;) line to your code so you don't need to use std::cin or std::cout every time you take input or print something. – PyPotato Dec 27 '19 at 13:24
  • 1
    I tried your solution. it works perfectly now ! Thank you so much ! – Harshaa Jaishankar Dec 31 '19 at 10:19
1
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>

don't use .h extension

we don't think we need #include<conio.h> anymore

and isn't simpler

getline(cin, A1.PI.name) than cin.getline(A1.PI.name,50)
then resize its max size

A1.PI.name.resize(50);
  • i am unable to use header files without .h extension in Turbo c7 maybe mine is an older version i guess. I ll surely try the getline(cin, A1.PI.name)) ; but isnt same as cin.getline()? – Harshaa Jaishankar Dec 28 '19 at 07:02