Question #55226

Write a program that computes and displays the charges for a patient’s hospital stay. First,
the program should ask if the patient was admitted as an in-patient or an out-patient. If the
patient was an in-patient the following data should be entered:
• The number of days spent in the hospital
• The daily rate
• Charges for hospital services (lab tests, etc.)
• Hospital medication charges.
If the patient was an out-patient the following data should be entered:
• Charges for hospital services (lab tests, etc.)
• Hospital medication charges.
Use a single, separate function to validate that no input is less than zero. If it is, it should
be re-entered before being returned.
Once the required data has been input and validated, the program should use two
overloaded functions to calculate the total charges. One of the functions should accept
arguments for the in-patient data, while the other function accepts arguments for out-
patient data. Both functions should return the total charges.

Expert's answer

#include <iostream>
using namespace std;
// this function reads integer value and re-read until it's 0 or positive.
int readNotNegative() {
    int x;
    cin >> x;
    while (x < 0) {
        cout << "Please enter not negative data: ";
        cin >> x;
    }
    return x;
}
// for out-patient total charges amount is sum of services and medication.
int computeTotalCharges(int hospitalServices, int hospitalMedication) {
    return hospitalMedication + hospitalServices;
}
// for in-patient total charges amount is number of days multiplied by daily rate plus sum of services and medication.
int computeTotalCharges(int numberOfDays, int dailyRate, int hospitalServices, int hospitalMedication) {
    return numberOfDays * dailyRate + hospitalMedication + hospitalServices;
}
int main() {
    // get patient type.
    int patientType;
    cout << "Enter 0 for in-patient or 1 for out-patient: ";
    cin >> patientType;
    // re-read patient type until valid
    while ((patientType != 0) && (patientType != 1)) {
        cout << "Incorrect data! Enter 0 for in-patient or 1 for out-patient: ";
        cin >> patientType;
    }
    int numberOfDays = 0;
    int dailyRate = 0;
    int hospitalServices = 0;
    int hospitalMedication = 0;
    // we read this data only for in-patient
    if (patientType == 0) {
        cout << "Enter number of days spent in the hospital: ";
        numberOfDays = readNotNegative();
        cout << "Enter daily rate: ";
        dailyRate = readNotNegative();
    }
    // we read this data for both in-patient and out-patient
    cout << "Enter charges for hospital services: ";
    hospitalServices = readNotNegative();
    cout << "Enter hospital medication charges: ";
    hospitalMedication = readNotNegative();
    // compute total charges depending on patient type
    int totalCharges;
    if (patientType == 0) {
        totalCharges = computeTotalCharges(numberOfDays, dailyRate, hospitalServices,
            hospitalMedication);
    } else {
        totalCharges = computeTotalCharges(hospitalServices, hospitalMedication);
    }
    cout << "Total charges: " << totalCharges << endl;
    // this is to keep console window to let user read the answer.
    system("pause");
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS