The following C++ code include 3 files: Patient.h, Patient.cpp
and Main.cpp. The program basically creates and stores patient
records. The original code has everything in a single .cpp file. I
tried to divide the code in 3 parts (Patient.h, Patient.cpp and
Main.cpp), but it is giving me errors.
Patient.h
#ifndef PATIENT_H
#define PATIENT_H
#include <string>
#include "Patient.cpp"
using namespace std;
class Patient{
private :
string firstname;
string lastname;
string location;
static int cnt;
int id;
public :
Patient(string, string, string);
string toString();
void setFirstName(string);
void setLastName(string);
void setLocation(string);
string getFirstName();
string getLastName();
string getLocation();
int getId();
};
#endif // PATIENT_H
Patient.cpp
#include "Patient.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
Patient::Patient(string firstname,string lastname,string
location){
cnt++;
this->firstname=firstname;
this->lastname=lastname;
this->location=location;
this->id=cnt;
}
string Patient::toString(){
stringstream ss;
if(id<=9){
ss << "Patient ID: " << "0" << id <<
endl;
}else{
ss<<"Patient ID:"<<id<<endl;
ss<<firstname<<endl;
ss<<lastname<<endl;
ss<<location<<endl;
}
return ss.str();
}
void Patient::setFirstName(string fname){
this->firstname=fname;
}
void Patient::setLastName(string lname){
this->lastname=lname;
}
void Patient::setLocation(string loc){
this->location=loc;
}
string Patient::getFirstName(){
return firstname;
}
string Patient::getLastName(){
return lastname;
}
string Patient::getLocation(){
return location;
}
int Patient::getId(){
return id;
}
int Patient::cnt=0;
Main.cpp
#include <iostream>
#include <iomanip>
#include <vector>
#include <sstream>
#include <fstream>
#include "Patient.h"
#include "Patient.cpp"
using namespace std;
int search(vector<Patient> patients,int id);
int main(){
string fname,lname,loc;
int id;
int choice ;
vector<Patient> patients;
while(true){
cout<<"\nMENU:"<<endl;
cout<<"\t1) Enter a patient record"<<endl;
cout<<"\t2) Print all patient records"<<endl;
cout<<"\t3) Export list to a file"<<endl;
cout<<"\t4) Edit patient information"<<endl;
cout<<"\t5) EXIT"<<endl;
cout<<"\n\tEnter Option:";
cin>>choice;
switch(choice){
case 1:{
cout<<"Enter patient's first name :";
cin>>fname;
cout<<"Enter patient's last name :";
cin>>lname;
cin.ignore();
cout<<"Enter patient's location :";
getline(cin,loc);
Patient p(fname,lname,loc);
patients.push_back(p);
continue;
}
case 2:{
cout<<"Displaying all records :"<<endl;
for(int i=0;i<patients.size();i++){
cout<<patients[i].toString();
}
continue;
}
case 3:{
ofstream dataOut;
dataOut.open("patientsRecords.txt");
for(int i=0;i<patients.size();i++){
dataOut<<patients[i].toString();
}
dataOut.close();
continue;
}
case 4:{
cout<<"Enter patient id :";
cin>>id;
int indx=search(patients,id);
if(indx==-1){
cout<<id<<" not found."<<endl;
}else{
cout<<"Enter new firstname:";
cin>>fname;
cout<<"Enter new lastname:";
cin>>lname;
cin.ignore();
cout<<"Enter new location:";
getline(cin,loc);
patients[indx].setFirstName(fname);
patients[indx].setLastName(lname);
patients[indx].setLocation(loc);
}
continue;
}
case 5:{
break;
}
default:{
cout<<"** Invalid Choice **"<<endl;
continue;
}
}
break;
}
return 0;
}
int search(vector<Patient> patients,int id){
for(int i=0;i<patients.size();i++){
if(patients[i].getId()==id){
return i;
}
}
return -1;
}
use this, I have shuffled few imports
Patient.h
#ifndef PATIENT_H
#define PATIENT_H
#include <string>
//#include "Patient.cpp"
using namespace std;
class Patient{
private :
string firstname;
string lastname;
string location;
static int cnt;
int id;
public :
Patient(string, string, string);
string toString();
void setFirstName(string);
void setLastName(string);
void setLocation(string);
string getFirstName();
string getLastName();
string getLocation();
int getId();
};
#endif // PATIENT_H
----------------------------------------------------------------------------------------------------------------------------------------
Patient.cpp
#include <iostream>
#include <iomanip>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include "Patient.h"
using namespace std;
Patient::Patient(string firstname,string lastname,string location){
cnt++;
this->firstname=firstname;
this->lastname=lastname;
this->location=location;
this->id=cnt;
}
string Patient::toString(){
stringstream ss;
if(id<=9){
ss << "Patient ID: " << "0" << id << endl;
}else{
ss<<"Patient ID:"<<id<<endl;
ss<<firstname<<endl;
ss<<lastname<<endl;
ss<<location<<endl;
}
return ss.str();
}
void Patient::setFirstName(string fname){
this->firstname=fname;
}
void Patient::setLastName(string lname){
this->lastname=lname;
}
void Patient::setLocation(string loc){
this->location=loc;
}
string Patient::getFirstName(){
return firstname;
}
string Patient::getLastName(){
return lastname;
}
string Patient::getLocation(){
return location;
}
int Patient::getId(){
return id;
}
int Patient::cnt=0;
-------------------------------------------------------------------------------
//Main.cpp
#include <iostream>
#include <iomanip>
#include <vector>
#include <sstream>
#include <fstream>
#include "Patient.h"
using namespace std;
int search(vector<Patient> patients,int id);
int main(){
string fname,lname,loc;
int id;
int choice ;
vector<Patient> patients;
while(true){
cout<<"\nMENU:"<<endl;
cout<<"\t1) Enter a patient record"<<endl;
cout<<"\t2) Print all patient records"<<endl;
cout<<"\t3) Export list to a file"<<endl;
cout<<"\t4) Edit patient information"<<endl;
cout<<"\t5) EXIT"<<endl;
cout<<"\n\tEnter Option:";
cin>>choice;
switch(choice){
case 1:{
cout<<"Enter patient's first name :";
cin>>fname;
cout<<"Enter patient's last name :";
cin>>lname;
cin.ignore();
cout<<"Enter patient's location :";
getline(cin,loc);
Patient p(fname,lname,loc);
patients.push_back(p);
continue;
}
case 2:{
cout<<"Displaying all records :"<<endl;
for(int i=0;i<patients.size();i++){
cout<<patients[i].toString();
}
continue;
}
case 3:{
ofstream dataOut;
dataOut.open("patientsRecords.txt");
for(int i=0;i<patients.size();i++){
dataOut<<patients[i].toString();
}
dataOut.close();
continue;
}
case 4:{
cout<<"Enter patient id :";
cin>>id;
int indx=search(patients,id);
if(indx==-1){
cout<<id<<" not found."<<endl;
}else{
cout<<"Enter new firstname:";
cin>>fname;
cout<<"Enter new lastname:";
cin>>lname;
cin.ignore();
cout<<"Enter new location:";
getline(cin,loc);
patients[indx].setFirstName(fname);
patients[indx].setLastName(lname);
patients[indx].setLocation(loc);
}
continue;
}
case 5:{
break;
}
default:{
cout<<"** Invalid Choice **"<<endl;
continue;
}
}
break;
}
return 0;
}
int search(vector<Patient> patients,int id){
for(int i=0;i<patients.size();i++){
if(patients[i].getId()==id){
return i;
}
}
return -1;
}
----------------------------------------------------------------------------------------------------------------
SEE OUTPUT
Thanks, PLEASE COMMENT if there is any concern. PLEASE
UPVOTE
The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...
Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found; string document[1000][6]; ifstream infile; char s[1000];...
How could I separate the following code to where I have a gradebook.cpp and gradebook.h file? #include <iostream> #include <stdio.h> #include <string> using namespace std; class Gradebook { public : int homework[5] = {-1, -1, -1, -1, -1}; int quiz[5] = {-1, -1, -1, -1, -1}; int exam[3] = {-1, -1, -1}; string name; int printMenu() { int selection; cout << "-=| MAIN MENU |=-" << endl; cout << "1. Add a student" << endl; cout << "2. Remove a...
Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...
I need to get this two last parts of my project done by tonight. If you see something wrong with the current code feel free to fix it. Thank you! Project 3 Description In this project, use project 1 and 2 as a starting point and complete the following tasks. Create a C++ project for a daycare. In this project, create a class called child which is defined as follows: private members: string First name string Last name integer Child...
Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...
The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...
in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...
/* BEGIN - DO NOT EDIT CODE */ // File: main.cpp #include "LoginAccount.h" #include "RegisteredLoginAccount.h" #include "GuestLoginAccount.h" #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; //prototypes and constants const int ARRAY_SIZE = 20; using number_of_records_t = int; //Function Declarations. void readAccountsFromFile(ifstream& fin, RegisteredLoginAccount registeredAccounts[], number_of_records_t& registeredLength, GuestLoginAccount guestAccounts[], number_of_records_t& guestLength); bool openFileForInput(ifstream& ifs, const string& filename); bool openFileForOutput(ofstream& ofs, const string& filename); void writeAccountsToFile(ofstream& ofs, RegisteredLoginAccount registeredAccounts[], const number_of_records_t registeredLength, GuestLoginAccount guestAccounts[], const number_of_records_t guestLength); void writeAccountToFile(ofstream&...
Following class is only part of my program that uses a hash table to simulate a market's client database, client class is my main class which asks user to input client names and then creates a hash table which then users can look up client names. wasn't able to upload everything as it is too much code, but what I need is to modify my client class instead of me inputting data line by line, the program should read from...
Please implement the following problem in basic C++ code and include detailed comments so that I am able to understand the processes for the solution. Thanks in advance. // personType.h #include <string> using namespace std; class personType { public: virtual void print() const; void setName(string first, string last); string getFirstName() const; string getLastName() const; personType(string first = "", string last = ""); protected: string firstName; string lastName; }; // personTypeImp.cpp #include <iostream> #include <string> #include "personType.h" using namespace std; void...