Queue Number System for Clinic registration counter.

closed account (Shf8M4Gy)
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
Last edited on
Hello arisjay7900,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

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 found the second link to be the most help.



Andy
 
queue <Patient> data


Does this mean you can use std::queue?
Hello arisjay7900,


You have a basic layout of your program.

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.

Andy

Edit: line 7 of code.
Last edited on
@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.

https://www.cplusplus.com/reference/queue/queue/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <string>
#include <iostream>
#include <queue>

using namespace 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 */ }
        else if (choice == 2) {
            /*invoke function inqueue() by sending necessary parameter to receive 

     and display total no of patients in queue */
        }
        else if (choice == 3) {
            /*invoke function display() by sending necessary parameter */
        }
        else if (choice == 4) {
            /*invoke function serve() by sending necessary parameter */
        }
    } while (choice != 5);
    
    return 0;
}
Last edited on
Using std::deque, this is fairly simple. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <deque>
#include <string>
#include <iostream>
#include <limits>
using namespace 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 */

	static int 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 (const auto& [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()) {
		const auto [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);
}

Might be a good idea to get approval from teacher if deque is OK - might even get extra points but at the moment queue specified.
The 'problem' with std::queue is that's no good way to iterate it to obtain the elements (is there?).
There's no begin, end, ++ etc.
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.

1
2
3
4
5
6
7
8
    size_t count{0};
    while ( count < Q.size() )
    {
        std::cout << Q.front() << '\n';
        Q.emplace(Q.front());
        Q.pop();
        count++;
    }
closed account (Shf8M4Gy)
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.

Aris
@aris,
Thanks, there are plenty of people here who can help especially if you give it a good try. :)
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <queue>
#include <string>
#include <iostream>
#include <limits>
using namespace std;

//data structure declaration
struct Patient {
	int q_no;
	string name;

	Patient(int q, const string& n) : q_no(q), name(n) {}
};

using Data = queue<Patient>;

ostream& operator<<(ostream& os, const Patient& pat)
{
	return os << pat.q_no << "  " << pat.name;
}

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 */

	static int num = 1000;
	string name;

	cout << "Enter patient name: ";
	getline(cin, name);

	data.emplace(++num, name);
}

int inqueue(const Data& data)
{
	/*to return total no of patient in queue*/

	return data.size();
}

void display(Data& data)
{
	/*to display list of patient in queue (name and queue no) */

	if (!data.empty())
		for (size_t count {0}; count < data.size(); ++count) {
			cout << data.front() << '\n';
			data.emplace(data.front());
			data.pop();
		}
	else
		cout << "No patients waiting\n";
}

void serve(Data& data)
{
	/*to serve patient in queue, only if user choose to*/

	if (!data.empty()) {
		cout << "Patient " << data.front() << " 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();
			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);
}

closed account (Shf8M4Gy)
@againtry

Yeah that's right.
closed account (Shf8M4Gy)
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.
Please don't remove your original post. It's not good forum etiquette.
Topic archived. No new replies allowed.