chp6 Overloaded Hospital

#include <iostream>
using namespace std;

double inputValidate(double);
char userChoice(char);
double calculateTotalCharges(double, double);
double calculateTotalCharges(double, double, double, double);

int main()
{
    char user_choice; // 1 or 2

    double days_spent,
           daily_rate,
           med_charges,
           services_charge,
           total_charges;

    cout << "Where you admitted as an " << endl
         << "in-patient or an out-patient? " << endl
         << "Enter 1 for in-patient or 2 for out-patient"
         << endl;
     
    user_choice = userChoice(user_choice);

    if (user_choice == '1')
    {
        cout << "Number of days spent in the hospital: ";
        days_spent = inputValidate(days_spent);

        cout << "Daily rate: $";
        daily_rate = inputValidate(daily_rate);

        cout << "Hospital medication charges: $";
        med_charges = inputValidate(med_charges);

        cout << "Charges for hospital services\n"
            << "(lab tests, etc.): $";
        services_charge = inputValidate(services_charge);

        total_charges = calculateTotalCharges(days_spent,
                                              daily_rate,
                                              med_charges,
                                              services_charge);
                        
        cout << "total_charges = $" 
            << total_charges
            << endl;
    }
    else
    {
        cout << "Hospital medication charges: $";
        med_charges = inputValidate(med_charges);

        cout << "Charges for hospital services\n"
            << "(lab tests, etc.): $";
        services_charge = inputValidate(services_charge);

        total_charges = calculateTotalCharges(med_charges,
                                              services_charge);

        cout << "total_charges = $" 
               << total_charges
               << endl;
    }
    

    return 0;
}

char userChoice(char letter)
{
    cin >> letter;
    // Error check algorithim
    while (!((letter == '1') || (letter == '2')))
    {
        // Explain error
        cout << "ERROR: enter 1 or 2: ";
        // Clear input stream
        cin.clear();
        // Discard previous input
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        // Receive input again
        cin >> letter;
    }
    return letter;
}

double inputValidate(double num)
{
    
    while(!(cin >> num) || num < 0)
    {
        cout << "Error. Number must not be "
             << " 0 or greater:";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    return num;
}

double calculateTotalCharges(double med_charges, 
                             double services_charge)
{
     return med_charges + services_charge;
}

double calculateTotalCharges(double days_spent, 
                             double daily_rate, 
                             double med_charges, 
                             double services_charge)
{
     return (days_spent * daily_rate) + med_charges + services_charge;
}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source