sorting and display char array

I had compile my code, it shows no error, but I cant see the correct output. I think there is something wrong with pArray and its function.

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//sort patient
//search doctor

#include <iostream>
#include <fstream>
#include <string>
#define MAX 100
using namespace std;

class Patient
{
	protected:
		char name[], ic[], contact[];
		string dateAppoint;
		int age;
		bool gender;	//0=female, 1=male
		
	public:
		void setPatient(const char* name, const char* ic, const char* contact, string dateAppoint, int age, bool gender)
		{
			name = "";
			ic = "";
			contact = "";
			this->dateAppoint=dateAppoint;
			this->age=age;
			this->gender=gender;
		}
		
		void displayPatient()
		{
			int num = 0; 
			
			cout << "====================    LIST OF PATIENTS    ====================\n"
				 << "-----------------------------------------------------------------\n"
				 << "NAME\tIC NO.\tCONTACT\tDATE APPOINTMENT\tAGE\t\nGENDER";
			
			
			cout << name << "\t"
				 << ic << "\t\t"
				 << contact << "\t\t"
				 << dateAppoint << "\t"
				 << age <<"\t";
				 
			if(gender==true)
			{
				cout << "Female\t\n";
			}	
			else
				cout << "Male\t\n";
				
			num++; 
	
			cout << "\nNUMBER OF PATIENTS: " << num << "\n\n\n";
		}
		
		void sortString(char data[MAX], int n)
		{
			char item;
			int pass, insertIndex;
			for (pass=1; pass<n; pass++)
			{
				item = data[pass];
				insertIndex = pass;
				while((insertIndex > 0) && (data[insertIndex -1] > item))
				{
					// insert the right item
					data[insertIndex] = data[insertIndex -1];
					insertIndex --;
				}
				data[insertIndex] = item;
				// insert item at the right place
			} // end for
		}
		
		char* getName()
		{
			return name;
		}
		
		char* getIC()
		{
			return ic;
		}
		
		char* getContact()
		{
			return contact;
		}
};

int menu()
{
	int choice;
	cout << "=============		CLINIC APPOINTMENT SYSTEM		=============\n"
		 << "\t\t\tLOGIN AS\n"
		 << "\t\t\t1. ADMIN\n\t\t\t2. PATIENT \n\t\t\t3. EXIT\n\n"
		 << "CHOICE : ";
	cin >> choice;
	
	return choice;
}

int sortMenu()
{
	int choice;
	cout << "<<< Sorting Process >>>\n[1] By name\n[2] By IC number\n[3] By contact number\n";
	cout << "\nOption: ";
	cin >> choice;
	
	return choice;
}

int main()
{
//	Patient pArray[100]; //max 100 patient
	Doctor dArray[10];
	
	int choice, choice2, choice3;
	choice = menu();
	
	Patient pArray[50];
	
	//set two existing patient
	pArray[0].setPatient("Joyce", "021203-01-1888", "017-5546992", "25-01-2022", 19, 0);
	pArray[1].setPatient("Ahmad", "700222-01-2523", "012-3456789", "07-01-2022", 51, 1);
	
	
	switch(choice)
	{
		case 1:
		{
		 	cout << "=============	ADMIN MENU	=============\n"
				 <<"\t1. DISPLAY PATIENTS' LIST \n\t2. SORT PATIENTS\n\t3. BACK TO MAIN MENU\n"
				 <<"CHOICE: ";
			cin >> choice2;
			
			switch(choice2)
			{
				case 1:		//display patient list
				{
					for(int i=0; i++; i<50)
					{
						pArray[i].displayPatient();
					}
					break;
				}
				
				case 2:		//sort patient
				{
					int sortChoice;
					
					sortChoice=sortMenu();
					
					switch(sortChoice)
					{
						case 1:
						{
							cout << "\nSort patient list by name:\n";
							
							for(int i=0; i<50; i++)
							{
								pArray[i].sortString(pArray[i].getName(), MAX);
								
								//display sorted patient
								cout << pArray[i].getName();
							}
							
							break;
						}
						
						case 2:
						{
							cout << "\nSort patient list by IC number:\n";
							
							for(int i=0; i<50; i++)
							{
								pArray[i].sortString(pArray[i].getIC(), MAX);
							}
					
							break;
						}
						
						case 3:
						{
							cout << "\nSort patient list by contact number:\n";
							
							for(int i=0; i<50; i++)
							{
								pArray[i].sortString(pArray[i].getContact(), MAX);
							}
							
							break;
						}
						
						default:
							cout << "Invalid choice!\n";
							break;
					}
					
					break;
				}
				
				case 3:		//exit system
				{
					break;
				}
					
				default:
					cout<<"Invalid choice!"<<endl;
					break;
			}
			break;
		}
		
	}
	
	return 0;
}
Last edited on
> char name[], ic[], contact[];
Replace all these with std::string
You don't have any allocated memory, and you're trashing everything as soon as you read a line.

