locked
Auto-increment number RRS feed

  • Question

  • Hello,

    This code is for (hospital management system).


    This code allow you to add patients.

    There are two types of patients. which is:
    1. Normal patient.
    2. Critically ill patient.

    once you added any patient. You will be able to show all patient using "ShowAllPatient;" method. -> i want the Auto-increment ID Number to appear for each patient has been added.

    And you will be able to call a patient to provide a hospital services for him/her using "GetNextPatient;" method.


    I need to generate an Auto-increment Number for each (patient) has been added. (Auto-increment Number) but it just should be different for each patient. For example the first patient will have 1, The second patient will have 2. etc. The Auto-increment number should appear for each patient.


    Note:
    This program is asking for entering ID.
    So i don't want the program to ask for ID anymore
    just create an Auto-increment number for each patient
    So first patient has been added ID=1
    second patient has been added ID=2
    etc.

    #include "stdafx.h"
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<iostream>
    using namespace std;
    
    
    // The maximum number of patients in queue
    #define MAXPATIENTS 30
    
    
    // define structure for patient information
    struct patient
    
    {
       char FirstName[50];
       char LastName[50];
       char ID[20];
    };
    
    
    // define class for queue
    class queue
    {
       public:
       queue (void);
       int RegisterPatien (patient p);
       int RegisterPatientAtBeginning (patient p);
       patient GetNextPatient (void);
       int CancelAll (patient * p);
       void OutputList (void);
       char DepartmentName[50];
       private:
       int ShowAllPatient;
       patient List[MAXPATIENTS];
    };
    
    
    // declare member functions for queue
    
    queue::queue ()
    {
       // Constructor
       ShowAllPatient = 0;
    }
    
    
    int queue::RegisterPatien (patient p)
    {
       // To add a patient (normally) to the queue (to the end).
       // returns 1 if successful, 0 if queue is full.
      if (ShowAllPatient >= MAXPATIENTS)
       {
          // queue is full
          return 0;
       }
          // put in new patient
          else
          List[ShowAllPatient] = p;  ShowAllPatient++;
          return 1;
    }
    
    
    int queue::RegisterPatientAtBeginning (patient p)
    {
       // adds a critically ill patient to the beginning of the queue.
       // returns 1 if successful, 0 if queue is full.
       int i;
       if (ShowAllPatient >= MAXPATIENTS)
       {
          // queue is full
          return 0;
       }
    
       // move all patients one position back in queue
       for (i = ShowAllPatient-1; i >= 0; i--)
       {
          List[i+1] = List[i];
       }
       // put in new patient
       List[0] = p;  ShowAllPatient++;
       return 1;
    }
    
    
    patient queue::GetNextPatient (void)
    {
       // gets the patient that is first in the queue.
       // returns patient with no ID if queue is empty
    
       int i;  patient p;
       if (ShowAllPatient == 0) {
       // queue is empty
       strcpy(p.ID,"");
       return p;}
       // get first patient
       p = List[0];
       // move all remaining patients one position forward in queue
       ShowAllPatient--;
       for (i=0; i<ShowAllPatient; i++)
       {
          List[i] = List[i+1];
       }
       // return patient
       return p;
    }
    
    
    int queue::CancelAll (patient * p)
    
    {
       // removes a patient from queue.
       // returns 1 if successful, 0 if patient not found
       int i, j, found = 0;
       // search for patient
       for (i=0; i<ShowAllPatient; i++)
    	{
          		if (stricmp(List[i].ID, p->ID) == 0)
    		{
    	 	// patient found in queue
    	 	*p = List[i];  found = 1;
    	 	// move all following patients one position forward in queue
    	 	ShowAllPatient--;
    	 		for (j=i; j<ShowAllPatient; j++)
    				{
    	    				List[j] = List[j+1];
    				}
    		}
    	}
       return found;
    }
    
    
    void queue::OutputList (void)
    {
       // lists entire queue on screen
       int i;
       if (ShowAllPatient == 0)
    	{
          		cout<< "Queue is empty";
    	}
       else
    	{
    
    		for (i=0; i<ShowAllPatient; i++)
    		{
    			cout << "First Name : " << List[i].FirstName<<endl;
    	 		cout << "Last Name : " << List[i].LastName<<endl;
    	 		cout << "Phone Number : " << List[i].ID<<endl<<endl;
    		}
    	}
    }
    
    
    // declare functions used by main:
    
    patient InputPatient (void)
    
    {
       // this function asks user for patient data.
       patient p;
       cout<<endl<<endl;
       cout << "Please enter the information of the Patient"<<endl<<endl;
       cout << "First name: "<<endl<<endl;
       cin.getline(p.FirstName, sizeof(p.FirstName));
       cout << "Last name: "<<endl<<endl;
       cin.getline(p.LastName, sizeof(p.LastName));
       cout << "Phone Number: "<<endl<<endl;
       cin.getline(p.ID, sizeof(p.ID));
       // check if data valid
       if (p.FirstName[0]==0 || p.LastName[0]==0 || p.ID[0]==0)
    	{
          		// rejected
          		strcpy(p.ID,"");
          		cout << "Error: Data not valid. Operation cancelled.";
          		getch();
    	}
       return p;
    }
    
    
    void OutputPatient (patient * p)
    {
       // this function outputs patient data to the screen
       if (p == NULL || p->ID[0]==0)
    	{
          		cout << "No patient";
          		return;
    	}
       else
       cout << "Patient Information:"<<endl<<endl;
       cout << "First name: " << p->FirstName<<endl<<endl;
       cout << "Last name: " << p->LastName<<endl<<endl;
       cout << "Phone Number: " << p->ID<<endl<<endl;
    }
    
    
    int ReadNumber()
    {
       // this function reads an integer number from the keyboard.
       // it is used because input with cin >> doesn't work properly!
       char buffer[20];
       cin.getline(buffer, sizeof(buffer));
       return atoi(buffer);
    }
    
    
    void DepartmentMenu (queue * q)
    {
       // this function defines the user interface with menu for one department
       int choice = 0, success;  patient p;
       while (choice != 6)
    	{
          	   // clear screen
    // print menu
    		   system("CLS");
          	   cout << "<< || Welcome || >> "<<endl << q->DepartmentName<<endl;
    		   cout << "Please enter your choice:"<<endl<<endl;
          	   cout << "1:  Add patient"<<endl;
          	   cout << "2:  Add critically ill patient"<<endl;
          	   cout << "3:  Call a patient for operation"<<endl;
          	   cout << "4:  Remove all patients from queue"<<endl;
          	   cout << "5:  Show all patient"<<endl;
    		   cout << "6:  Exit"<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
          	   // get user choice
          	   choice = ReadNumber();
          		// do indicated action
          	switch (choice)
    	  {
          		case 1:   // Add normal patient
    	 	p = InputPatient();
    	 	if (p.ID[0])
    		   {
    	    		success = q->RegisterPatien(p);
    				system("CLS");
    				if (success)
    			    {
    	       			cout << "Patient added:"<<endl<<endl;
    
    			    }
    	        else
    		   {
    	       		// error
    	       		cout << "Sorry: The queue is full. We Cannot add any patient:";
    		   }
    	    		OutputPatient(&p);
    	    		cout << "Press any key";
    	    		getch();
    	  }
    	 break;
    
          case 2:   // Add critically ill patient
    	 p = InputPatient();
    	 if (p.ID[0])
    	     {
    	    	success = q->RegisterPatientAtBeginning(p);
    			system("CLS");
    
    			if (success)
    			{
    	       		  cout << "Patient added:";
    	     		}
    	 	  else
    	     		{
    	       		  // error
    	       		  cout << "Sorry: The queue is full. We Cannot add any patient:";
    			}
    
    			  OutputPatient(&p);
    	    		  cout << "Press any key to contiune";
    	    		  getch();
    	     }
    	 break;
    
          case 3:   // Call patient for operation /First Come First Surve
    	 p = q->GetNextPatient();
    	 system("CLS");
    	 if (p.ID[0])
    	    {
    	       cout << "Patient to operate:";
    	       OutputPatient(&p);
    	 }
    	 else
    	    {
    	       cout << "Currently there is no patient to operate.";
    	 }
    	       cout << "Press any key to contiune";
    	       getch();
    	       break;
    
          case 4:   // Remove a patient from queue
    	 p = InputPatient();
    	 if (p.ID[0])
    	   {
    	        success = q->CancelAll(&p);
    			system("CLS");			
    			if (success)
    		      {
    	       		cout << "Patient removed:";
    		      }
    	    	  else
    		      {
    	       		// error
    	       		cout << "Sorrt: We cannot find patient:";
    		      }
    	   	OutputPatient(&p);
    	    	cout << "Press any key to contiune";
    	    	getch();
    	   }
    	 break;
    
          case 5:   // Show all patient -> queues
    		  system("CLS");
    		  q->OutputList();
    	 cout << "Press any key";
    	 getch();  break;
    	 }
          }
    }
    
    
    // the main function defining queues and main menu
    void main ()
    {
       int i, MenuChoice = 0;
       // define  queue
       queue department[1];
       // set department name
       strcpy (department[0].DepartmentName, "To Emergency Department");
    
       while (MenuChoice != 2)
    	{
    		system("CLS");
    
    // Cout menu
          	   cout << "Welcome to Emergency Department (ED)"<<endl;
    		   cout << "Plrase Enter a Number from the follwing manu:"<<endl<<endl;
              for (i = 0; i < 1; i++)
    		{
    	 	   // write menu item for department i
    	 	   cout<< "" << (i+1) << ":  "<< department[i].DepartmentName;
    		   cout<<endl;
    		  }
          	  cout << "2:  Exit"<<endl;
          	  // get user choice
          	  MenuChoice = ReadNumber();
          	  // is it a department name?
          		if (MenuChoice >= 1 && MenuChoice <= 1)
    		    {
    	 		// call submenu for department
    	 		// (using pointer arithmetics here
    	 		DepartmentMenu (department + (MenuChoice-1));
    		    }
    	}
    }

    Saturday, February 18, 2012 7:27 AM

