Can someone pleaseexplain what this code doing and why; every line does not need
ID: 3742407 • Letter: C
Question
Can someone pleaseexplain what this code doing and why; every line does not need to be commented, but related blocks of code and functions do. So i can understand which function does what.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int Patient_Info(string fullName, int numberOfDays, float dailyRate, float medicalCharges, float medicalServies, float &totalCharges);
int Patient_Info(string fullName, float medicalCharges, float medicalServies, float &totalCharges);
int Patient_Info(string fullName, int numberOfDays, float dailyRate, float medicalCharges, float medicalServies, float &totalCharges) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int success_status = 0;
totalCharges = 0.0;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (numberOfDays > 0 && dailyRate > 0.0 && medicalCharges > 0.0 && medicalServies > 0.0) {
success_status = 1;
cout << "In-patient: name " << fullName << "; " << numberOfDays << "days; $" << dailyRate << "per day; " << medicalCharges << " med charges; hospital services $" << medicalServies << endl;
totalCharges = numberOfDays * dailyRate + medicalCharges + medicalServies;
}
return success_status;
}
int Patient_Info(string fullName, float medicalCharges, float medicalServies, float &totalCharges) {
int Success_Status = 0;
totalCharges = 0.0;
if (medicalCharges > 0.0 && medicalServies > 0.0) {
Success_Status = 1;
cout << "Out-patient: name " << fullName << "; " << medicalCharges << " med charges; hospital services $" << medicalServies << endl;
totalCharges = medicalCharges + medicalServies;
}
return Success_Status;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
string fullName;
int numberOfDays, successStatus, patientType = 0, inPatientCount = 0, outPatientCount = 0, totalPatientCount = 0;
float medicalCharges, medicalServies, dailyRate, totalCharges, inpatientTotalCharges = 0.0, outpatientTotalCharges = 0.0, inAndOutPatientTotalCharges = 0.0;
char yesOrNo;
do {
cout << "Plese enter patient Details: " << endl;
cout << "Patient (1: Inpatient 2:OutPatient)" << endl;
cin >> patientType;
if (patientType != 1 and patientType != 2)
continue;
if (patientType == 1) {
cout << "Enter Patient Fullname: ";
cin >> fullName;
cout << "Enter number of days spent in the hospital: ";
cin >> numberOfDays;
cout << "Enter daily rate: ";
cin >> dailyRate;
cout << "Enter hospital medication charges: ";
cin >> medicalCharges;
cout << "Enter charges for hospital services (lab tests, MRIs, etc.): ";
cin >> medicalServies;
successStatus = Patient_Info(fullName, numberOfDays, dailyRate, medicalCharges, medicalServies, totalCharges);
if (successStatus) {
inPatientCount++;
totalPatientCount++;
inpatientTotalCharges += totalCharges;
}
else {
cout << "InPatient Info provided is not Valid" << endl;
cout << "InPatient Info will not be added to average" << endl;
}
}
if (patientType == 2) {
cout << "Enter Patient Fullname: ";
cin >> fullName;
cout << "Enter hospital medication charges: ";
cin >> medicalCharges;
cout << "Enter charges for hospital services (lab tests, MRIs, etc.): ";
cin >> medicalServies;
successStatus = Patient_Info(fullName, medicalCharges, medicalServies, totalCharges);
if (successStatus) {
outPatientCount++;
totalPatientCount++;
outpatientTotalCharges += totalCharges;
}
else {
cout << "OutPatient Info provided is not Valid" << endl;
cout << "OutPatient Info will not be added to average" << endl;
}
}
cout << "Do you want to Exit (y )" << endl;
if ((cin >> yesOrNo)) {
cout << "Please enter y/n." << endl;
}
if (yesOrNo == 'y') {
break;
}
}
while (yesOrNo != 'y');
cout << "Statistices " << endl;
cout << "No. of inpatients: " << inPatientCount << endl;
if (inPatientCount) {
cout << "Inpatient Average charge is: " << inpatientTotalCharges / inPatientCount << endl;
}
cout << "No. of outpatients " << outPatientCount << endl;
if (outPatientCount) {
cout << "Outpatient Average charge is: " << outpatientTotalCharges / outPatientCount << endl;
}
cout << "No. of patients: " << totalPatientCount << endl;
if (totalPatientCount) {
inAndOutPatientTotalCharges = inpatientTotalCharges + outpatientTotalCharges;
cout << "Patient Average charge is: " << inAndOutPatientTotalCharges / totalPatientCount << endl;
}
system("Pause");
return 1;
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// function prototypes or declarations
// overloaded function Patient_Info() with 2 declarations having different number and types of //arguments for in-patient and out-patient
int Patient_Info(string fullName, int numberOfDays, float dailyRate, float medicalCharges, float medicalServies, float &totalCharges);
int Patient_Info(string fullName, float medicalCharges, float medicalServies, float &totalCharges);
// function definitions
int Patient_Info(string fullName, int numberOfDays, float dailyRate, float medicalCharges, float medicalServies, float &totalCharges) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int success_status = 0;
totalCharges = 0.0;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (numberOfDays > 0 && dailyRate > 0.0 && medicalCharges > 0.0 && medicalServies > 0.0) {
success_status = 1;
cout << "In-patient: name " << fullName << "; " << numberOfDays << "days; $" << dailyRate << "per day; " << medicalCharges << " med charges; hospital services $" << medicalServies << endl;
// compute total charges for In-patient
totalCharges = numberOfDays * dailyRate + medicalCharges + medicalServies;
}
return success_status;
}
int Patient_Info(string fullName, float medicalCharges, float medicalServies, float &totalCharges) {
int Success_Status = 0;
totalCharges = 0.0;
if (medicalCharges > 0.0 && medicalServies > 0.0) {
Success_Status = 1;
cout << "Out-patient: name " << fullName << "; " << medicalCharges << " med charges; hospital services $" << medicalServies << endl;
// compute total charges for out-patient
totalCharges = medicalCharges + medicalServies;
}
return Success_Status;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
string fullName;
int numberOfDays, successStatus, patientType = 0, inPatientCount = 0, outPatientCount = 0, totalPatientCount = 0;
float medicalCharges, medicalServies, dailyRate, totalCharges, inpatientTotalCharges = 0.0, outpatientTotalCharges = 0.0, inAndOutPatientTotalCharges = 0.0;
char yesOrNo;
do { // loop to process In-patients and out-patients until user exits
cout << "Plese enter patient Details: " << endl;
cout << "Patient (1: Inpatient 2:OutPatient)" << endl;
cin >> patientType;
if (patientType != 1 and patientType != 2) // Invalid patientType
continue; // skip following code
if (patientType == 1) { // In-patient
cout << "Enter Patient Fullname: ";
cin >> fullName;
cout << "Enter number of days spent in the hospital: ";
cin >> numberOfDays;
cout << "Enter daily rate: ";
cin >> dailyRate;
cout << "Enter hospital medication charges: ";
cin >> medicalCharges;
cout << "Enter charges for hospital services (lab tests, MRIs, etc.): ";
cin >> medicalServies;
successStatus = Patient_Info(fullName, numberOfDays, dailyRate, medicalCharges, medicalServies, totalCharges);
if (successStatus) { // successStatus = 1
inPatientCount++; // increment in-patients
totalPatientCount++; // increment total patients
inpatientTotalCharges += totalCharges; // compute total charges for in-patients
}
else {
cout << "InPatient Info provided is not Valid" << endl;
cout << "InPatient Info will not be added to average" << endl;
}
}
if (patientType == 2) { // out-patient
cout << "Enter Patient Fullname: ";
cin >> fullName;
cout << "Enter hospital medication charges: ";
cin >> medicalCharges;
cout << "Enter charges for hospital services (lab tests, MRIs, etc.): ";
cin >> medicalServies;
successStatus = Patient_Info(fullName, medicalCharges, medicalServies, totalCharges);
if (successStatus) { // successStatus = 1
outPatientCount++; // increment total out-patients
totalPatientCount++; // increment total patients
outpatientTotalCharges += totalCharges; //compute total outpatient charges
}
else {
cout << "OutPatient Info provided is not Valid" << endl;
cout << "OutPatient Info will not be added to average" << endl;
}
}
cout << "Do you want to Exit (y )" << endl; // check if user wants to process more patients
if ((cin >> yesOrNo)) {
cout << "Please enter y/n." << endl;
}
if (yesOrNo == 'y') {
break;
}
}
while (yesOrNo != 'y'); // user does not want to process more patients
cout << "Statistices " << endl;
cout << "No. of inpatients: " << inPatientCount << endl;
if (inPatientCount) { // if in-patientCount != 0
cout << "Inpatient Average charge is: " << inpatientTotalCharges / inPatientCount << endl;
}
cout << "No. of outpatients " << outPatientCount << endl;
if (outPatientCount) { // if out-patient count is != 0
cout << "Outpatient Average charge is: " << outpatientTotalCharges / outPatientCount << endl;
}
cout << "No. of patients: " << totalPatientCount << endl;
if (totalPatientCount) { // if total patients != 0
inAndOutPatientTotalCharges = inpatientTotalCharges + outpatientTotalCharges;
cout << "Patient Average charge is: " << inAndOutPatientTotalCharges / totalPatientCount << endl;
}
system("Pause");
return 1;
}
Added the comments. Do ask if any doubt. Please upvote.