You are to write a Queue Number System for Clinic registration counter. General requirements are as follows:
1. When a patient comes in, the operator will generate queue number and give it to the patient (assume printing is available). Program will record the queue number and the patient name generated (i.e. in a suitable data structure).
2. Patients have to wait until their numbers are called. When a patient is served, the patient queue number will be removed from the queue.
3. Using the program, staff can:
• Get the total number of waiting patients in queue
• Display all patients in queue
• Serve a patient – this will show the queue number to be served and update the queue list.
• Exit – program will prompt user if the queue is not empty
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I would start by coding the menu and getting it to display correctly first.
You are missing some of your code and I had to add what I think you left out. There are still some errors that need fixed in order for the program to compile completely.
In "main" you have: queue <Patient> data; int choice, no = 1000;. It is usually better to put different types on different lines, so that it is less confusing and easier to find:
1 2 3 4 5 6 7
Patient p;
std::queue<Patient> data;
int choice{}, no = 1000;
std::cout <<
"WELCOME TO QUEUE NUMBER SERVICE SYSTEM\n""~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
In line 3 it is best to initialize the variable choice because for now it causes an error since it contains a garbage value.
The "cout" statement is just an example of a different way of writing this. I find it resembles more of what the actual output will look like.
For line 7 you could use std::string(39, '~') and it will construct an object of a string filled with 39 characters of "~". Just an option.
@OP - you could have done this easily yourself but as a start the following code compiles with a few very small changes and some dummy return values.
Go through each part one at a time. Some of it, like the menu display, is just a cut and paste from your post. You've got 5 menu options - there's your plan - just 1 at a time ...
You probably need to get up to speed on how STL queues work by the look of it. The rest is pretty straightforward once you understand their funcionality.
#include <string>
#include <iostream>
#include <queue>
usingnamespace std;
/* data structure declaration */
struct Patient {
int q_no;
string name;
};
int menu() {
/* to display and return user’s selection*/
int a;
return a;
}
void append(/*input arguments*/) {
/*to get input name from user and add patient record (name and queue no)
to the queue */
}
int inqueue(/*input argument*/) {
/*to return total no of patient in queue*/
int a;
return a;
}
void display(/*input argument*/) {
/*to display list of patient in queue (name and queue no) */
}
void serve(/*input argument*/) {
/*to serve patient in queue, only if user choose to*/
}
int main() {
Patient p;
queue<Patient> data;
int choice, no = 1000;
cout << "WELCOME TO QUEUE NUMBER SERVICE SYSTEM\n";
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
do {
//invoke function menu() to receive user’s selection.
if (choice == 1) { no++;
/*invoke function append() by sending necessary parameters and queue
no */ }
elseif (choice == 2) {
/*invoke function inqueue() by sending necessary parameter to receive
and display total no of patients in queue */
}
elseif (choice == 3) {
/*invoke function display() by sending necessary parameter */
}
elseif (choice == 4) {
/*invoke function serve() by sending necessary parameter */
}
} while (choice != 5);
return 0;
}
#include <deque>
#include <string>
#include <iostream>
#include <limits>
usingnamespace std;
//data structure declaration
struct Patient {
int q_no;
string name;
Patient(int q, const string& n) : q_no(q), name(n) {}
};
using Data = deque<Patient>;
int menu()
{
/* to display and return user’s selection*/
int choice {};
cout << "\n1) Generate queue\n";
cout << "2) Get total waiting patients\n";
cout << "3) Display all waiting patients\n";
cout << "4) Serve patient\n";
cout << "5) Exit\n";
cout << "\nOption (1 - 5) :";
while (!(cin >> choice) || choice < 1 || choice > 5) {
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Invalid choice. Enter option (1 - 5): ";
}
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return choice;
}
void append(Data& data)
{
/*to get input name from user and add patient record (name and queue no) to the queue */
staticint num = 1000;
string name;
cout << "Enter patient name: ";
getline(cin, name);
data.emplace_back(++num, name);
}
int inqueue(const Data& data)
{
/*to return total no of patient in queue*/
return data.size();
}
void display(const Data& data)
{
/*to display list of patient in queue (name and queue no) */
if (!data.empty())
for (constauto& [qno, name] : data) // Needs C++17
cout << qno << " " << name << '\n';
else
cout << "No patients waiting\n";
}
void serve(Data& data)
{
/*to serve patient in queue, only if user choose to*/
if (!data.empty()) {
constauto [qno, name] = data.front(); // Needs C++17
cout << "Patient " << qno << " " << name << " is served [Y/N]: ";
char ans;
while (!(cin >> ans) || (ans = tolower(ans)) != 'y' && ans != 'n') {
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Invalid reply. Please answer y or n: ";
}
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (ans == 'Y' or ans == 'y') {
data.pop_front();
cout << "Queue updated\n";
}
} else
cout << "There are no patients waiting\n";
}
int main()
{
Data data;
int choice {};
cout << "WELCOME TO QUEUE NUMBER SERVICE SYSTEM\n";
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
do {
//invoke function menu() to receive user’s selection.
switch (choice = menu()) {
case 1:
append(data);
/*invoke function append() by sending necessary parameters and queue no */
break;
case 2:
cout << "There are " << inqueue(data) << " patients in the queue\n";
/*invoke function inqueue() by sending necessary parameter to receive and display total no of patients in queue */
break;
case 3:
display(data);
/*invoke function display() by sending necessary parameter */
break;
case 4:
serve(data);
/*invoke function serve() by sending necessary parameter */
break;
}
} while (choice != 5);
}
True, there's no off the shelf iterator method but the following does the job. My only point was what the teacher expects having specified the basic type and we may as well make it clear to @OP.
Wow! Thank you so much againtry! anyway, I just couldn't understand with Queue in Data Structure and its kinda complicated for me with online course. you already help me to understand it and it crazy how you can just explain it to me thru this platform. I think you are much better than my lecturer to be honest.
That code to iterate a queue is horrible! - but I know of no better. You can't use it on a const queue (as the queue is altered) which you should be able to for iteration. I remember now why I don't like queue!
Updated version which uses queue rather than deque:
My teacher is happy with the code! and sometimes the code stop all the sudden because there is bugs. And I think he did mention can cause from using pointer. But Im happy with your work @seeplus and he happy as well.