Hi, I'm hoping someone can help me to fix a problem I'm having with part of a C++ assignment.
Basically I need an array of patients names, addresses, ID, amount owing, and number of treatments, so I created a struct called patient with a typedef P.
I create a new array with just one value initially, after which I want to increase the array size by 1 each time I want to add in another patient.
I the code below, I have a function to display my menu, and another to enter a new patient.
Finally when I choose Q to quit, the program prints out the list entered. (although it's not working yet!).
When I run the code, it is fine for 1 entry, however when I try to enter in any extra patient it appears to work but when I quit, it generates an unhandled expression and crashes out.
Code:
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
|
#include <iostream>
#include <iomanip>
using namespace std;
struct Patient{
int pID;
string pName;
string pAddress;
float amountOwing;
int noTreatments;
};
typedef struct Patient P;
void enterPatients(P *parray, int &pCount);
char getOption();
void main()
{
int pCount=1; // allow for 1 patient to start with
P * parray = new P[pCount];
char option;
do
{
option = getOption();
switch(option)
{
case 'P': enterPatients(parray, pCount);
pCount++;
cout << " in swich statement " << pCount << endl;
break;
// more cases to come!
}
}while(option!='Q');
//output the patients to check if stored correctly:
for(int i=0;i<pCount;i++)
{
cout << parray[i].pName << " " << parray[i].pID << " " << parray[i].pAddress << " " << parray[i].amountOwing << endl;
}
}
void enterPatients(P *parray, int &pCount)
{
if(pCount==1) // enter just the first patient details
{
cin.get();
cout << "Please enter patient details:\n";
parray[pCount-1].pID = pCount-1;
cout << "Enter patient name " << pCount-1 << endl;
getline(cin, parray[pCount-1].pName);
cout << "Enter patient address \n";
getline(cin, parray[pCount-1].pAddress);
parray[pCount-1].amountOwing = 0.00; // initially patient owes nothing
parray[pCount-1].noTreatments = 0; // initially patient has no treatments
cout << pCount << " in pcount ==1 looop "<< endl;
return;
}
// for entering 2nd and subsequent patients
cout << pCount << " in loop for pCount > 1 "<< endl;
P * temparray = new P[pCount];
// new temparray to transfer original details into bigger array
for(int i=0;i<pCount-1; i++)
{
temparray[i].pID = parray[i].pID;
temparray[i].pName = parray[i].pName;
temparray[i].pAddress = parray[i].pAddress;
temparray[i].amountOwing = parray[i].amountOwing;
temparray[i].noTreatments = parray[i].noTreatments;
}
delete [] parray; // is this correct to remove the original array of structs?
parray = temparray; // is this correct to point at the copied array?
// should I delete the temparray now?
cin.get();
cout << "Please enter patient details:\n";
parray[pCount-1].pID = pCount-1;
cout << "Enter patient name " << pCount-1 << endl;
getline(cin, parray[pCount-1].pName);
cout << "Enter patient address \n";
getline(cin, parray[pCount-1].pAddress);
parray[pCount-1].amountOwing = 0.00; // initially patient owes nothing
parray[pCount-1].noTreatments = 0; // initially patient has no treatments
}
char getOption()
{
//system("CLS");
char option;
cout << "What would you like to do next?\n\n" ;
cout << setw(10) << "Press P to enter a new patient\n";
cout << setw(10) << "Press Q to quit\n";
cin >> option;
//cin.get();
option = toupper(option);
return option;
}
|