M
mchoya
I'm so frustrated. I'm beginning school next week and I have been working
on a simple program for several days now without being able to complete it.
(Though I have brushed up on a lot of C++ while trying.)
I realize this code is long but, I'm not sure of any other way to display
the issue.
The header file and ?implementation file work. (This assignment was
building on another in which I created the header and implementation with a
simple executable, i.e. display the read data.)
This executable is supposed to 1. create an array of Patient objects, then
2. present a user menu to a. read Patient data from a user specified file,
b. display data. c. quit program.
Facts: max patients = 1000.
Requirements: cannot change header or implement file; use an array; handle
user errors
So here it is, any assistance would be extremely appreciated.
HEADER
#ifndef PATIENT_H
#define PATIENT_H
#include <iostream>
#include <string>
using namespace std;
class Patient
{
public:
Patient();
Patient(string fName, string lName, string idString, int bYear);
string getName() const;
int getAge() const;
string getID() const;
void setFirstName (string fname);
void setLastName (string lname);
void setID(string idString);
void setBirthYear(int bYear);
private:
string firstName;
string lastName;
string ID;
int birthYear;
};
#endif
HEADER DEFINITIONS
#include <iostream>
#include <string>
#include "patient.h"
using namespace std;
Patient:atient() {
setFirstName("First");
setLastName("Last");
setID("Z9999");
setBirthYear(9999);
}
Patient:atient(string fName, string lName, string idString, int bYear) {
setFirstName(fName);
setLastName(lName);
setID(idString);
setBirthYear(bYear);
}
string Patient::getName() const {
return lastName + ", " + firstName;
}
int Patient::getAge() const {
return birthYear;
}
string Patient::getID() const {
return ID;
}
void Patient::setFirstName(string fName) {
firstName = fName;
}
void Patient::setLastName(string lName) {
lastName = lName;
}
void Patient::setID(string idString) {
int i = idString.size();
if (i == 5) {
ID = idString;
}
else {
cerr <<"\nIllegal Patient ID: " <<idString<<" using Z9999"<<endl;
ID = "Z9999";
}
}
void Patient::setBirthYear(int Year) {
if ((Year >1873) && (Year < 2003)) {
birthYear = 2003 - Year;
}
else {
cerr<<"\nIllegal Year: "<<Year<<" using 9999"<<endl;
birthYear = 9999;
}
}
**** Problem Driver ***********
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "patient.h"
using namespace std;
char resp;
void GetList();
bool getPatient(ifstream &fin, Patient &P);
const int MaxPatientSize = 1000;
int main()
{
Patient P[MaxPatientSize];
GetList();
do{
cout <<"\nWould you like to read another Patient File? (y or n):";
cin >> resp;
}while ((resp != 'y') && (resp != 'Y') && (resp != 'n') && (resp != 'N'));
if ((resp == 'y') || (resp == 'Y'))
GetList();
else
cout <<"Good Bye!"<< endl;
return 0;
}
void GetList()
{
cout<<"Enter name of Patient List to be read: ";
cin.getline(
ifstream fin(file_name.c_str());
if (!fin)
{
cerr<<"\nCannot open Patient List. Good Bye\n";
exit(1);
}
do
{
cout<<"Would you like to display your Patient List? (y or n): ";
cin >> resp;
}while ((resp != 'y') && (resp != 'Y') && (resp != 'n') && (resp !=
'N'));
if ((resp == 'y') || (resp == 'Y'))
{
for (P = Patient(),
while(getPatient(fin, P))
{
cout<<"\n\n\n"<<setw(25) <<setiosflags(ios::left)<<"Patient Name"
<<setw(20)
<<setiosflags(ios::left)<<"Patient ID" <<setw(15)
<<setiosflags(ios::left)<<"Patient Age"<<endl;
cout <<setw(25)<<setiosflags(ios::left)<<P.getName() <<setw(20)
<<setiosflags(ios::left)<<P.getID() <<setw(15)
<<setiosflags(ios::left)<<P.getAge()<<"\n\n";
fin.close();
P = Patient();
}
else
return;
}
bool getPatient(ifstream &fin, Patient &P)
{
bool isOK = true;
string name = "";
if (getline(fin, name, '\n') && isOK)
fam.setFirstName(name);
else
isOK = false;
if (getline(fin, name, '\n') && isOK)
fam.setLastName(name);
else
isOK = false;
if (getline(fin, name, '\n') && isOK)
fam.setID(name);
else
isOK = false;
/*f (getline(fin, name, '\n') && isOK)
fam.setBirthYear(name);
else
isOK = false; */
return isOK;
}
on a simple program for several days now without being able to complete it.
(Though I have brushed up on a lot of C++ while trying.)
I realize this code is long but, I'm not sure of any other way to display
the issue.
The header file and ?implementation file work. (This assignment was
building on another in which I created the header and implementation with a
simple executable, i.e. display the read data.)
This executable is supposed to 1. create an array of Patient objects, then
2. present a user menu to a. read Patient data from a user specified file,
b. display data. c. quit program.
Facts: max patients = 1000.
Requirements: cannot change header or implement file; use an array; handle
user errors
So here it is, any assistance would be extremely appreciated.
HEADER
#ifndef PATIENT_H
#define PATIENT_H
#include <iostream>
#include <string>
using namespace std;
class Patient
{
public:
Patient();
Patient(string fName, string lName, string idString, int bYear);
string getName() const;
int getAge() const;
string getID() const;
void setFirstName (string fname);
void setLastName (string lname);
void setID(string idString);
void setBirthYear(int bYear);
private:
string firstName;
string lastName;
string ID;
int birthYear;
};
#endif
HEADER DEFINITIONS
#include <iostream>
#include <string>
#include "patient.h"
using namespace std;
Patient:atient() {
setFirstName("First");
setLastName("Last");
setID("Z9999");
setBirthYear(9999);
}
Patient:atient(string fName, string lName, string idString, int bYear) {
setFirstName(fName);
setLastName(lName);
setID(idString);
setBirthYear(bYear);
}
string Patient::getName() const {
return lastName + ", " + firstName;
}
int Patient::getAge() const {
return birthYear;
}
string Patient::getID() const {
return ID;
}
void Patient::setFirstName(string fName) {
firstName = fName;
}
void Patient::setLastName(string lName) {
lastName = lName;
}
void Patient::setID(string idString) {
int i = idString.size();
if (i == 5) {
ID = idString;
}
else {
cerr <<"\nIllegal Patient ID: " <<idString<<" using Z9999"<<endl;
ID = "Z9999";
}
}
void Patient::setBirthYear(int Year) {
if ((Year >1873) && (Year < 2003)) {
birthYear = 2003 - Year;
}
else {
cerr<<"\nIllegal Year: "<<Year<<" using 9999"<<endl;
birthYear = 9999;
}
}
**** Problem Driver ***********
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "patient.h"
using namespace std;
char resp;
void GetList();
bool getPatient(ifstream &fin, Patient &P);
const int MaxPatientSize = 1000;
int main()
{
Patient P[MaxPatientSize];
GetList();
do{
cout <<"\nWould you like to read another Patient File? (y or n):";
cin >> resp;
}while ((resp != 'y') && (resp != 'Y') && (resp != 'n') && (resp != 'N'));
if ((resp == 'y') || (resp == 'Y'))
GetList();
else
cout <<"Good Bye!"<< endl;
return 0;
}
void GetList()
{
cout<<"Enter name of Patient List to be read: ";
cin.getline(
ifstream fin(file_name.c_str());
if (!fin)
{
cerr<<"\nCannot open Patient List. Good Bye\n";
exit(1);
}
do
{
cout<<"Would you like to display your Patient List? (y or n): ";
cin >> resp;
}while ((resp != 'y') && (resp != 'Y') && (resp != 'n') && (resp !=
'N'));
if ((resp == 'y') || (resp == 'Y'))
{
for (P = Patient(),
while(getPatient(fin, P))
{
cout<<"\n\n\n"<<setw(25) <<setiosflags(ios::left)<<"Patient Name"
<<setw(20)
<<setiosflags(ios::left)<<"Patient ID" <<setw(15)
<<setiosflags(ios::left)<<"Patient Age"<<endl;
cout <<setw(25)<<setiosflags(ios::left)<<P.getName() <<setw(20)
<<setiosflags(ios::left)<<P.getID() <<setw(15)
<<setiosflags(ios::left)<<P.getAge()<<"\n\n";
fin.close();
P = Patient();
}
else
return;
}
bool getPatient(ifstream &fin, Patient &P)
{
bool isOK = true;
string name = "";
if (getline(fin, name, '\n') && isOK)
fam.setFirstName(name);
else
isOK = false;
if (getline(fin, name, '\n') && isOK)
fam.setLastName(name);
else
isOK = false;
if (getline(fin, name, '\n') && isOK)
fam.setID(name);
else
isOK = false;
/*f (getline(fin, name, '\n') && isOK)
fam.setBirthYear(name);
else
isOK = false; */
return isOK;
}