> char* getName
And all these with a reference to a std::string.

You should have proper set/get methods.
Just returning direct references (or pointers) to the innards of your class is a bad idea.
Last edited on
thank you!
but I get another error when I switch it all to string

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//patient

#include <iostream>
#include <iomanip>
#include <string>
#define MAX 100
using namespace std;

class Patient
{
	private:
		string name, ic, contact, dateAppoint;
		int age;
		bool gender;	//0=female, 1=male
		
	public:
		void setPatient(string name, string ic, string contact, string dateAppoint, int age, bool gender)
		{
			this->name=name;
			this->ic=ic;
			this->contact=contact;
			this->dateAppoint=dateAppoint;
			this->age=age;
			this->gender=gender;
		}
		
		void display()
		{
			cout << name << "\t"
				 << ic << "\t\t"
				 << contact << "\t\t"
				 << dateAppoint << "\t"
				 << age <<"\t";
				 
			if(gender==false)
			{
				cout << "Female\t\n";
			}	
			else
				cout << "Male\t\n";
		}
		
		string getName()
		{
			return name;
		}
};

void insertionSort(string data[], int n)	//n is number of string to compare
{
	string item;
	int pass, insertIndex;
	
	for (pass=1; pass<n; pass++)
	{
		item = data[pass];
		insertIndex = pass;
		while((insertIndex > 0) && (data[insertIndex -1] > item))
		{
			// insert the right item
			data[insertIndex] = data[insertIndex -1];
			insertIndex --;
			
			for(int i=0; i<n; i++)
				cout << data[i] << " ";
			cout << endl;
		}
		
		data[insertIndex] = item;
		
		for(int i=0; i<n; i++)
			cout << data[i] << " ";
		
		cout << endl;
			
		cout << endl;
		// insert item at the right place
	} // end for
}

int main()	
{
	int choice2, pnum=2;
	Patient pArray[50];
	
	//set two existing patient
	pArray[0].setPatient("Joyce", "021203-01-1888", "017-5546992", "25-01-2022", 19, 0);
	pArray[1].setPatient("Ahmad", "700222-01-2523", "012-3456789", "07-01-2022", 51, 1);
		 	
	cout << "=============	ADMIN MENU	=============\n"
		 <<"\t1. DISPLAY PATIENTS' LIST \n\t2. SORT PATIENTS\n\t3. BACK TO MAIN MENU\n"
		 <<"CHOICE: ";
	cin >> choice2;
			
	switch(choice2)
	{
		case 1:		//display patient list
		{
			cout << "\n=============	PATIENT LIST	=============\n"
				 << "NAME" << setw(14) << "IC NO." << setw(23) << "CONTACT" << setw(26) << "DATE BOOKED"
				 << setw(8) <<"AGE" << setw(12) << "GENDER\n";
				 
			for(int i=0; i<pnum; ++i)
			{
				pArray[i].display();
			}
			
			cout << "\nNUMBER OF PATIENTS: " << pnum << "\n\n\n";
			
			break;
		}
				
		case 2:		//sort patient
		{
			int sortChoice;
					
			switch(sortChoice)
			{
				case 1:
				{
					cout << "\nSort patient list by name:\n";
					for(int i=0; i<pnum; i++)
					{
						insertionSort(pArray[i].getName, 2);
					}
					break;
				}
						
				case 2:
				{
					cout << "\nSort patient list by IC number:\n";
					break;
				}
						
				case 3:
				{
					cout << "\nSort patient list by contact number:\n";
					break;
				}
				
				default:
					cout << "Invalid choice!\n";
					break;
			}
					
			break;
		}
		
		case 3:		//exit system
		{
			break;
		}
				
		default:
			cout<<"Invalid choice!"<<endl;
			break;
	}
		
	return 0;
}
Last edited on
124:41: error: invalid use of non-static member function


