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?
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.
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
} */
#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";
}
}
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++;
}
}