I have to write a program that must compute and display the charges for a patients hospital stay. first the program should ask if the patient was admitted as an in patient or an out patient. If the patient was as 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.)
hosppital medication charges

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.
Input validation: do not accept negative numbers for any information. Im having trouble getting my data to come out right.

And this is what I have to far: // This program computes and displays patient hospital charges.// It uses overloaded functions.#include <iostream>#include <iomanip>using namespace std;// Function prototypesdouble patientCharges(int, double, double, double); // In-patientdouble patientCharges(double, double); // Out-patientint main(){ char patientType; // I=in-patient, O=out-patient int days; // Number of days of hospital stay double roomRate, // Daily room rate medication, // Total medication charges services, // Total for tests and other services totalCharges; // Total of all charges // Input and validate patient type cout << "This program will compute patient hospital charges.\n"; cout << "Enter I for in-patient or O for out-patient: "; cin >> patientType; //add code here // Input and validate data relevant to in-patients //add code here // Input and validate data relevant to all patients //add code here // Call correct patientCharges function to return total charges //add code here // Display the billing statment cout << fixed << showpoint << setprecision(2) << endl << endl; cout << "******************************\n"; if (patientType == 'I') cout << "Room charges $" << setw(8) << days*roomRate << endl; if (services > 0.0) cout << "Lab & Services $" << setw(8) << services << endl; if (medication > 0.0) cout << "Medication $" << setw(8) << medication << endl; cout << "Total charges $" << setw(8) << totalCharges << endl; cout << "******************************\n"; return 0;}// End of main function/************************************************************* * patientCharge * * This function is called by main to calculate and return * * total patient charges for in-patients * *************************************************************/double patientCharges(int days, double rate, double med, double serv){ //add code here}// end overload function patientCharges/************************************************************* * patientCharge * * This function is called by main to calculate and return * * total patient charges for out-patients * *************************************************************/double patientCharges(double med, double serv){ //add code here}// end overload function patientCharges can anyone help?

Dani AI

Generated

A few concrete issues in the original posts: inconsistent variable names and typos (for example patientTye), missing return types or mismatched prototypes, unterminated cout strings, and no robust input validation. was right to ask for properly formatted code; 's comment about editor coloring correctly hints at missing quotes / a missing return type. The corrected approach below uses two overloaded functions (same name), defends against negative or non-numeric input, and prints room charges only for in-patients.

#include <iostream>
#include <iomanip>
#include <limits>
#include <cctype>

using namespace std;

double patientCharges(int days, double dailyRate, double services, double medication) {
    return days * dailyRate + services + medication;
}

double patientCharges(double services, double medication) {
    return services + medication;
}