Answers

  • >i tried a lot. But i couldnt do it

    Then I suggest you review the course material.
    Everything you need to know in order to do this
    assignment will have been covered already.

    >i wish that i know how to create an Auto-increment number for each patient.

    Using a static class member variable is easy enough,
    as this example illustrates:

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;

    class ManageStaff {
      static int ID;
      public:
        int AddPerson(string);    
    };

    int ManageStaff::AddPerson(string str)
    {
      cout << str << " has an ID of " << ++ID << endl;
      return ID;   
    }

    int ManageStaff::ID = 0;

    int _tmain(int argc, _TCHAR* argv[])
    {
      ManageStaff personnel;
      personnel.AddPerson("Peter");
      personnel.AddPerson("Flopsy");
      personnel.AddPerson("Mopsy");
      personnel.AddPerson("Cotton-tail");

      return 0;
    }

    Output:

    Peter has an ID of 1
    Flopsy has an ID of 2
    Mopsy has an ID of 3
    Cotton-tail has an ID of 4

    - Wayne

    • Marked as answer by Helen Zhao Friday, February 24, 2012 8:03 AM
    Saturday, February 18, 2012 7:45 PM
  • Hi general07z,

    Thanks for your post in the MSDN Forum.

    I have been watching this issue for a while now. And I understand your issue. To help you get more information about auto-increment number, I created a Win32 console application to implement this task. Please refer to the following code sample for more information:

    static int num=0;//total number of patients.
    static int normalNum=0;//total number of normal patients.
    static int illNum=0;//total number of critically ill patients.
    void addNormal()
    {// every time a normal patient is added, num and normalNum will be increased
     num++;
     normalNum++;
    }
    void addIll()
    {// every time a critically ill patient is added, num and illNum will be increased
     num++;
     illNum++;
    }
    int main ()
    {
     for(int i=0;i<10;i++)
     {
      addNormal();
     }
     
     for(int i=0;i<10;i++)
     {
      addIll();
     }
     cout<<"total number of patients: "<<num<<endl;//num=20.
     cout<<"number of normal patients: "<<normalNum<<endl;//normalNum=10;
     cout<<"number of critically ill patients: "<<illNum<<endl;//illNum=10;
        return 0;
    }

    I hope this reply is helpful to you. If you have any questions, please feel free to let me know. By the way, please mark the reply which helps you as answer.

    Best regards,


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Helen Zhao Friday, February 24, 2012 8:03 AM
    Monday, February 20, 2012 2:37 AM

All replies

  • >just create an Auto-increment number for each patient

    So what's stopping you? You must know how to create an
    integer variable and how to increment it by one.

    If you want to make the number a member of your queue
    class then make it static. Add one to it each time a
    new patient is added.

    - Wayne
    Saturday, February 18, 2012 9:07 AM
  • hey,

    i wish that i know how to create an Auto-increment number for each patient.

    i tried a lot. But i couldnt do it

    static unsigned int ID;

    Where should i use this code?

    and what is the remaining to done this Auto-increment number for each patient?

    thanks alot

    Saturday, February 18, 2012 10:45 AM
  • >i tried a lot. But i couldnt do it

    Then I suggest you review the course material.
    Everything you need to know in order to do this
    assignment will have been covered already.

    >i wish that i know how to create an Auto-increment number for each patient.

    Using a static class member variable is easy enough,
    as this example illustrates:

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;

    class ManageStaff {
      static int ID;
      public:
        int AddPerson(string);    
    };

    int ManageStaff::AddPerson(string str)
    {
      cout << str << " has an ID of " << ++ID << endl;
      return ID;   
    }

    int ManageStaff::ID = 0;

    int _tmain(int argc, _TCHAR* argv[])
    {
      ManageStaff personnel;
      personnel.AddPerson("Peter");
      personnel.AddPerson("Flopsy");
      personnel.AddPerson("Mopsy");
      personnel.AddPerson("Cotton-tail");

      return 0;
    }

    Output:

    Peter has an ID of 1
    Flopsy has an ID of 2
    Mopsy has an ID of 3
    Cotton-tail has an ID of 4

    - Wayne

    • Marked as answer by Helen Zhao Friday, February 24, 2012 8:03 AM
    Saturday, February 18, 2012 7:45 PM
  • a static type like something kind of globle data for the class. that means it is shared by all the instance of this class.

    put it in ur definition of class.

    if auto +1 , u can use it as "class_name::var_name++";

    Sunday, February 19, 2012 3:46 PM
  • Hi general07z,

    Thanks for your post in the MSDN Forum.

    I have been watching this issue for a while now. And I understand your issue. To help you get more information about auto-increment number, I created a Win32 console application to implement this task. Please refer to the following code sample for more information:

    static int num=0;//total number of patients.
    static int normalNum=0;//total number of normal patients.
    static int illNum=0;//total number of critically ill patients.
    void addNormal()
    {// every time a normal patient is added, num and normalNum will be increased
     num++;
     normalNum++;
    }
    void addIll()
    {// every time a critically ill patient is added, num and illNum will be increased
     num++;
     illNum++;
    }
    int main ()
    {
     for(int i=0;i<10;i++)
     {
      addNormal();
     }
     
     for(int i=0;i<10;i++)
     {
      addIll();
     }
     cout<<"total number of patients: "<<num<<endl;//num=20.
     cout<<"number of normal patients: "<<normalNum<<endl;//normalNum=10;
     cout<<"number of critically ill patients: "<<illNum<<endl;//illNum=10;
        return 0;
    }

    I hope this reply is helpful to you. If you have any questions, please feel free to let me know. By the way, please mark the reply which helps you as answer.

    Best regards,


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Helen Zhao Friday, February 24, 2012 8:03 AM
    Monday, February 20, 2012 2:37 AM