Answer to Question #192233 in C++ for Shuvo

Question #192233

Write a program that computes and displays the charges for a patient’s hos- pital stay. First, the program should ask if the patient was admitted as an inpatient or an outpatient. If the patient was an inpatient, the following data should be entered:

• The number of days spent in the hospital

• The daily rate

• Hospital medication charges

• Charges for hospital services (lab tests, etc.)

The program should ask for the following data if the patient was an outpatient: • Hospital medication charges

• Charges for hospital services (lab tests, etc.)

The program should use two overloaded functions to calculate the total charges. One of the functions should accept arguments for the inpatient data, and the other function accepts arguments for outpatient information. Both functions should return total charges. All functions should be tested individually.

Input validation: Do not accept negative values for any data, and make your program robust against accidental non-numeric data.


1
Expert's answer
2021-05-12T02:14:16-0400
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
float totalCharges(int days, float rate, float charges, float services){
    return days * rate + charges + services;
}
float totalCharges(float charges, float services){
    return charges + services;
}
bool check(string s){
    for(int i = 0; i < s.length(); i++)
        if(s[i] < '0' || s[i] > '9') return false;
    return true;
}
int main(){
    int choice, days;
    float rate, charges, services, total;
    bool flag = false;
    string s;
    do{    
        cout<<"Type of patient;\n";
        cout<<"1. Inpatient\n";
        cout<<"2. Outpatient\n";
        cin>>s;
        if(!check(s)) choice = 0;
        else choice = stoi(s);
        switch(choice){
            case 1: do{
                        flag = false;
                        cout<<"Number of days stayed in hospital: ";
                        cin>>s;
                        if(!check(s)){
                            cout<<"Invalid input!\n";
                            flag = true;
                        }
                        else days = stoi(s);
                    }while(flag);
                    
                    do{
                        flag = false;
                        cout<<"Daily rate: ";
                        cin>>s;
                        if(!check(s)){
                            cout<<"Invalid input!\n";
                            flag = true;
                        }
                        else rate = stoi(s);
                    }while(flag);
                    
                    do{
                        flag = false;
                        cout<<"Hospital medication charges: ";
                        cin>>s;
                        if(!check(s)){
                            cout<<"Invalid input!\n";
                            flag = true;
                        }
                        else charges = stoi(s);
                    }while(flag);
                    
                    do{
                        flag = false;
                        cout<<"Charges for other hospital services (lab tests, etc.): ";
                        cin>>s;
                        if(!check(s)){
                            cout<<"Invalid input!\n";
                            flag = true;
                        }
                        else services = stoi(s);
                    }while(flag);
                    total = totalCharges(days, rate, charges, services);
                    break;
                    
            case 2: do{
                        flag = false;
                        cout<<"Hospital medication charges: ";
                        cin>>s;
                        if(!check(s)){
                            cout<<"Invalid input!\n";
                            flag = true;
                        }
                        else charges = stoi(s);
                    }while(flag);
                    
                    do{
                        flag = false;
                        cout<<"Charges for other hospital services (lab tests, etc.): ";
                        cin>>s;
                        if(!check(s)){
                            cout<<"Invalid input!\n";
                            flag = true;
                        }
                        else services = stoi(s);
                    }while(flag);
                    total = totalCharges(charges, services);
                    break;
            default: cout<<"Invalid choice!\n"; break;
        }
    }while(choice < 1 || choice > 2);


    cout<<"\nTotal Charge: "<<fixed<<setprecision(2)<<total;
    return 0;
}

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!

Comments

No comments. Be the first!

Leave a comment