int main() {
    char pType;
    cout << "Compute hospital charges.\nEnter I for in-patient or O for out-patient: ";
    while (cin >> pType) {
        pType = toupper(static_cast<unsigned char>(pType));
        if (pType == 'I' || pType == 'O') break;
        cout << "Invalid entry. Enter I or O: ";
    }

    int numDays = 0;
    double dailyRate = 0.0;
    double svcCharges = 0.0;
    double medCharges = 0.0;

    if (pType == 'I') {
        cout << "Number of days (non-negative): ";
        while (!(cin >> numDays) || numDays < 0) {
            cout << "Enter a non-negative integer for days: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        cout << "Daily room rate (non-negative): ";
        while (!(cin >> dailyRate) || dailyRate < 0.0) {
            cout << "Enter a non-negative number for rate: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }

    cout << "Hospital services charges (non-negative): ";
    while (!(cin >> svcCharges) || svcCharges < 0.0) {
        cout << "Enter non-negative number for services: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    cout << "Medication charges (non-negative): ";
    while (!(cin >> medCharges) || medCharges < 0.0) {
        cout << "Enter non-negative number for medication: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    double total = (pType == 'I')
        ? patientCharges(numDays, dailyRate, svcCharges, medCharges)
        : patientCharges(svcCharges, medCharges);

    cout << fixed << setprecision(2) << "\n******************************\n";
    if (pType == 'I') cout << "Room charges    $" << setw(8) << numDays * dailyRate << '\n';
    if (svcCharges > 0.0) cout << "Lab & Services  $" << setw(8) << svcCharges << '\n';
    if (medCharges > 0.0) cout << "Medication      $" << setw(8) << medCharges << '\n';
    cout << "Total charges   $" << setw(8) << total << '\n';
    cout << "******************************\n";
    return 0;
}

Notes and quick checklist:

  • Fix typos and keep consistent names (e.g., patientType vs patientTye).
  • Validate every numeric input (use cin.clear() and cin.ignore(...) on failure).
  • Use toupper() to accept both I and i.
  • Compile with warnings enabled (e.g., g++ -Wall -Wextra) to catch missing return types and unused variables early.

This sample implements the overloaded functions requested by and addresses the formatting/typo hints from and while providing a ready-to-compile starting point.

Recommended Answers

All 7 Replies

Sorry, but it's impossible to read your snippet.
Please, use code tag properly:
[code=c++] sources (with line breaks)

[/code]

// This program computes and displays patient hospital charges.
// It uses overloaded functions.
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes double patientCharges(int, double, double, double); 
// In-patientdouble patientCharges(double, double);			   	 
// Out-patientint main()
{	
char patientType;	          
// I=in-patient, O=out-patient	int  days;						
// Number of days of hospital stay	double roomRate,	          
// Daily room rate	       medication,         
 // Total medication charges          services,          
  // Total for tests and other services          
totalCharges;	       // Total of all charges	
// Input and validate patient type   cout << "This program will compute patient hospital charges.\n";	
cout << "Enter I for in-patient or O for out-patient: ";   
cin  >> patientType;			
//add code here	     	 
// Input and validate data relevant to in-patients			
//add code here   	
// Input and validate data relevant to all patients   		
//add code here  	
// Call correct patientCharges function to return total charges			
//add code here    	
// Display the billing statment	
cout << fixed << showpoint << setprecision(2) << endl << endl;   
cout << "******************************\n";   
if (patientType == 'I')      
cout << "Room charges    $" << setw(8) << days*roomRate << endl;   
if (services > 0.0)      
cout << "Lab & Services  $" << setw(8) << services << endl;   
if (medication > 0.0)      
cout << "Medication      $" << setw(8) << medication << endl;   
cout    << "Total charges   $" << setw(8) << totalCharges << endl;	
cout << "******************************\n";   	
return 0;

}// End of main function/************************************************************* *                       
 patientCharge                      
* * This function is called by main to calculate and return   * * total patient charges for in-patients

Are you sure that this typo nightmare is better than previous one?
Once more:
[code=c++] source(s) (with line breaks!!!)

[/code]

#include <iostream
#include <iomanip>
using namespace std;
//function prototypes
double patientcharges(int, double, double, double);
//in- patient double
patientCharges(double,double);
//out-patient

int main()

{
char patientTye;
//I = in-patient;
//O = out-patient;
int days; //num days of hospital stay
double roomRate;
//Daily room rate
double medication;
// Total medication charges          
double services,          
  // Total for tests and other services          
totalCharges;	      
 // Total of all charges	
// Input and validate patient type   
cout << "This program will compute patient hospital charges	
cout << "Enter I for in-patient or O for out-patient: ";   
cin  >> patientType;			
//add code here	     	 
// Input and validate data relevant to in-patients		
//add code here   	
// Input and validate data relevant to all patients   	
//add code here  	
// Call correct patientCharges function to return total charge	
//add code here    	
// Display the billing statment	
cout << fixed << showpoint << setprecision(2) << endl << endl;   
cout << "******************************\n";   
if (patientType == 'I')      
cout << "Room charges    $" << setw(8) <<  days*roomRate<<endl;   
if (services > 0.0)      
cout << "Lab & Services  $" << setw(8) << services << endl;   
if (medication > 0.0)      
cout << "Medication      $" << setw(8) << medication << endl;   
cout    << "Total charges   $" << setw(8) << totalCharges << endl;
cout << "******************************\n";   	
return 0;
}

//Calculate function that i have no clue how to do
#include <iostream
#include <iomanip>
using namespace std;


//function prototypes
double patientcharges(int, double, double, double);
//in- patient double
patientCharges(double,double);
//out-patient


int main()
{
    char patientTye;
    //I = in-patient;
    //O = out-patient;
    int days; //num days of hospital stay
    double roomRate;
    //Daily room rate
    double medication;
    // Total medication charges
    double services,
    // Total for tests and other services
    totalCharges;
    // Total of all charges
    // Input and validate patient type
    
    
    cout << "This program will compute patient hospital charges
    cout << "Enter I for in-patient or O for out-patient: ";
    cin >> patientType;
    
    
    //add code here
    // Input and validate data relevant to in-patients
    
    
    //add code here
    // Input and validate data relevant to all patients
    
    
    //add code here
    // Call correct patientCharges function to return total charge
    
    
    //add code here
    // Display the billing statment
    cout << fixed << showpoint << setprecision(2) << endl << endl;
    cout << "******************************\n";
    if (patientType == 'I')
        cout << "Room charges $" << setw(8) << days*roomRate<<endl;
    if (services > 0.0)
        cout << "Lab & Services $" << setw(8) << services << endl;
    if (medication > 0.0)
        cout << "Medication $" << setw(8) << medication << endl;
    cout << "Total charges $" << setw(8) << totalCharges << endl;
    cout << "******************************\n";
    
    
    return 0;
}

//Calculate function that i have no clue how to do

The coloring of the code immediately alerts you to problems.

Line 9 - doesn't start with purple, so you have forgotten the return type.

Line 31 - cout is in blue, but for is in purple, rather than vice-versa. Almost definitely means you are missing a quotes mark in the previous line.

i have the source code for this. It is a basic only
if you want just leave a message..

i want the source code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.