Beg Class challenge for a returning student

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::patient() {
setFirstName("First");
setLastName("Last");
setID("Z9999");
setBirthYear(9999);
}
Patient::patient(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;
}
 
M

Mike Wahler

mchoya said:
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.

You haven't told us what issues you're having.
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.

Tell us what problems you're having. Be specific.

-Mike
 
R

rossum

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.
Yes, the .cpp file is called the implementation file.
(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.
Help us to help you by clearly explaining the problem you are having.
I will give some comments but since I do not know what problem you
want us to look at, I do not know if my comments are relevant.
HEADER
#ifndef PATIENT_H
#define PATIENT_H

#include <iostream>
Why are you including iosteam? You do not use it in this file.
#include <string>

using namespace std;
AAARRGGGHHHH! NEVER put this in a header file. You are likely to
give yourself big problems with clashing names if you do this with any
sort of sizeable program. By doing this you undo all the good work
that namespaces can do for you by keeping names separate.
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::patient() {
setFirstName("First");
setLastName("Last");
setID("Z9999");
setBirthYear(9999);
}
A point of style, better to use some vertical space to separate
functions, it makes the code easieer to read. Also on a point of
style, it is unusual to use as little as one space for indentation,
three or four is more usual.
Patient::patient(string fName, string lName, string idString, int bYear) {
setFirstName(fName);
You don't have to use the functions, you can assign directly to the
data if you want.
setLastName(lName);
setID(idString);
setBirthYear(bYear);
}
string Patient::getName() const {
return lastName + ", " + firstName;
}
All these one-liners are ideal for inlining.
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(
You have missed off the parameters, closing parenthesis and semicolon.
You compiler should start complaining about this point. Please
either post compilable code or copy the compiler's messages along with
the code.
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(),
You have left off much of this for statement. Again your compiler
should be complaining.
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)
getline defaults to '\n' as delimiter so it is not necessary to
include it explicitly.
fam.setFirstName(name);
What is "fam"? Where is it defined? Your compiler should be
complaining about fam also.
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;
}

rossum
 
M

mchoya

Of course your right, sorry.

Well, I'm receiveing "unresolved external links" to the header file getAge,
getID, getName.

But , I guess I'm looking for direction in the following, regarding the
executable file:
can I declare an array while initializing an object; ex: Patient
P[MaxSize], where MaxSize has been declared.(my goal being to limit the
number of Patient files (think hospital beds) to MaxSize
have I chosen appropriate parameters for the function getList in the
executable file

Thanks

mchoya said:
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::patient() {
setFirstName("First");
setLastName("Last");
setID("Z9999");
setBirthYear(9999);
}
Patient::patient(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;
}
 
T

Thomas Matthews

Thanks.

top-post.
don't
Please

mchoya wrote:
[Re-arranged]
[snip]
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;
int bYear() const;
Use const reference to avoid the overhead of passing a whole
string structure:
void setFirstName(const string& first_name);
See above.
See above.
My preference is to include some whitespace between
the public, private and protected sections.
You _may_ want this as an unsigned signed int since
people can't be born in a negative year.
You may want to leave the fields empty for the default constructor.

Prefer to use initialization lists:
Patient::patient(const string& fName,
const string& lName,
const string& idString,
unsigned int bYear)
/* here is the initialization list */
: firstName(fname),
lastName(lname),
ID(idString),
birthYear(bYear)
{
}

If initialization is more complicated, then use an
initialization method and have each constructor call
it.

You may want to change the title of this method.
Some users may want to have both last name first
and first then last layouts.

Where did this magic number come from?
Explain to the user why the ID is illegal:
cerr << "Patient ID string too long.".
Although you may want to through an exception because
this method assumes that cerr will be available and
the user will be looking at it. With an exception,
the function using Patient::setID will get to choose
what to do with this error.

Why bother altering the year. There is nothing to
gain, but much more to lose. Just check for boundary
conditions and throw an exception. An integer, signed
or unsigned, has enough capacity for a persons birth
year.

Hmmm, does the driver have a problem? :)

#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'));
Do yourself a favor and research the std::toupper and std::tolower
methods. This will simplify to:
cin >> resp;
resp = std::toupper(resp);
} while (resp != 'Y' && resp != 'N');

if (resp == 'Y') /* see std::toupper */

See EXIT_SUCCESS in <cstdlib>
return EXIT_SUCCESS;

This line is an error. The compiler should have said something.

See EXIT_FAILURE in <cstdlib>
exit(EXIT_FAILURE);
This is bad karma to have a function terminate the program.
The function should throw an exception or return a status.
Let the main() function be the only function that terminates
the program.

std::toupper or std::tolower. 'Nuff said.

What language is this?
1. Don't mix while and for loops.
2. A "for" loop implies one or more iterations.
3. A "while" loop implies zero or more iterations.
More bad karma.

Looks like your Patient class would really benefit by overloading
operator <<. This would simplify to:
cout << P;
So, why is the file closed after reading one patient?

This isn't necessary. No reason to clear an object when
it will be overwritten; just a waste of time and energy.
Poor karma.

Note: This function could be replaced by overloading
the operator>> in the Patient class.
The first line does not need to check the isOK
variable.
Beware of using cut and paste.
My suggestion is to implement two methods in your
Patient class that load and store to a string.

For example, your above input function will fail
if the data fields are all on one line separated
by tabs or commas. You will need another method
when retrieving the data from a database or
other input source.

My experience is to:
1. Input the data to a string.
2. Have the object initialize its members from
the given string.

In your program, the above simplifies to:
std::string text_line;
std::string patient_record;
std::vector<Patient> patient_list;
while (fin)
{
for (unsigned int i = 0;
(i < LINES_PER_RECORD) && (getline(fin, text_line);
++i;
{
if (i > 0)
{
patient_record += '\t';
}
patient_record += text_line;
}
if (i < LINES_PER_RECORD)
{
// Handle case of not enough data.
}
else
{
Patient new_patient;
new_patient.load_from(text_line);
patient_list.push_back(new_patient);
}
}

Of course your right, sorry.

Well, I'm receiveing "unresolved external links" to the header
getAge, getID, getName.

But , I guess I'm looking for direction in the following, regarding the
executable file:
can I declare an array while initializing an object; ex: Patient
P[MaxSize], where MaxSize has been declared.(my goal being to limit the
number of Patient files (think hospital beds) to MaxSize
have I chosen appropriate parameters for the function getList in the
executable file

Thanks

You also have to make sure that your linker is fed all object
modules or your project includes all your source files.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,824
Latest member
Nater888

Latest Threads

Top