insertionSort is a member function of the Patient class, but you call it like a free function.
It would be better if you make it a free function and pass in an array of Patients.
hi, what is free function? is it standalone?
Yes, free or standalone are just different terms.
I had tried to pass patient array into the sort function, but I think there is still some error need to fix.
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//patient

#include <iostream>
#include <iomanip>
#include <string>
#define MAX 100
using namespace std;

class Patient
{
	private:
		string name, ic, contact, dateAppoint;
		int age;
		bool gender;	//0=female, 1=male
		
	public:
		void setPatient(string name, string ic, string contact, string dateAppoint, int age, bool gender)
		{
			this->name=name;
			this->ic=ic;
			this->contact=contact;
			this->dateAppoint=dateAppoint;
			this->age=age;
			this->gender=gender;
		}
		
		void display()
		{
			cout << name << "\t"
				 << ic << "\t\t"
				 << contact << "\t\t"
				 << dateAppoint << "\t"
				 << age <<"\t";
				 
			if(gender==false)
			{
				cout << "Female\t\n";
			}	
			else
				cout << "Male\t\n";
		}
		
		string getName()
		{
			return name;
		}
};

int sortMenu()
{
	int choice;
	cout << "<<< Sorting Process >>>\n[1] By name\n[2] By IC number\n[3] By contact number\n";
	cout << "\nOption: ";
	cin >> choice;
	
	return choice;
}

//pass patient datatype
void insertionSort(Patient data[], int n)	//n is number of string to compare
{
	Patient item;
	int pass, insertIndex;
	
	for (pass=1; pass<n; pass++)
	{
		item = data[pass];
		insertIndex = pass;
		while((insertIndex > 0) && (data[insertIndex-1].getName() > item.getName()))
		{
			// insert the right item
			data[insertIndex].getName() = data[insertIndex-1].getName();
			insertIndex --;
			
			for(int i=0; i<n; i++)
				cout << data[i].getName() << " ";
			cout << endl;
		}
			
		cout << endl;
		// insert item at the right place
	} // end for
}

int main()	
{
	int choice2, pnum=2;
	Patient pArray[50];
	
	//set two existing patient
	pArray[0].setPatient("Joyce", "021203-01-1888", "017-5546992", "25-01-2022", 19, 0);
	pArray[1].setPatient("Ahmad", "700222-01-2523", "012-3456789", "07-01-2022", 51, 1);
		 	
	cout << "=============	ADMIN MENU	=============\n"
		 <<"\t1. DISPLAY PATIENTS' LIST \n\t2. SORT PATIENTS\n\t3. BACK TO MAIN MENU\n"
		 <<"CHOICE: ";
	cin >> choice2;
			
	switch(choice2)
	{
		case 1:		//display patient list
		{
			cout << "\n=============	PATIENT LIST	=============\n"
				 << "NAME" << setw(14) << "IC NO." << setw(23) << "CONTACT" << setw(26) << "DATE BOOKED"
				 << setw(8) <<"AGE" << setw(12) << "GENDER\n";
				 
			for(int i=0; i<pnum; ++i)
			{
				pArray[i].display();
			}
			
			cout << "\nNUMBER OF PATIENTS: " << pnum << "\n\n\n";
			
			break;
		}
				
		case 2:		//sort patient
		{
			int sortChoice;
			sortChoice=sortMenu();
					
			switch(sortChoice)
			{
				case 1:
				{
					cout << "\nSort patient list by name:\n";
					for(int i=0; i<pnum; i++)
					{
						insertionSort(pArray, 2);	//what if pass patient type to insertionsort?
					// 	how to compare only the name of the patient? how to pass
					}
					break;
				}
						
				case 2:
				{
					cout << "\nSort patient list by IC number:\n";
					break;
				}
						
				case 3:
				{
					cout << "\nSort patient list by contact number:\n";
					break;
				}
				
				default:
					cout << "Invalid choice!\n";
					break;
			}
					
			break;
		}
		
		case 3:		//exit system
		{
			break;
		}
				
		default:
			cout<<"Invalid choice!"<<endl;
			break;
	}
		
	return 0;
}
Last edited on
C++ has it's sort function - sort. Using this then perhaps something like:

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
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>

