Answer to Question #324003 in C for Niri

Question #324003

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: i. The number of days spent in the hospital ii. The daily rate iii. Charges for hospital services (lab tests, etc.) iv. Hospital medication charges. If the patient was an out-patient the following data should be entered: i. Charges for hospital services (lab tests, etc.) ii. Hospital medication charges. The program should use two 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. 


1
Expert's answer
2022-04-06T05:07:24-0400
#include <stdio.h>

void input_i(int *num)
{
    float temp;
    scanf("%f", &temp);
    while (temp < 0)
    {
    printf("Input invalid. Plese input not negative number: ");
    scanf("%f", &temp);
    }
    *num = temp;
    getchar();
}

void input_f(float *num)
{
    float temp;
    scanf("%f", &temp);
    getchar();
    while (temp < 0)
    {
    printf("Input invalid. Plese input not negative number: ");
    scanf("%f", &temp);
    getchar();
    }
    *num = temp;
    
}

float in_patient()
{
    float rez, ch_hosp, ch_med, d_rate;
    int num_days;
    printf("Number of days spent in the hospital: ");
    input_i(&num_days);
    printf("Daily rate: ");
    input_f(&d_rate);
    printf("Charges for hospital services: ");
    input_f(&ch_hosp);
    printf("Hospital medication charges: ");
    input_f(&ch_med);
    rez = num_days * d_rate + ch_hosp + ch_med;
    return rez;
}

float out_patient()
{
    float rez, ch_hosp, ch_med;
    printf("Charges for hospital services: ");
    input_f(&ch_hosp);
    printf("Hospital medication charges: ");
    input_f(&ch_med);
    rez = ch_hosp + ch_med;
    return rez;
}

int main() {
      char sel;
    printf("\nPatient was admitted as:\n");
    printf("(1) In-patient\n");
    printf("(2) Out-patient\n");
    printf("\nChoise: ");
    sel = getchar();
    getchar();
    printf("\nEnter:\n");
    switch(sel){
    case '1':
    printf("\nTotal charges: %.2f\n", in_patient());
    break;
    case '2':
    printf("\nTotal charges: %.2f\n", out_patient());
    break;
    default:
    printf("\nInvalid select option\n");
    break;
    }
    getchar();
    
}

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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS