title: Auto generate ID in C++
body: <pre><code>// AnE.cpp : Defines the entry point for the console application.
  //
  
  #include "stdafx.h"
  #include&lt;conio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;iostream&gt;
  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 &gt;= 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 &gt;= MAXPATIENTS)
     {
        // queue is full
        return 0;
     }
  
     // move all patients one position back in queue
     for (i = ShowAllPatient-1; i &gt;= 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&lt;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&lt;ShowAllPatient; i++)
      {
              if (stricmp(List[i].ID, p-&gt;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&lt;ShowAllPatient; j++)
                  {
                          List[j] = List[j+1];
                  }
          }
      }
     return found;
  }
  
  
  void queue::OutputList (void)
  {
     // lists entire queue on screen
     int i;
     if (ShowAllPatient == 0)
      {
              cout&lt;&lt; "Queue is empty";
      }
     else
      {
  
          for (i=0; i&lt;ShowAllPatient; i++)
          {
              cout &lt;&lt; "First Name : " &lt;&lt; List[i].FirstName&lt;&lt;endl;
              cout &lt;&lt; "Last Name : " &lt;&lt; List[i].LastName&lt;&lt;endl;
          }
      }
  }
  
  
  // declare functions used by main:
  
  patient InputPatient (void)
  
  {
     // this function asks user for patient data.
     patient p;
     cout&lt;&lt;endl&lt;&lt;endl;
     cout &lt;&lt; "Please enter the information of the Patient"&lt;&lt;endl&lt;&lt;endl;
     cout &lt;&lt; "First name: "&lt;&lt;endl&lt;&lt;endl;
     cin.getline(p.FirstName, sizeof(p.FirstName));
     cout &lt;&lt; "Last name: "&lt;&lt;endl&lt;&lt;endl;
     cin.getline(p.LastName, sizeof(p.LastName));
     // check if data valid
     if (p.FirstName[0]==0 || p.LastName[0]==0 || p.ID[0]==0)
      {
              // rejected
              strcpy(p.ID,"");
              cout &lt;&lt; "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-&gt;ID[0]==0)
      {
              cout &lt;&lt; "No patient";
  
      return;
      }
     else
     cout &lt;&lt; "Patient Information:"&lt;&lt;endl&lt;&lt;endl;
     cout &lt;&lt; "First name: " &lt;&lt; p-&gt;FirstName&lt;&lt;endl&lt;&lt;endl;
     cout &lt;&lt; "Last name: " &lt;&lt; p-&gt;LastName&lt;&lt;endl&lt;&lt;endl;
  }
  
  
  int ReadNumber()
  {
     // this function reads an integer number from the keyboard.
     // it is used because input with cin &gt;&gt; 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)
      {
          // print menu
             system("CLS");
             cout &lt;&lt; "&lt;&lt; || Welcome || &gt;&gt; "&lt;&lt;endl &lt;&lt; q-&gt;DepartmentName&lt;&lt;endl;
             cout &lt;&lt; "Please enter your choice:"&lt;&lt;endl&lt;&lt;endl;
             cout &lt;&lt; "1:  Register patient"&lt;&lt;endl;
             cout &lt;&lt; "2:  Serve patient "&lt;&lt;endl;
             cout &lt;&lt; "3:  Cancel all patients from queue"&lt;&lt;endl;
             cout &lt;&lt; "4:  Show all patient"&lt;&lt;endl;
             cout &lt;&lt; "5:  Exit"&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl;
  
             choice = ReadNumber();
  
          switch (choice)
        {
              case 1:   // Add new patient
          p = InputPatient();
          if (p.ID[0])
             {
                  success = q-&gt;RegisterPatien(p);
                  system("CLS");
                  if (success)
                  {
                      cout &lt;&lt; "Patient added:"&lt;&lt;endl&lt;&lt;endl;
  
                  }
              else
             {
                  // error
                  cout &lt;&lt; "Sorry: The queue is full. We Cannot add any patient:";
             }
                  OutputPatient(&amp;p);
                  cout &lt;&lt; "Press any key";
                  getch();
        }
       break;
  
        case 2:   // Call patient for operation /First Come First Surve
       p = q-&gt;GetNextPatient();
       system("CLS");
       if (p.ID[0])
          {
             cout &lt;&lt; "Patient to operate:";
             OutputPatient(&amp;p);
       }
       else
          {
             cout &lt;&lt; "Currently there is no patient to operate.";
       }
             cout &lt;&lt; "Press any key to contiune";
             getch();
             break;
  
        case 3:   // Cancel all from queue
       p = InputPatient();
       if (p.ID[0])
         {
              success = q-&gt;CancelAll(&amp;p);
              system("CLS");          
              if (success)
                {
                  cout &lt;&lt; "Patient removed:";
                }
                else
                {
                  // error
                  cout &lt;&lt; "Sort: We cannot find patient:";
                }
          OutputPatient(&amp;p);
              cout &lt;&lt; "Press any key to contiune";
              getch();
         }
       break;
  
        case 4:   // Show all patient -&gt; queues
            system("CLS");
            q-&gt;OutputList();
       cout &lt;&lt; "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_s (department[0].DepartmentName, "To Emergency Department");
  
     while (MenuChoice != 2)
      {
          system("CLS");
  
  // Cout menu
             cout&lt;&lt;"\n------------------------------------\n";
             cout &lt;&lt; "Welcome to Waiting Room Management System"&lt;&lt;endl;
             cout&lt;&lt;"---------------------------------------\n";
             cout &lt;&lt; "Please Select a Number from the following menu:"&lt;&lt;endl&lt;&lt;endl;
            for (i = 0; i &lt; 1; i++)
  
      {
             // write menu item for department i
             cout&lt;&lt; "" &lt;&lt; (i+1) &lt;&lt; ":  "&lt;&lt; department[i].DepartmentName;
             cout&lt;&lt;endl;
            }
            cout &lt;&lt; "2:  Exit"&lt;&lt;endl;
            // get user choice
            MenuChoice = ReadNumber();
            // is it a department name?
              if (MenuChoice &gt;= 1 &amp;&amp; MenuChoice &lt;= 1)
              {
              // call submenu for department
              // (using pointer arithmetics here:)
              DepartmentMenu (department + (MenuChoice-1));
              }
      }
  }
  </code></pre>
  
  <p>Okay, it's Vc++ for waiting room. You can see the code is working well, but I have problem with generating ID! I need to generate ID for each patient (auto-generate by system). How I can generate ID for my queue?
  Thanks alot !</p>