using namespace std;

class Patient {
private:
	string name, ic, contact, dateAppoint;
	unsigned age{};
	bool gender{};	//false = female, true = male

public:
	void setPatient(const string& name_, const string& ic_, const string& contact_, const string& dateAppoint_, unsigned age_, bool gender_) {
		name = name_;
		ic = ic_;
		contact = contact_;
		dateAppoint = dateAppoint_;
		age = age_;
		gender = gender_;
	}

	void display() const {
		cout << name << "\t"
			<< ic << "\t\t"
			<< contact << "\t\t"
			<< dateAppoint << "\t"
			<< age << "\t";

		cout << (gender ? "Male" : "Female") << "\n";
	}

	string getName() const { return name; }
	string getIc() const { return ic; }
	string getContact() const { return contact; }
};

unsigned sortMenu() {
	unsigned choice{};

	cout << "<<< Sorting Process >>>\n[1] By name\n[2] By IC number\n[3] By contact number\n";
	cout << "\nOption: ";
	cin >> choice;

	return choice;
}

int main() {
	const unsigned pnum{ 2 };
	Patient pArray[50]{};

	//set two existing patient
	pArray[0].setPatient("Joyce", "021203-01-1888", "017-5546992", "25-01-2022", 19, 0);
	pArray[1].setPatient("Ahmad", "700222-01-2523", "012-3456789", "07-01-2022", 51, 1);

	for (unsigned choice2{}; choice2 != 3; ) {
		cout << "=============	ADMIN MENU	=============\n"
			<< "\t1. DISPLAY PATIENTS' LIST \n\t2. SORT PATIENTS\n\t3. EXIT\n"
			<< "CHOICE: ";
		cin >> choice2;

		switch (choice2) {
		case 1:		//display patient list
		{
			cout << "\n=============	PATIENT LIST	=============\n"
				<< "NAME" << setw(14) << "IC NO." << setw(23) << "CONTACT" << setw(26) << "DATE BOOKED"
				<< setw(8) << "AGE" << setw(12) << "GENDER\n";

			for (unsigned i = 0; i < pnum; ++i)
				pArray[i].display();

			cout << "\nNUMBER OF PATIENTS: " << pnum << "\n\n";
		}
		break;

		case 2:		//sort patient
		{
			const auto sortChoice{ sortMenu() };

			switch (sortChoice) {
			case 1:
				cout << "\nSort patient list by name:\n";
				sort(pArray, pArray + pnum, [](const auto& p1, const auto& p2) {return p1.getName() < p2.getName(); });
				break;

			case 2:
				cout << "\nSort patient list by IC number:\n";
				sort(pArray, pArray + pnum, [](const auto& p1, const auto& p2) {return p1.getIc() < p2.getIc(); });
				break;

			case 3:
				cout << "\nSort patient list by contact number:\n";
				sort(pArray, pArray + pnum, [](const auto& p1, const auto& p2) {return p1.getContact() < p2.getContact(); });
				break;

			default:
				cout << "Invalid choice!\n";
				break;
			}
		}
		break;

		case 3:		//exit system
			break;

		default:
			cout << "Invalid choice!\n";
			break;
		}
	}
}

Thank you, everyone! I had solved it!
Topic archived. No new replies allowed.