Adding a new patient into an array?

I'm not sure if there's a way to do this in C++, but I'd like to be able to add multiple patients into my program using an array. How can I best go about this?

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

using namespace std;

class patientType {
public:
	string firstName;
	string lastName;
	int patientID;
};

const int maxPatients = 10;

void printMenu();
void printAll();
void add();
void sortLast();
void sortID();
void searchLast();
void searchID();
int quit();

int main() {
	printMenu();





	system("pause");
	return 0;
}

void printMenu() {
	int input;
	cout << "Enter the corresponding number for your choice: " << endl;
	cout << "(1): Print all patients." << endl;
	cout << "(2): Add a patient." << endl;
	cout << "(3): Sort by last name." << endl;
	cout << "(4): Sort by ID." << endl;
	cout << "(5): Search by last name." << endl;
	cout << "(6): Search by ID." << endl;
	cout << "(7): Exit the program." << endl;
	cin >> input;

	switch (input)
	{
	case 1:
		printAll();
		printMenu();
		break;
	case 2:
		add();
		printMenu();
		break;
	case 3:
		sortLast();
		printMenu();
		break;
	case 4:
		sortID();
		printMenu();
		break;
	case 5:
		searchLast();
		printMenu();
		break;
	case 6:
		searchID();
		printMenu();
		break;
	case 7:
		quit();
	default:
		system("pause");
	}
}

void printAll() {
	patientType patients[maxPatients];


	for (int i = 0; i < maxPatients; i++)
	{
		cout << "First Name            Last Name         ID" << endl;
		cout << "------------------------------------------" << endl;
		cout << patients[i].firstName << "        " << patients[i].lastName << "         " << patients[i].patientID << endl;
	}

}

void add() {
	patientType patients[maxPatients];
//The function I'd like help with.

}
Add the patient object to an array as you'll do with any other primitive type.
Also, you would not want to declare the patients array out of main because of the automatic storage.
You also need to keep track of the number of patients in the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    int nPatients = 0;  //number of patients
    
    patientType patients[maxPatients];
    
    printMenu();
    ...
}
void add(int &nPatients)  //note pass by reference
{
      /*
      check if array is full. i.e whether nPatients equals maxPatients
      if not full
          -instantiate object of patientType
          -insert object at index "nPatients" in array
          -increment "nPatients"
      else
           -report error
           -return error code
} */
I recommend you use a vector, which is a type of dynamic container- it's essentially an array that can grow and shrink.

http://www.cplusplus.com/reference/vector/vector/

You access things in the exact same way, here's a quick snippet


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
#include <vector>
#include <iostream>

using std::vector;
using std::cout;

int main(){
	//Make a new vector that stores integers
	vector<int> container;

	//append the container with a 5, the container now has a size of 1
	container.push_back(5);

	//append the container with 0-14
	for(int a = 0; a < 15; a++){
		container.push_back(a);
	}

	//set the container's first object to 4
	container[0] = 4;

	container[1] = container[0] + container[3];

	//set the container's last object to 555
	container[container.size()-1] = 555;

	//print the contents of the container
	for(int a = 0; a < container.size(); a++){
		cout << "container[" << a << "] = " << container[a] << "\n";
	}
}

Last edited on
Is this ok?

1
2
3
4
5
6
7
8
9
10
void add(int &numPatients) {
	patientType patients[maxPatients];
	if (numPatients != maxPatients) {
		for (int i = 0; i < maxPatients; i++){
			cout << "Enter the first name, last name, and ID of the new patient: " << endl;
			cin >> patients[i].firstName >> "        " >> patients[i].lastName >> "         " >> patients[i].patientID;
		}
		numPatients++;
	}
}
test program your self and see it is ok!
As for the sorting, which type do guys recommend for this problem? Bubble, Insertion, or Selection?
I have two errors with my printAll function. Here's the function:

1
2
3
4
5
6
7
8
9
10
11
12
void printAll() {
	patientType patients[maxPatients];
	int numPatients;
	add(patients, numPatients);
	for (int i = 0; i < maxPatients; i++)
	{
		cout << "First Name            Last Name         ID" << endl;
		cout << "------------------------------------------" << endl;
		cout << patients[i].firstName << patients[i].lastName << patients[i].patientID << endl;
	}

}


The first error is

Error 1 error LNK2019: unresolved external symbol "void __cdecl add(class patientType * const,int &)" (?add@@YAXQAVpatientType@@AAH@Z) referenced in function "void __cdecl printAll(void)" (?printAll@@YAXXZ)

The other is "1 unresolved externals" but that goes with error LNK2019, right?
Topic archived. No new replies allowed.