Question

In: Computer Science

C++ please show the error mesg if user enter others letter insated of I and O...

C++ please show the error mesg if user enter others letter insated of I and O and also numbers must be greater than 0 if user input -, show the error mesg untill user out the correct...thanks

please Overloaded Hospital
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. Please keep asking the user until the user enters the valid choice. Please refer to the test cases for more details.

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. Keep validating the data until the user enters valid value.


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.

my code

#include <iostream>
#include <iomanip>
using namespace std;

void In_Patient_Total(int days, double rate,double service, double medication);
void Out_Patient_Total(double service,double medication);

int main()
{
   char Choise;
   int days;
   double rate, service, medication;
   cout << "Do you want to calculate the total hospital bill for \n(I)an In - Patient, or \n(O)an Out - Patient ? ";
   cin >> Choise;
   while (Choise != 'I' && Choise != 'i' && Choise != 'o' && Choise != 'O')
   {
       cout << "Enter I or O: ";
       cin >> Choise;
   }
    if (Choise == 'I' || Choise == 'i') {
        cout << "How many days were spent in the hospital? \n";
        cin >> days;
            while (days < 0);
            cout << "Number must be a positive number : ";
            cin >> days;
        cout << "What is the hospital daily rate? $\n";
        cin >> rate;
            while (rate < 0);
            cout << "Number must be a positive number : ";
            cin >> rate;
        cout << "What were the charges for hospital services (lab tests, etc.)? $\n";
        cin >> service;
            while (service < 0);
            cout << "Number must be a positive number : ";
            cin >> service;
        cout << "What were the hospital medication charges? $\n";
        cin >> medication;
            while (medication < 0);
            cout << "Number must be a positive number : ";
            cin >> days;
            In_Patient_Total(int days, double rate, double service, double medication);
    }
    if (Choise == 'O' || Choise == 'o') {
        cout << "What were the charges for hospital services (lab tests, etc.)? $\n";
        cin >> medication;
            while (medication < 0);
            cout << "Number must be a positive number : ";
            cin >> days;
        cout << "What were the hospital medication charges? $\n";
        cin >> service;
            while (service < 0);
            cout << "Number must be a positive number : ";
            cin >> service;
    }
        }
void In_Patient_Total(int days, double rate, double service, double medication)
{
    return (days * rate) + (service, medication);
}

void Out_Patient_Total(double service, double medication) {
    return (service + medication);
}

Solutions

Expert Solution

Made some changes to your code but works alright.

#include <iostream>
#include <iomanip>
using namespace std;
double calcData(int, double, double, double); //changed your declarations to a simple form
double calcData(double, double);
int main()
{
        char   InOut;
        int    NumOfDays;
        double DailyRate,
                   ServiceCharges,
                   MedicationCharges,
                   TotalCharges;
        cout << "This program computes and displays a patient’s hospital charges\n" //all regular information displayed to the user
             << "Was patient admitted as an in-patient or an out-patient?\n"
             << "Enter (I) for in-patient\nEnter (O) for out-patient\n";
        cin  >> InOut; //changed Choise to InOut
        switch(InOut) //For better use if conditions are replaced to switch condition 
        {
                case 'i' : //whether the input is i or I
                case 'I' :  do
                                        {
                                                cout << "How many days were spent in the hospital? ";
                                                cin  >> NumOfDays;

                                                if (NumOfDays < 0) //minimum value should be greater than zero
                                                {
                                                        cout << "Number of days must be greater than 0.\n";
                                                }

                                        } while(NumOfDays < 0);

                                        do
                                        {
                                                cout << "What was the daily rate? ";
                                                cin  >> DailyRate; //entering the daily rate of hospital daily

                                                if (DailyRate < 0)
                                                {
                                                        cout << "Daily rate must be greater than 0.\n";
                                                }

                                        } while(DailyRate < 0);
                                        

                case 'o' : //for case 'O' or 'o'
                case 'O' :  do
                                    {
                                                cout << "Enter the charges for hospital services "
                                                 << "(lab tests, etc.): ";
                                                cin  >> ServiceCharges;

                                                if (ServiceCharges < 0)
                                                {
                                                        cout << "Charges for hospital services must be "
                                                                        "greater than 0.\n";
                                                }

                                    } while (ServiceCharges < 0);
                
                                    do
                                    {
                                        cout << "Enter the hospital medication charges: ";
                                        cin  >> MedicationCharges;

                                        if (MedicationCharges < 0)
                                        {
                                                cout << "Hospital medication charges must be greater"
                                                         << " than 0.\n";
                                        }

                                    } while(MedicationCharges < 0);

        }

        cout << "\n        Patient hospital stay report\n";
        cout << right << fixed << showpoint << setprecision(2);

        switch(InOut)
        {
                case 'i' :
                case 'I' : TotalCharges = calcData(NumOfDays, DailyRate, ServiceCharges, MedicationCharges);
                                   cout << "Number of days spent in the hospital : " 
                                        << setw(11) << NumOfDays << endl;
                                   cout << "Daily rate                           : $"
                                        << setw(10) << DailyRate << endl;
                                   break;
                case 'o' :
                case 'O' : TotalCharges = calcData(ServiceCharges, MedicationCharges);
                
        }

        cout << "Charges for hospital services        : $" 
             << setw(10) << ServiceCharges << endl;
        cout << "Hospital medication charges          : $" 
             << setw(10) << MedicationCharges << endl;
        cout << "Total charges                        : $" 
             << setw(10) << TotalCharges << endl;
        
}
// calculation of total charges for the given user input
double calcData(int NumOfDays, double DailyRate, double ServiceCharges,
                                double MedicationCharges)
{
        return (NumOfDays * DailyRate) + calcData(ServiceCharges, MedicationCharges);
}

