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

  1. #1
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33

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

    Hi all. I am currently at a standstill with the project I am working on. The context is as follows:

    A Doctors surgery needs a computer program to provide self-service to patients. Patients use this console to check in on arrival to the surgery. They can also use the console to enquire about their position in the waiting list, or to find out how many doctors are currently available at the surgery. The program is also used by doctors to check out (discharge) patients when they have finished examining them. Doctors use the program as well to check in and out of their rooms.The program must maintain a waiting list (queue) of all checked in patients, and as soon as one of the doctors is free, the program must call on the next patient in the queue by displaying a message.

    I have managed to implement the queue, which I will post below so you can see what functions I've used for it.

    What I'm struggling with is how I would tie this in to the ten rooms with Doctors. I want to create functions that do the following:

    -A command that displays the number of doctors currently available at the surgery, and a list of all examination rooms that are currently staffed

    -A command used by a doctor when they arrive at their room and they are ready to see patients. When this command is executed, the program must automatically call on the next patient in the queue, and instruct them to proceed to a room

    -A command is used by doctors to check out (or formally discharge) a patient from the surgery.

    I have a function (remove_nth in my code) that can remove specific elements of the queue defined by the user. I was thinking I could use this to discharge patients, but I'm just a bit stumped as to how to tie everything together. Any help would be greatly appreciated. Thank you.

    Code:
    
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <stdbool.h> 
    #define MAX 6
    
    
    /*FUNCTION PROTOTYPES START*/
    
    
    void insert(int data);
    void dequeue();
    void position(int n);
    void print();
    void remove_nth(int n);
    void help(void);
    void status();
    void queue_display();
    void loading();
    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] = {1,2,3,4,5,6,7,8,9,10}; //Array for Doctors rooms
    int room;
    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) {
       //Declare our variables
    
    
       /*Patient Commands*/
       char i; //Patient check in
       char p; //Patient queue position
       char q; //Patient quit
       char d; //Available doctors
       char h; //Help - display commands
       /*Doctors Commands*/
       char o; //Paitient check out
       char r; //Doctor ready
       char a; //Doctor away
       char w; //Waiting list
       char x; //Exit
       /*Other*/
       char choice;
       char s; //Room status (for testing purposes)
       int id; //Argument for functions
    
    
       printf("Hello and welcome to Mo's Surgery\n");
       printf("Press RETURN to continue...\n");
       getchar();
       system("clear");
       printf("Loading...\n\n");
       //For loop to add delay to give loading effect
       int c = 1, k = 1;
       for (c = 1; c <= 32767; c++)
          for (k = 1; k <= 32767; k++) {}
    
    
       do {
          printf("\nMo's Surgery\n");
          printf("-------------\n");
          printf("Enter a command\n");
          printf("hint - to view available commands enter 'h'\n");
          printf(">>>");
          scanf(" %c", & choice);
    
    
          switch (choice) {
          case 'h':
             loading();
             system("clear");
             help();
             break;
    
    
          case 'i':
             loading();
             system("clear");
             scanf(" %d", & id);
             insert(id);
             break;
    
    
          case 'p':
             loading();
             system("clear");
             scanf("%d", & id);
             position(id);
             break;
    
    
          case 'q':
             loading();
             system("clear");
             scanf(" %d", & id);
             cancel(id);
             break;
    
    
          case 'd':
             loading();
             system("clear");
    
    
             break;
    
    
          case 'o':
             loading();
             system("clear");
             scanf("%d", & id);
             remove_nth(id);
             break;
    
    
          case 'r':
             loading();
             system("clear");
             dequeue();
             break;
    
    
          case 'a':
             loading();
             system("clear");
    
    
             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");
          }
       } while (choice != 'x');
    
    
       return 0;
    
    
    }
    
    
    /*HELP FUNCTION */
    
    
    void help() {
       printf("Patient commands\n");
       printf("----------------------\n");
       printf("i - Check in on arrival\n");
       printf("p - Check position in queue\n");
       printf("q - Cancel appointment\n");
       printf("d - View # of available Doctors\n\n");
       printf("Administrator commands only\n");
       printf("------------------------------\n");
       printf("o - Check patient out\n");
       printf("r - Call patient\n");
       printf("a - Doctor away\n");
       printf("w - View waiting list\n\n");
       printf("Other\n");
       printf("----------\n");
       printf("x - Exit\n\n");
    }
    
    
    /*QUEUE 'IS EMPTY' FUNCTION*/
    
    
    bool is_empty() {
       if (front == -1 && rear == -1)
          return true;
       else
          return false;
    }
    
    
    /*QUEUE 'IS FULL' FUNCTION*/
    
    
    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 insert(int data) {
      if (is_full())
        printf("\nSorry, the waiting queue is full. Please try again later.\n");
      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 = front; i <= rear; i++) {
        if (queue[i % MAX] == data) {
          printf("Position in queue is %d \n\n", (i % MAX + 1) - front);
        } else {
          for (int i = 0; i < 6; i++) {
            if (data = queue[i])
              printf("You are already checked in!\n");
            printf("Position in queue is %d \n\n", (rear + MAX - front) % MAX + 1);
          }
        }
    
    
      }
    
    
    }
    
    
    /*DEQUEUE FUNCTION (Removes element in queue from front end)*/
    
    
    void dequeue() {
       if (is_empty())
          printf("\nNothing in queue!!!\n");
       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;
       }
    }
    
    
    /*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]);
       }
    }
    
    
    /*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]);
       }
    }
    
    
    /*PRINT FUNCTION (Prints out queue)*/
    
    
    void print() {
       if (front == -1)
          printf("\nNothing to display\n");
       else {
          int i = front;
          printf("\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 (i >= front) pos = i - front;
          else pos = MAX - (n - front);
    
    
          {
             printf("You are in the queue!\n\n");
             printf("Position in queue is %d \n\n", (rear + MAX - front) % MAX + 1);
             return;
          }
       }
    }
    
    
    /*REMOVE NTH FUNCTION (Removes nth element from queue)*/
    
    
    void remove_nth(int n) {
       int i;
       if ((front == -1) && (rear == -1)) {
          printf("Queue is empty! Nothing to remove.\n");
          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;
             printf("Succesfully deleted\n\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 %d\n", room_no[i]);
       printf("\n");
    }
    
    
    /*CANCEL FUNCTION (Allows patient to cancel appt. (similair to remove_nth))*/
    
    
    void cancel(int n) {
       int i;
       if ((front == -1) && (rear == -1)) {
          printf("Queue is empty! Nothing to cancel.\n");
          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;
             printf("Appointment successfully canceled\n\n");
             return;
          }
       }
       printf("\n%d not found in queue to delete\n\n", n);
    
    
    }
    
    
    void queue_display() {
       for (int i = 0; i < 6; i++)
          printf("%d\t", queue[i]);
          printf("\n");
          printf("front = %d\n", queue[front]);
          printf("rear = %d\n", queue[rear]);
          print("\n");
    }
    
    
    void loading() {
      int c, k;
       printf("Loading...\n\n");
       //For loop to add delay to give loading effect
       c = 1,  k = 1;
       for (c = 1; c <= 32767; c++)
          for (k = 1; k <= 32767; k++) {}
    }
    
    
    bool is_value_in(int val, int *arr, int size){
        int i;
        for (i=0; i < size; i++) {
            if (array[i] == val)
                return true;
        }
        return false;
    }


  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    just looking at the top of your code I can tell you this is redundnant.
    Code:
       /*Patient Commands*/
       char i; //Patient check in
       char p; //Patient queue position
       char q; //Patient quit
       char d; //Available doctors
       char h; //Help - display commands
       /*Doctors Commands*/
       char o; //Paitient check out
       char r; //Doctor ready
       char a; //Doctor away
       char w; //Waiting list
       char x; //Exit
       /*Other*/
       char choice;
    .....
       scanf(" %c", & choice);
    
    
        
      switch (choice) {
          case 'h':
             loading();
             system("clear");
             help();
             break;
    where all of your char 'letter to indicate what they are for' are just var that can have any value added to them making them obsolete to that in which you declared them for.

    your choice looks like you're using them little letters for the same thing, yes? so you can get rid of them maybe comment them letters and what they mean to keep notes on it in your code though. even though this had nothing to do with your one issue you're asking about.
    but it saves memory.

  3. #3
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    Thanks for the tip, userxbw

    I managed to get it written and working. For anybody who may be interested, or if anybody has any tips or advice I'm all ears. I'll post the code below. Also I realise the use of pointers and structs would have probably been put to good use in a program like this, but I simply wasn't allowed to use them for this particular assignment. Here's the code:


    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
    		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);
    			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;
                }
             }
          }
    }
    
    
    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;
       }
    }
    
    
    
    
    /*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]);
    	}
    }
    
    
    /*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]);
    	}
    }
    
    
    /*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;
    		}
    	}
    }
    
    
    /*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 %i, the Doctor will see you now. Please come to room #%i", queue[front], room_no);
             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++) {}
    }

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    gcc -Wall -lm -o "doctor_queue" "doctor_queue.c" (in directory: /home/userx/bin)
    doctor_queue.c: In function 'position':
    doctor_queue.c:384:21: warning: 'return' with no value, in function returning non-void
                         return;
                         ^
    doctor_queue.c: In function 'doctor_ready':
    doctor_queue.c:466:25: warning: format '%i' expects argument of type 'int', but argument 3 has type 'int (*)[2]' [-Wformat=]
                      printf("Patient number %i, the Doctor will see you now. Please come to room #%i", queue[front], room_no);
                             ^
    doctor_queue.c: In function 'check_if':
    doctor_queue.c:298:9: warning: control reaches end of non-void function [-Wreturn-type]
             }
             ^
    doctor_queue.c: In function 'first':
    doctor_queue.c:331:9: warning: control reaches end of non-void function [-Wreturn-type]
             }
             ^
    doctor_queue.c: In function 'last':
    doctor_queue.c:345:9: warning: control reaches end of non-void function [-Wreturn-type]
             }
             ^
    Compilation finished successfully.
    that is what I got out of it so far. did you're compiler not give you the same? it Compilation finished successfully., But personally I'd fix that for starters.

    this is declared to give a return value but is not, the term "default return value" used loosely because I just made up that term dynamically speaking.
    Code:
     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;
                    }
                }
            }
    this one needs a default return value
    Code:
      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;
               }
    // here it is looking for a return value of some sort
            }
    // same
           int first() {
                if (front == -1) {
                    puts("Error: cannot return front from empty queue");
                    return -1;
                }
                else {
                    printf("%d\n\n", queue[front]);
                }
    // no return value placed here 
            }
    //same
            /*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]);
                }
    // no return value placed here
            }
    this printf is in need of adjustment
    Code:
     printf("Patient number %i, the Doctor will see you now. Please come to room #%i", queue[front], room_no);
    //
    doctor_queue.c:466:25: warning: format '%i' expects argument of type 'int', but argument 3 has type 'int (*)[2]' [-Wformat=]
    In your entire switch you are "requesting" information but the user when looking at the screen how does he or she know that?
    Code:
         case 'p':
              loading();
              system("clear");
              scanf("%d", &patient_id); <--- here is your program is looking for input, but not telling the user that. 
              position(patient_id);      // so it just sits there with a blank screen, looking like it is hanging up somewhere. 
              break;                      // when in fact it is just waiting for input.  it is a simple fix .. think about it.
    when someone gets put on the waiting list does a doctor ever show up and call on that person? Because I am still waiting. and the only doctor is,
    Currently there is 1 Doctor in the Surgery. They are in room 0.
    in real life is there ever really a room 0 ? If the doctor is in Surgery then should he not be in the Surgery room, OR as it is called?
    Last edited by userxbw; 11-22-2017 at 11:01 AM.

  5. #5
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    I'm having to use an online compiler right now as the PC I'm on is a library one without a compiler. So thanks for that, I wasn't aware. I'll edit my OP to void the return of those functions, since they don't need to give back a value. Also I'm not sure what I need to do to that printf?

    Ah, it wont let me edit. But I've changed it anyhow. Thanks.

    Fixed that printf I think:

    Code:
    printf("Patient number %d, the Doctor will see you now. Please come to room #%d", queue[front], x + 1);
    or

    Code:
    printf("Patient number %d, the Doctor will see you now. Please come to room #%d", queue[front], room_id);
    would probably be better.
    Last edited by GeorgeIO; 11-22-2017 at 10:52 AM.

  6. #6
    Guest
    Guest
    @George, there are up-to-date online compilers that give you error messages. You could use something like Wandbox (click the compiler dropdown and select C from the languages, then a compiler from the left-menu). I'd also enable the -pedantic flag.

  7. #7
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    In answer to a user not knowing they need to input, doesn't this suffice?

    Code:
            puts("\nLeeds Student Medical Practice");        
            puts("-------------");
            puts("Enter a command");
            puts("hint - to view available commands enter 'h'");
            printf(">>>");
    Thanks for that Adrian. I'll give Wandbox a go from now on.

    This is what I'm getting now (using Wandbox with the pedantic flag):

    Code:
    Startprog.cc: In function 'int main()':
    prog.cc:125:32: error: too many arguments to function 'void cancel_appt()'
              cancel_appt(patient_id);
                                    ^
    prog.cc:33:6: note: declared here
     void cancel_appt();
          ^~~~~~~~~~~
    1Finish
    Why is it stating too many arguments?
    Last edited by GeorgeIO; 11-22-2017 at 11:05 AM.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by GeorgeIO View Post
    This is what I'm getting now (using Wandbox with the pedantic flag):

    Code:
    Startprog.cc: In function 'int main()':
    prog.cc:125:32: error: too many arguments to function 'void cancel_appt()'
              cancel_appt(patient_id);
                                    ^
    prog.cc:33:6: note: declared here
     void cancel_appt();
          ^~~~~~~~~~~
    1Finish
    Why is it stating too many arguments?
    Because you told the compiler that your function takes zero arguments and you gave it one. Note that C allows you to declare a function as taking any number of arguments like that (put a "void" inside the parentheses to mean "zero arguments"), but C++ doesn't. You compiled this program as C++ (you named the source file prog.cc instead of prog.c).

  9. #9
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by GeorgeIO View Post
    In answer to a user not knowing they need to input, doesn't this suffice?

    Code:
            puts("\nLeeds Student Medical Practice");        
            puts("-------------");
            puts("Enter a command");
            puts("hint - to view available commands enter 'h'");
            printf(">>>");
    Thanks for that Adrian. I'll give Wandbox a go from now on.

    This is what I'm getting now (using Wandbox with the pedantic flag):

    Code:
    Startprog.cc: In function 'int main()':
    prog.cc:125:32: error: too many arguments to function 'void cancel_appt()'
              cancel_appt(patient_id);
                                    ^
    prog.cc:33:6: note: declared here
     void cancel_appt();
          ^~~~~~~~~~~
    1Finish
    Why is it stating too many arguments?
    in your switch I used this
    Code:
     case 'i':
                        loading();
                        system("clear");
                        printf("enter patients ID\n"); <------ 
                        int patient_id; 
                        scanf(" %d", &patient_id);
                        enqueue(patient_id);
                        break;
    it prints out a message what it is looking for
    your function prototype does not have any data prams in the prams
    Code:
    prog.cc:125:32: error: too many arguments to function 'void cancel_appt()'
              cancel_appt(patient_id); <-- you're passing in a value when 
                                    ^
    prog.cc:33:6: note: declared here
     void cancel_appt(); <---- its declared void 
          ^~~~~~~~~~~
    should be something like
    Code:
    int cancel_appt(int ID)
    {
     do whatever
    
    return 0; <-- return zero to indicate no errors 
    }
    so that return value can be used to your advantage with something like this
    Code:
    if ( cancel_appt( id ) != 0 ) 
    {
       printf("your appointment cannot be canceled, too bad for you.\n"
               "charges will be ensued regardless if you show up or not.\n");
    }
    but you still can make it a void return function as well.
    Last edited by userxbw; 11-22-2017 at 11:18 AM.

  10. #10
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    My tutor wanted the input to be entered in one line. For example, a patient whose id number is 764 must type in this command on arrival:>>> i 764. Not sure if that's more prone to errors, but that's what he wanted. In the help menu I've put this, so it's clear to the user:

    Code:
    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");
    Last edited by GeorgeIO; 11-22-2017 at 11:43 AM.

  11. #11
    Guest
    Guest
    Quote Originally Posted by christop
    You compiled this program as C++ (you named the source file prog.cc instead of prog.c).
    @George: I don't know if that's the reason in this case, but be sure that the small green label in the top-left reads C, not C++. The menus below should list C11(GNU), not things like Boost etc.

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    1 , 2 ,3 where used to check in, 1 checked in, took 1 in put into room 1, selected o to check patient out, it deleted the person waiting number 2 , patient number one went where? theoretically he is still in the room waiting to be discharged.

    next run
    Code:
    enter patients ID
    1
    
    Welcome Patient No. 1
    All Doctors a busy right now.    
    Please relax in the waiting area.
    
    Welcome Patient No. 1
     Please come to room # 11.
    
    1 has been removed from the queue.
    
    Leeds Student Medical Practice
    logically, should I not have been told to just goto room 11 and wait and not greeted twice in the same "sentence" or instance? just a yes or no, you're the programmer of this and it is your call in how your logic is in that instance. he could have sat down then suddenly a doctor was available then gets called abruptly in a real word situation.

    sucks that you got a use an online compiler though. I'm still testing ...... pls wait in lobby

  13. #13
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    There's still a few errors I need to file out. E.g. case 'o' needs to be changed to:

    Code:
    case 'o':         loading();
             system("clear");
             scanf("%d", & patient_id);
             patient_discharge(patient_id);
             break;
    You get the general idea though

    The function itself probably needs to be changed to this too, just for clarity:

    Code:
    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;
          }
       }
    }
    Also, you're right about the above point. I'll get to work on changing that.
    Last edited by GeorgeIO; 11-22-2017 at 11:53 AM.

  14. #14
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by GeorgeIO View Post
    My tutor wanted the input to be entered in one line. For example, a patient whose id number is 764 must type in this command on arrival:>>> i 764. Not sure if that's more prone to errors, but that's what he wanted. In the help menu I've put this, so it's clear to the user:

    Code:
    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");
    that not a problem then it should in my option then when ever you need input to cause your program to indicate that so the user knows.

    Then whenever input is needed, to print what it is looking for and in what manner to input it. I think that would satisfy your instructors specifications. You got functions down. maybe write a message function that you can use to be called instead of writing it every time in your program.

    let me think on this and see if I can come up with something that might help you.
    as it stands
    Code:
    $ ./doctor_queue
    Hello and welcome to Leeds Student Medical Practice
    Press RETURN to continue...
    
    
    
    
    
    
    
    
    
    Loading...
    
    
    Leeds Student Medical Practice
    -------------
    Enter a command
    hint - to view available commands enter 'h'
    >>>
    is what I am seeing and your print out might be an update?

    I added that looks sweet, but what about a doctor now needing to use it. My idea not needed to be used is add an option to press that will just show doctors commands without having to use h for help to see what they are. but it could be argued why does a patient need to see or have access to the doctors codes for? that is a breach of security. therefore bad programming. in a real world situation that is.

    you could add a pass code number that needs inputted before a doctor can get in and give that professor something to think about .
    Last edited by userxbw; 11-22-2017 at 12:03 PM.

  15. #15
    Registered User GeorgeIO's Avatar
    Join Date
    Nov 2017
    Posts
    33
    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

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