Thread: How to implement a room booking system as part of a circular queue?

  1. #16
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by GeorgeIO View Post
    Yes, it is an update. My bad for not stating that. But your idea is perhaps a better one.

    Also, I'm struggling as to why the program isn't telling a patient to go straight to a room when a doctor is ready and the room is free. Like you said, it greets them twice, adds them to the queue telling them the Dr's are busy, to immediately then tell them to proceed to a room and removes them from the queue. I thought I had the positioning of the else-if's correctly in the enqueue function but obviously not
    well Now I am going to have to really take a closer look at this to figure out code flow logic behind this while you're prob be changing code while I'm trying to figure it out.
    **wonders if everyone else in here gave up helping on this one.

  2. #17
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    Thank you friend. Tell you what, I'll repost the updated code below so you have more of an idea of what's changed:

    Code:
    /*********************************************************************
    * I declare that the following program was written by me and that I
    * have NOT copied any part of this code from any other source.
    *
    * Name: George Strawbridge
    *
    * Email: [email protected]
    *
    * Date: 22/11/2017
    *
    * *********************************************************************/
    
    
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <stdbool.h> 
    #define MAX 6 //Size of queue array
    
    
    
    
    /*FUNCTION PROTOTYPES START*/
    
    
    void enqueue(int data);
    int dequeue();
    void position(int n);
    void print();
    void clear();
    void remove_nth(int n);
    void help(void);
    void status();
    void queue_display();
    void loading();
    void doctor_ready(int room_no);
    void doctor_away(int id);
    void patient_discharge(int n);
    void doctors();
    int cancel_appt(int patient_id);
    int check_if(int id);
    int first();
    int last();
    bool is_empty();
    bool is_full();
    
    
    /*FUNCTION PROTOTYPES FINISH*/
    
    
    /*GLOBAL VARIABLE DECLARATIONS START*/
    
    
    int queue[MAX]; //Queue for patients waiting
    int room_no[10][2]; //2d array for Doctors rooms
    int front = -1; //Front of queue
    int rear = -1; //Back of queue
    int num = -1;
    int pos = 1; //Position of a number in an array
    
    
    /*GLOBAL VARIABLE DECLARATIONS FINISH*/
    
    
    int main() {
    
    
       /*Patient Commands
       i-Patient check in
       p-Patient queue position
       q-Patient quit
       d-Available doctors
       h-Help - display commands*/
    
    
       /*Doctors Commands
       o-Paitient check out
       r-Doctor ready
       a-Doctor away
       w-Waiting list
       x-Exit*/
    
    
       /*Other
       s-Room status (for testing purposes)*/
    
    
       for (int i = 0; i < 10; i++)
          for (int j = 0; j < 2; j++) { //Initialise rooms to zero
             room_no[i][j] = 0;
          }
    
    
       /*WELCOME MESSAGE*/
    
    
       puts("Hello and welcome to Leeds Student Medical Practice");
       puts("Press RETURN to continue...");
       getchar();
       system("clear");
       loading();
       char choice; //Choice for switch statement
    
    
       do { //Do-While loop to keep switch statement running until exit of program
          puts("\nLeeds Student Medical Practice");
          puts("-------------");
          puts("Enter a command");
          puts("hint - to view available commands enter 'h'");
          printf(">>>");
    
    
          scanf(" %c", & choice);
          /*SWITCH START*/
          switch (choice) {
          case 'h':
             loading();
             system("clear");
             help();
             break;
    
    
          case 'i':
             loading();
             system("clear");
             int patient_id;
             scanf(" %d", & patient_id);
             enqueue(patient_id);
             break;
    
    
          case 'p':
             loading();
             system("clear");
             scanf("%d", & patient_id);
             position(patient_id);
             break;
    
    
          case 'q':
             loading();
             system("clear");
             scanf(" %d", & patient_id);
             cancel_appt(patient_id);
             break;
    
    
          case 'd':
             loading();
             system("clear");
             doctors();
             break;
    
    
          case 'o':
             loading();
             system("clear");
             scanf("%d", & patient_id);
             patient_discharge(patient_id);
             break;
    
    
          case 'r':
             loading();
             system("clear");
             int room_id;
             scanf("%d", & room_id);
             doctor_ready(room_id);
             break;
    
    
          case 'a':
             loading();
             system("clear");
             scanf("%d", & room_id);
             doctor_away(room_id);
             break;
    
    
          case 't':
             loading();
             system("clear");
             status();
             break;
    
    
          case 'w':
             loading();
             system("clear");
             print();
             break;
    
    
          case 'u':
             system("clear");
             queue_display();
             break;
    
    
          case 'x':
             loading();
             system("clear");
             exit(1);
             break;
    
    
          default:
             printf("Error, command not recognised. Please enter a valid command.\n");
             /*SWITCH FINISH*/
          }
       } while (choice != 'x'); //Do-while finish
    
    
       return 0;
    
    
    }
    
    
    /*HELP FUNCTION (Prints list of commands for user)*/
    
    
    void help() {
       puts("Patient commands");
       puts("----------------------");
       puts("i # - Check in on arrival, where # is your patient id number");
       puts("p #- Check position in queue");
       puts("q #- Cancel appointment");
       puts("d - View # of available Doctors");
       puts("x - Exit\n");
       puts("To access staff commands enter your 4 digit passcode:");
       int input_code, staff_code = 1234;
       printf(">>>");
       scanf("%d", &input_code);
          if (input_code == staff_code)
       system("clear");
       loading();
            printf("Access Granted\n");
       puts("Administrator commands only");
       puts("------------------------------");
       puts("o - Check patient out");
       puts("r - Call patient");
       puts("a - Doctor away");
       puts("w - View waiting list\n");
       puts("x - Exit\n");
    }
    
    
    /*QUEUE 'IS EMPTY' FUNCTION (Checks if queue is empty)*/
    
    
    bool is_empty() {
       if (front == -1 && rear == -1)
          return true;
       else
          return false;
    }
    
    
    /*QUEUE 'IS FULL' FUNCTION (Checks if queue is full)*/
    
    
    bool is_full() {
       if ((rear == MAX - 1 && front == 0) || (front == rear + 1))
          return true;
    
    
       else {
          return false;
       }
    }
    
    
    /*ENQUEUE FUNCTION (Inserts element at rear end)*/
    
    
    void enqueue(int data) {
       if (check_if(data) == 1)
          puts("You have checked in already! To view position in queue press 'p #', where # represents your ID number.");
       else if (is_full())
          puts("\nSorry, the waiting queue is full. Please try again later.");
       else {
          if (rear == MAX - 1 && front != 0)
             rear = -1;
          queue[++rear] = data;
          printf("\nWelcome Patient No. %d\nAll Doctors are busy right now.\nPlease relax in the waiting area.\n\n", data);
          if (front == -1)
             front = 0;
    
    
          for (int i = 0; i <= 10; i++) {
             if (room_no[i][0] == 1 && room_no[i][1] == 0) {
                printf("Welcome Patient No. %d\n Please come to room # %d.\n", data, i + 1);
                room_no[i][1] = data;
                dequeue();
                break;
             } else if (i == 10) {
                position(data);
                break;
             }
          }
       }
    }
    
    
    /*CHECK_IF FUNCTION (Checks if patient is already in the queue)*/
    
    
    int check_if(int id) {
       if (queue[rear] == id)
          return 1;
    
    
       else {
          for (int i = 0; i < 10; i++) {
             if (id == room_no[i][1])
                return 1;
          }
       }
       for (int i = front; i != rear; i++) {
          if (queue[i] == id) {
             return 1;
          } else if (i == MAX - 1)
             i = 0;
          else
             return 0;
       }
       return 0;
    }
    
    
    
    
    /*DEQUEUE FUNCTION (Removes element in queue from front end)*/
    
    
    int dequeue() {
       if (is_empty())
          puts("\nNothing in queue!!!");
       else {
          printf("\n%d has been removed from the queue.\n", queue[front++]);
          if (front == MAX)
             front = 0;
          if (front - 1 == rear)
             front = rear = -1;
       }
       return queue[front];
    }
    
    
    /*FIRST FUNCTION (Returns element at front of queue)*/
    
    
    int first() {
       if (front == -1) {
          puts("Error: cannot return front from empty queue");
          return -1;
       } else {
          printf("%d\n\n", queue[front]);
       }
       return 1;
    }
    
    
    /*LAST FUNCTION (Returns element at back of queue)*/
    
    
    int last() {
       if (rear == -1) {
          puts("Error: cannot return rear from empty queue.");
          return -1;
       } else {
          printf("%d\n\n", queue[rear]);
       }
       return 1;
    }
    
    
    /*PRINT FUNCTION (Prints out queue)*/
    
    
    void print() {
       if (front == -1)
          puts("\nNo patients are waiting");
       else {
          int i = front;
          puts("\nThe following patients are waiting: \n");
          if (front <= rear) {
             while (i <= rear)
                printf("%d\t", queue[i++]);
          } else {
             while (i <= MAX - 1)
                printf("%d\t", queue[i++]);
             i = 0;
             while (i <= rear)
                printf("%d\t", queue[i++]);
             printf("\n");
          }
       }
    }
    
    
    /*POSITION FUNCTION (Tells user their position in the queue)*/
    
    
    void position(int n) {
       for (int i = front; i <= rear; i++) {
    
    
          if (queue[i % MAX] == n) {
             printf("You are in the queue!\n");
             printf("Position in queue is %d \n", (i % MAX + 1) - front);
          }
       }
    }
    
    
    /*REMOVE NTH FUNCTION (Removes nth element from queue)*/
    
    
    void remove_nth(int n) {
       int i;
       if ((front == -1) && (rear == -1)) {
          puts("There are no patients waiting! Nothing to remove.");
          return;
       }
       for (i = 0; i <= rear; i++) {
          if (n == queue[i]) {
             for (; i < rear; i++) {
                queue[i] = queue[i + 1];
             }
             queue[i] = 0;
             rear--;
             if (rear == -1)
                front = -1;
             puts("Succesfully removed\n");
          }
       }
       printf("\n%d not found in queue to remove.\n\n", n);
    }
    
    
    /*STATUS FUNCTION (Checks if rooms are available or not)*/
    
    
    void status() {
       for (int i = 0; i < 10; i++) {
          printf("Room %i: ", i + 1);
          if (room_no[i][0] == 0)
             puts("Unoccupied");
          else if (room_no[i][1] == 0)
             puts("Dr is present without a patient");
          else
             printf("A Dr is in with patient no. %i\n", room_no[i][1]);
       }
    }
    
    
    /*DOCTORS FUNCTION (Displays the number of doctors currently available at the surgery, and a list of all examination rooms that are currently staffed)*/
    
    
    void doctors() {
       int doctor_count = 0;
       int rooms_ocup[10];
       for (int i = 0; i < 10; i++) {
          if (room_no[i][0] == 1) {
             rooms_ocup[doctor_count] = i;
             doctor_count++;
          }
       }
       if (doctor_count > 1) {
          printf("Currently there are %i Doctors in the Surgery. They are in rooms ", doctor_count);
          for (int i = 0; i < doctor_count - 1; i++)
             printf("%i, ", rooms_ocup[i]);
          printf("%i, ", rooms_ocup[doctor_count - 1]);
       } else printf("Currently there is 0 Doctors in the Surgery.");
    }
    
    
    /*DOCTOR READY FUNCTION (Allows doctor to declare they are ready to accept patients)*/
    
    
    void doctor_ready(int room_id) {
       int x = room_id - 1;
       for (int i = 0; i <= 10; i++) {
          if (i == x)
             room_no[i][0] = 1;
       }
       if (room_id > 10 || room_id < 1)
          puts("Wrong room! There are only ten rooms!");
       else if (is_empty() == 0) {
          if (room_no[x][1] == 0) {
             printf("Patient number %d, the Doctor will see you now. Please come to room #%d", queue[front], x + 1);
             room_no[x][1] = queue[front];
             dequeue();
          } else
             puts("Current patient needs to be discharged");
       } else
          puts("There are no patients waiting to be seen.");
    }
    
    
    /*DOCTOR AWAY FUNCTION (Allows doctor to declare they are leaving their room)*/
    
    
    void doctor_away(int id) {
       if (room_no[id - 1][1] != 0)
          puts("Please discharge your patient before leaving.");
       else
          room_no[id - 1][0] = 0;
    }
    
    
    /*DISCHARGE PATIENT FUNCTION (Checks patient out of a room)*/
    
    
    void patient_discharge(int n) {
       for (int i = 0; i <= 10; i++) {
          if (i == 10)
             puts("That patient isn't in any room!");
          else if (room_no[i][1] == n) {
             room_no[i][1] = 0;
             puts("Patient successfully discharged.");
             break;
          }
       }
    }
    
    
    /*CANCEL FUNCTION (Allows patient to cancel appt. (similair to remove_nth))*/
    
    
    int cancel_appt(int n) {
       int i;
       if ((front == -1) && (rear == -1)) {
          puts("No patients waiting! Nothing to cancel.");
       }
       for (i = 0; i <= rear; i++) {
          if (n == queue[i]) {
             for (; i < rear; i++) {
                queue[i] = queue[i + 1];
             }
             queue[i] = 0;
             rear--;
             if (rear == -1)
                front = -1;
             puts("Appointment successfully canceled\n");
          }
       }
       printf("\n%d not found in queue to delete\n\n", n);
       return 0;
    }
    
    
    /*QUEUE DISPLAY FUNCTION (Displays array)*/
    
    
    void queue_display() {
       printf("First: %d Last: %d \n\n", front, rear);
       for (int i = 0; i < MAX; i++) {
          if (i == front)
             puts("(First->)");
          if (i == rear)
             puts("(Last->)");
          printf("%d\t", queue[i]);
       }
       printf("\n\n");
    }
    
    
    /*CLEAR FUNCTION (Clears entire queue)*/
    
    
    void clear() {
       front = -1;
       rear = -1;
    }
    
    
    /*LOADING FUNCTION (Adds loading effect)*/
    
    
    void loading() {
       puts("Loading...\n");
       //For loop to add delay to give loading effect
       for (int c = 1; c <= 32767; c++)
          for (int k = 1; k <= 32767; k++) {}
    }

  3. #18
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    Also, when I try and run the code on Wandbox it goes into an infinite loop. I've double checked the settings are they all seem to be correct.

  4. #19
    Banned
    Join Date
    Aug 2017
    Posts
    861
    first use take a look ...
    Code:
    Leeds Student Medical Practice
    Patient commands
    ----------------------
    i # - Check in on arrival, where # is your patient id number
    p #- Check position in queue
    q #- Cancel appointment
    d - View # of available Doctors
    -------------
    Enter a command
    hint - to view available commands enter 'h'
    >>>i 1
    Loading...
    
    Welcome Patient No. 1
    All Doctors a busy right now.
    Please relax in the waiting area.
    
    HELLO guess what...
    Welcome Patient No. 1
     Please come to room # 11.
    
    1 has been removed from the queue.
    
    Leeds Student Medical Practice
    Patient commands
    what's this?
    first input say id = 1
    everything is set to -1
    Code:
     int front = -1; //Front of queue
            int rear = -1;  //Back of queue
            int num = -1;
            int pos = 1; //Position of a number in an array
    then it goes to the checks get to where I put in a tell tell printf
    now take a look at what is happening there.
    Code:
            void enqueue(int data) {
                  if (check_if(data) == 1)
                     puts("You have checked in already!");
                  else if (is_full())
                     puts("\nSorry, the waiting queue is full. Please try again later.");
                  else {
                     if (rear == MAX - 1 && front != 0)
                        rear = -1;
                     queue[++rear] = data;
                     printf("\nWelcome Patient No. %d\nAll Doctors a busy right now.\nPlease relax in the waiting area.\n\n", data);
                     if (front == -1)
                        front = 0;
                   printf("HELLO guess what...\n");
                     for (int i = 0; i <= 10; i++) {
                        if (room_no[i][0] == 1 && room_no[i][1] == 0) {
                           printf("Welcome Patient No. %d\n Please come to room # %d.\n", data, i + 1);
                           room_no[i][1] = data;
                           dequeue();
                           break;
                        } else if (i == 10) {
                           position(data);
                           break;
                        }
                     }
                  }
            }

  5. #20
    Banned
    Join Date
    Aug 2017
    Posts
    861
    you need to change your message placement to here
    Code:
     do { //Do-While loop to keep switch statement running until exit of program
                    puts("\nLeeds Student Medical Practice");
                    puts("Patient commands");  
                    puts("----------------------");
                    puts("i # - Check in on arrival, where # is your patient id number");
                    puts("p #- Check position in queue");
                    puts("q #- Cancel appointment");
                    puts("d - View # of available Doctors");
                    puts("-------------");
                    puts("Enter a command");
                    puts("hint - to view available commands enter 'h'");
                    printf(">>>");
                 
                    scanf(" %c", &choice);
                /*SWITCH START*/
    this works too notice the difference
    Code:
     do { //Do-While loop to keep switch statement running until exit of program
                    printf("\nLeeds Student Medical Practice\n"
                            "Patient commands\n"  
                            "----------------------\n"
                            "i # - Check in on arrival, where # is your patient id number\n"
                            "p #- Check position in queue\n"
                            "q #- Cancel appointment\n"
                            "d - View # of available Doctors\n"
                            "-------------\n"
                            "Enter a command\n"
                            "hint - to view available commands enter 'h'\n"
                            ">>>");
                 
                    scanf(" %c", &choice);
    Last edited by userxbw; 11-22-2017 at 01:16 PM.

  6. #21
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    Think I got it working. Give it a go:

    Code:
    void enqueue(int data) {   if (check_if(data) == 1) {
          puts("You have checked in already! To view position in queue press 'p #', where # represents your ID number.");
       } else if (is_full())
          puts("\nSorry, the waiting queue is full. Please try again later.");
    
    
       else if (is_empty()) {
          front = 0;
          rear = 0;
          queue[rear] = data;
       } else if (rear == MAX - 1 && front != 0) {
          rear = 0;
          queue[rear] = data;
          printf("\nWelcome Patient No. %d\nAll Doctors are busy right now.\nPlease relax in the waiting area.\n\n", data);
       } else {
          queue[++rear] = data;
       }
       for (int i = 0; i <= 10; i++) {
          if (room_no[i][0] == 1 && room_no[i][1] == 0) {
             printf("Welcome Patient No. %d\n Please come to room # %d.\n", data, i + 1);
             room_no[i][1] = data;
             dequeue();
             break;
          } else if (i == 10) {
             position(data);
             break;
          }
       }
    }

  7. #22
    Banned
    Join Date
    Aug 2017
    Posts
    861
    first off I'd remove your password thing it is not a required thing in your homework. and did you place your message in your loop so it will show up when ran?

  8. #23
    Banned
    Join Date
    Aug 2017
    Posts
    861
    this is what I am using of your code in post # 3 , that password thing removed, not really needed it is of part of your home work, is it?
    Fixed your first print out for commands to screen , fixed your returns warnings by adding return 0 in all of them, and replaced that printf with your fixed one.
    added your redone function
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <stdbool.h> 
    #define MAX 6 //Size of queue array
     
     
     
    /*FUNCTION PROTOTYPES START*/
     
     
    void enqueue(int data);
    int dequeue();
    int position(int n);
    void print();
    void clear();
    void remove_nth(int n);
    void help(void);
    void status();
    void queue_display();
    void loading();
    void cancel_appt();
    void doctor_ready(int room_no);
    void doctor_away(int id);
    void patient_discharge(int n);
    void doctors();
    int check_if(int id);
    int first();
    int last();
    bool is_empty();
    bool is_full();
     
     
    /*FUNCTION PROTOTYPES FINISH*/
     
     
    /*GLOBAL VARIABLE DECLARATIONS START*/
     
     
    int queue[MAX]; //Queue for patients waiting
    int room_no[10][2]; //2d array for Doctors rooms
    int front = -1; //Front of queue
    int rear = -1;  //Back of queue
    int num = -1;
    int pos = 1; //Position of a number in an array
     
     
    /*GLOBAL VARIABLE DECLARATIONS FINISH*/
     
     
    int main(int argc, char * * argv) {
     
     
        /*Patient Commands
        i-Patient check in
        p-Patient queue position
        q-Patient quit
        d-Available doctors
        h-Help - display commands*/
     
     
        /*Doctors Commands
        o-Paitient check out
        r-Doctor ready
        a-Doctor away
        w-Waiting list
        x-Exit*/
     
     
        /*Other
        s-Room status (for testing purposes)*/
         
        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 2; j++){   //Initialise rooms to zero
              room_no[i][j] = 0;
            }
             
            /*WELCOME MESSAGE*/
         
        puts("Hello and welcome to Leeds Student Medical Practice");
        puts("Press RETURN to continue...");
        getchar();
        system("clear");
        loading();
      char choice; //Choice for switch statement
       
        do { //Do-While loop to keep switch statement running until exit of program
              printf("\nMo's Surgery\n"
                    "-------------\n"
                    "Patient commands\n"   
                    "----------------------\n"
                    "i # - Check in on arrival, where # is your patient id number\n"
                    "p #- Check position in queue\n"
                    "q #- Cancel appointment\n"
                    "d - View # of available Doctors\n"
                    "Enter a command\n"
                    "hint - to view available commands enter 'h'\n"
                    ">>>");
            
            scanf(" %c", & choice);
        /*SWITCH START*/
            switch (choice) {
            case 'h':
                loading();
                system("clear");
                help();
                break;
     
     
            case 'i':
                loading();
                system("clear");
                int patient_id; 
                scanf(" %d", &patient_id);
                enqueue(patient_id);
                break;
     
     
            case 'p':
                loading();
                system("clear");
                scanf("%d", &patient_id);
                position(patient_id);
                break;
     
     
            case 'q':
                loading();
                system("clear");
                scanf(" %d", &patient_id);
                cancel_appt(patient_id);
                break;
     
     
            case 'd':
                loading();
                system("clear");
                doctors();
                break;
     
     
            case 'o':
                loading();
                system("clear");
                scanf("%d", &patient_id);
                remove_nth(patient_id);
                break;
     
     
            case 'r':
                loading();
                system("clear");
                int room_id;
                scanf("%d", &room_id);
                doctor_ready(room_id);
                break;
     
     
            case 'a':
                loading();
                system("clear");
                scanf("%d", &room_id);
                doctor_away(room_id);
                break;
     
     
            case 's':
                loading();
                system("clear");
                status();
                break;
     
     
            case 'w':
                loading();
                system("clear");
                print();
                break;
     
     
            case 'u':
                queue_display();
                break;
     
     
            case 'x':
                loading();
                system("clear");
                exit(1);
                break;
     
     
            default:
                printf("Error, command not recognised. Please enter a valid command.\n");
            /*SWITCH FINISH*/
            }
        } while (choice != 'x'); //Do-while finish
     
     
        return 0;
     
     
    }
     
     
    /*HELP FUNCTION (Prints list of commands for user)*/
     
     
    void help() {
        puts("Patient commands");
        puts("----------------------");
        puts("i - Check in on arrival");
        puts("p - Check position in queue");
        puts("q - Cancel appointment");
        puts("d - View # of available Doctors\n");
        puts("Administrator commands only");
        puts("------------------------------");
        puts("o - Check patient out");
        puts("r - Call patient");
        puts("a - Doctor away");
        puts("w - View waiting list\n");
        puts("Other");
        puts("----------");
        puts("x - Exit\n");
    }
     
     
    /*QUEUE 'IS EMPTY' FUNCTION (Checks if queue is empty)*/
     
     
    bool is_empty() {
        if (front == -1 && rear == -1)
            return true;
        else
            return false;
    }
     
     
    /*QUEUE 'IS FULL' FUNCTION (Checks if queue is full)*/
     
     
    bool is_full() {
        if ((rear == MAX - 1 && front == 0) || (front == rear + 1))
            return true;
     
     
        else {
            return false;
        }
    }
     
     
    /*ENQUEUE FUNCTION (Inserts element at rear end)*/
     
     /*
    void enqueue(int data) {
          if (check_if(data) == 1)
             puts("You have checked in already!");
          else if (is_full())
             puts("\nSorry, the waiting queue is full. Please try again later.");
          else {
             if (rear == MAX - 1 && front != 0)
                rear = -1;
             queue[++rear] = data;
             printf("\nWelcome Patient No. %d\nAll Doctors a busy right now.\nPlease relax in the waiting area.\n\n", data);
             if (front == -1)
                front = 0;
           
             for (int i = 0; i <= 10; i++) {
                if (room_no[i][0] == 1 && room_no[i][1] == 0) {
                   printf("Welcome Patient No. %d\n Please come to room # %d.\n", data, i + 1);
                   room_no[i][1] = data;
                   dequeue();
                   break;
                } else if (i == 10) {
                   position(data);
                   break;
                }
             }
          }
    }
     ***/
     void enqueue(int data) {   if (check_if(data) == 1) {
          puts("You have checked in already! To view position in queue press 'p #', where # represents your ID number.");
       } else if (is_full())
          puts("\nSorry, the waiting queue is full. Please try again later.");
     
     
       else if (is_empty()) {
          front = 0;
          rear = 0;
          queue[rear] = data;
       } else if (rear == MAX - 1 && front != 0) {
          rear = 0;
          queue[rear] = data;
          printf("\nWelcome Patient No. %d\nAll Doctors are busy right now.\nPlease relax in the waiting area.\n\n", data);
       } else {
          queue[++rear] = data;
       }
       for (int i = 0; i <= 10; i++) {
          if (room_no[i][0] == 1 && room_no[i][1] == 0) {
             printf("Welcome Patient No. %d\n Please come to room # %d.\n", data, i + 1);
             room_no[i][1] = data;
             dequeue();
             break;
          } else if (i == 10) {
             position(data);
             break;
          }
       }
    }
     
    int check_if(int id) {
       if (queue[rear] == id)
          return 1;
     
     
       else {
          for (int i = 0; i < 10; i++) {
             if (id == room_no[i][1])
                return 1;
          }
       }
       for (int i = front; i != rear; i++) {
          if (queue[i] == id) {
             return 1;
          } else if (i == MAX - 1)
             i = 0;
          else
             return 0;
       }
       return 0;
    }
     
     
     
     
    /*DEQUEUE FUNCTION (Removes element in queue from front end)*/
     
     
    int dequeue() {
        if (is_empty())
            puts("\nNothing in queue!!!");
        else {
            printf("\n%d has been removed from the queue.\n", queue[front++]);
            if (front == MAX)
                front = 0;
            if (front - 1 == rear)
                front = rear = -1;
        }
        return queue[front];
    }
     
     
    /*FIRST FUNCTION (Returns element at front of queue)*/
     
     
    int first() {
        if (front == -1) {
            puts("Error: cannot return front from empty queue");
            return -1;
        }
        else {
            printf("%d\n\n", queue[front]);
        }
        return 0;
    }
     
     
    /*LAST FUNCTION (Returns element at back of queue)*/
     
     
    int last() {
        if (rear == -1) {
            puts("Error: cannot return rear from empty queue");
            return -1;
        }
        else {
            printf("%d\n\n", queue[rear]);
        }
        return 0;
    }
     
     
    /*PRINT FUNCTION (Prints out queue)*/
     
     
    void print() {
        if (front == -1)
            puts("\nNothing to display");
        else {
            int i = front;
            puts("\nThe following patients are waiting: \n");
            if (front <= rear) {
                while (i <= rear)
                    printf("%d\t", queue[i++]);
            }
            else {
                while (i <= MAX - 1)
                    printf("%d\t", queue[i++]);
                i = 0;
                while (i <= rear)
                    printf("%d\t", queue[i++]);
                printf("\n");
            }
        }
    }
     
     
    /*POSITION FUNCTION (Tells user their position in the queue)*/
     
     
    int position(int n) {
        for (int i = front; i <= rear; i++)
        {
     
     
            if (queue[i % MAX] == n) {
                printf("You are in the queue!\n");
                printf("Position in queue is %d \n", (i%MAX + 1) - front);
                return 0;
            }
        }
        return 0;
    }
     
     
    /*REMOVE NTH FUNCTION (Removes nth element from queue)*/
     
     
    void remove_nth(int n) {
        int i;
        if ((front == -1) && (rear == -1)) {
            puts("Queue is empty! Nothing to remove.");
            return;
        }
        for (i = 0; i <= rear; i++) {
            if (n == queue[i]) {
                for (; i < rear; i++) {
                    queue[i] = queue[i + 1];
                }
                queue[i] = 0;
                rear--;
                if (rear == -1)
                    front = -1;
                puts("Succesfully deleted\n");
                return;
            }
        }
        printf("\n%d not found in queue to delete\n\n", n);
    }
     
     
    /*STATUS FUNCTION (Checks if rooms are available or not)*/
     
     
    void status() {
        for (int i = 0; i < 10; i++) {
          printf("Room %i: ", i + 1);
          if (room_no[i][0] == 0)
            puts("Unoccupied");
              else if (room_no[i][1] == 0)
                puts("Dr is present with no patient");
              else
                printf("A Dr is in with patient no. %i\n", room_no[i][1]);
            }
    }
     
     
    /*DOCTORS FUNCTION (Displays the number of doctors currently available at the surgery, and a list of all examination rooms that are currently staffed)*/
     
     
    void doctors() {
       int doctor_count = 0;
       int rooms_ocup[10];
       for (int i = 0; i < 10; i++) {
          if (room_no[i][0] == 1) {
             rooms_ocup[doctor_count] = i;
             doctor_count++;
          }
       }
       if (doctor_count > 1) {
          printf("Currently there are %i Doctors in the Surgery. They are in rooms ", doctor_count);
          for (int i = 0; i < doctor_count - 1; i++)
             printf("%i, ", rooms_ocup[i]);
          printf("%i, ", rooms_ocup[doctor_count - 1]);
       } else printf("Currently there is 1 Doctor in the Surgery. They are in room %i.\n", rooms_ocup[0]);
    }
     
     
    /*DOCTOR READY FUNCTION (Allows doctor to declare they are ready to accept patients)*/
     
     
    void doctor_ready(int room_id) {
       int x = room_id - 1;
       for (int i = 0; i <= 10; i++) {
          if (i == x)
             room_no[i][0] = 1;
       }
       if (room_id > 10 || room_id < 1)
          puts("Wrong room! There are only ten rooms!");
       else if (is_empty() == 0) {
          if (room_no[x][1] == 0) {
            printf("Patient number %d, the Doctor will see you now. Please come to room #%d", queue[front], room_id);
             room_no[x][1] = queue[front];
             dequeue();
          } else
             puts("Current patient needs to be discharged");
       } else
          puts("There are no patients waiting to be seen.");
    }
     
     
    /*DOCTOR AWAY FUNCTION (Allows doctor to declare they are leaving their room)*/
     
     
    void doctor_away(int id) {
      if(room_no[id - 1][1] != 0)
        puts("Please discharge your patient before leaving.");
      else
        room_no[id - 1][0] = 0;
    }
         
     
     
    /*DISCHARGE PATIENT FUNCTION (Checks patient out of a room)*/
     
     
    void patient_discharge(int n){
      for(int i = 0; i <= 10; i++){
         if(i == 10)
            puts("That patient isn't in any room!");
         else if (room_no[i][1] == n) {
            room_no[i][1] = 0;
          break;
       }
     }
    }
     
     
    /*CANCEL FUNCTION (Allows patient to cancel appt. (similair to remove_nth))*/
     
     
    void cancel_appt(int n) {
        int i;
        if ((front == -1) && (rear == -1)) {
            puts("Queue is empty! Nothing to cancel.");
            return;
        }
        for (i = 0; i <= rear; i++) {
            if (n == queue[i]) {
                for (; i < rear; i++) {
                    queue[i] = queue[i + 1];
                }
                queue[i] = 0;
                rear--;
                if (rear == -1)
                    front = -1;
                puts("Appointment successfully canceled\n");
                return;
            }
        }
        printf("\n%d not found in queue to delete\n\n", n);
    }
     
     
    /*QUEUE DISPLAY FUNCTION (Displays array)*/
     
     
    void queue_display() {
      printf("First: %d Last %d \n", front, rear);
        for (int i = 0; i < MAX; i++){
          if(i == front)
            puts("(First->)");
            if(i == rear)
            puts("(Last->)");
            printf("%d\t", queue[i]);
    }
    printf("\n\n");
    }
     
     
    /*CLEAR FUNCTION (Clears entire queue)*/
    void clear(){
      front = -1;
      rear = -1;
    }
     
     
    void loading() {
        puts("Loading...\n");
        //For loop to add delay to give loading effect
        for (int c = 1; c <= 32767; c++)
            for (int k = 1; k <= 32767; k++) {}
    }
    this is the out put for first run adding first patent
    Code:
    userx@slackwhere:~/bin
    $ ./doctor_queue
    Hello and welcome to Leeds Student Medical Practice
    Press RETURN to continue...
    
    
    Loading...
    
    
    Mo's Surgery
    -------------
    Patient commands
    ----------------------
    i # - Check in on arrival, where # is your patient id number
    p #- Check position in queue
    q #- Cancel appointment
    d - View # of available Doctors
    Enter a command
    hint - to view available commands enter 'h'
    >>>i 1
    Loading...
    
    
    Welcome Patient No. 1
     Please come to room # 11.
    
    1 has been removed from the queue.
    
    Mo's Surgery
    -------------
    Patient commands
    ----------------------
    i # - Check in on arrival, where # is your patient id number
    p #- Check position in queue
    q #- Cancel appointment
    d - View # of available Doctors
    Enter a command
    hint - to view available commands enter 'h'
    >>>
    how you can see what it is doing, you're the programmer for this ... the ball is in your court. I'm just a tester
    Last edited by userxbw; 11-22-2017 at 02:22 PM.

  9. #24
    Banned
    Join Date
    Aug 2017
    Posts
    861
    oh my
    Code:
    
    You are in the queue!
    Position in queue is 3 
    
    Mo's Surgery
    -------------
    Patient commands
    ----------------------
    i # - Check in on arrival, where # is your patient id number
    p #- Check position in queue
    q #- Cancel appointment
    d - View # of available Doctors
    Enter a command
    hint - to view available commands enter 'h'
    >>>p
    Loading...
    // stuck here and is still here just saying loading .....
    added a few more then selected check position went to this and is hanging or wanting input? cannot tell no output to screen to know?
    Last edited by userxbw; 11-22-2017 at 02:30 PM.

  10. #25
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    I appreciate all the help and advice, I really do! Also the p command needs to be followed by the patient id number, which I see you missed off. Ideally I would have an error message return every time the user does something like that, stating that they need to include their id number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program to implement queue operations using linear queue
    By riteshkrjain in forum C Programming
    Replies: 2
    Last Post: 10-03-2017, 10:57 AM
  2. How to implement a destructor for a dynamic circular queue?
    By garmbrust in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2015, 08:27 AM
  3. Hotel Booking system
    By TenTierHook in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2010, 06:53 AM

Tags for this Thread