double calcData(double ServiceCharges, double MedicationCharges)
{
        return ServiceCharges + MedicationCharges;
}

Related Solutions

Create in C++ Prompt the user to enter a 3-letter abbreviation or a day of the...
Create in C++ Prompt the user to enter a 3-letter abbreviation or a day of the week and display the full name of the day of the week. Use an enumerated data type to solve this problem. Enumerate the days of the week in a data type. Start with Monday and end with Friday. Set all of the characters of the user input to lower case. Set an enumerated value based on the user input. Create a function that displays...
For a game that takes user input of certain letters, Upper: O,C,I,Z; Lower: o,c,i,z. The board...
For a game that takes user input of certain letters, Upper: O,C,I,Z; Lower: o,c,i,z. The board is a 4 by 4 matrix and the numbers on the right are the positon that correspond with the dots. So if player 2 statisfies the requirement for the winning condition. Winning conditions being 4 letters in a row of (all upper case-- Z I C O ), (all straight-line-- Z I i I), (all consonants - Z z C c), (all curved-- C...
using this code under, I want when the user input only letter q an error message...
using this code under, I want when the user input only letter q an error message appears that there is no second smallest, how I can do that? #include <iostream> #include <sstream> #include <vector> #include <algorithm> using namespace std; int secondSmallestNumber(vector<int> &I); int main() {    cout << "Enter the numbers in random order: (close by entering q)" << endl;    vector<int> I;    int input;    stringstream ss;    string str;    bool flag = false;    while (!flag) {        getline(cin, str);        ss.clear();        ss << str;...
When I run this C++ program that asks the user to enter the population of 4...
When I run this C++ program that asks the user to enter the population of 4 cities and produce the bar graph - it runs but ends with a Debug Error - run time check failure 2 stack around variable population was corrupted - any help would be appreciated #include <iostream> using namespace std; int main() { int population[4],k;    int i=1,n=5;       do    {        cout<<"Enter the population of city "<<i<<":"<<endl;             cin>>population[i];        if(population[i]<0)       ...
Write in C# please! Ask the user to enter all of their exam grades. Once they...
Write in C# please! Ask the user to enter all of their exam grades. Once they are done, calculate the minimum score, the maximum score and the average score for all of their scores. Perform this using at least 2 Loops (can be the same type of loop) and not any built in functions.
C code please (1) Prompt the user to enter a string of their choosing. Store the...
C code please (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle flights and...
Please code C# 8. Write a program that prompts the user to enter an integer. The...
Please code C# 8. Write a program that prompts the user to enter an integer. The program then determines and displays the following: Whether the integer is divisible by 5 and 6 Whether the integer is divisible by 5 or 6
I have to create a program in C++ where a user can enter as many numbers...
I have to create a program in C++ where a user can enter as many numbers as they want (they predetermine the number of values to be inputted) and then the program can echo that input back to the user and then determine if the numbers were even, odd, or a zero and it outputs how many of each were found. This is to be down with four void functions and no arrays. The program initializes the variables zero, odds,...
C++ OOP Provide a user-friendly way to enter expressions to be evaluated. Please add a comment...
C++ OOP Provide a user-friendly way to enter expressions to be evaluated. Please add a comment explaining each step of your program. Make a program to evaluate infix arithmetic expressions containing integer operands and the operators + (addition), - (subtraction), * (multiplication), / (division) and pairs of parentheses, properly nested. Use the following two-stack algorithm (by E. W. Dijkstra): If the next token in the expression is an integer, push the integer onto the value stack. If the next token...
Assignment in C: prompt the user to enter secret message that is terminated by presding Enter....
Assignment in C: prompt the user to enter secret message that is terminated by presding Enter. You can assume that the the length of this message will be less than 100 characters. You will then parae this message, character by character, converting them to lower case, and find corresponding characters in the words found in the key text word array. Once a character match is found, you will write the index of the word and the index of the character...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT