3

In this code it does not run my first function getPatientType. This code skips directly to the, "what are the medical charges and then asks for the service charges and returns 0 no matter what I enter. I am looking for some assistance in getting this code up and running correctly.

#include <iostream>
#include <iomanip>
using namespace std;
int numdays;
float dailyRate;
double medChrg;
double totalChrg;
char doAgain;
char patientType;
int daysHospital;
double serviceChrg;



int main()
{

I'm not sure if this is the correct way to call this function, and why it does not run.

    char getPatientType(char);

    if (patientType == 'y' || patientType == 'Y') 
    {
        cout << "How many days was the patient in the hospital? ";
        cin >> daysHospital;
        while (daysHospital < 0) {
            cout << "Enter a valid number of days: ";
            cin >> daysHospital;
        }

        cout << "What is the daily rate? : ";
        cin >> dailyRate;
        while (dailyRate < 0)
        {
            cout << "Enter a valid daily Rate. : ";
            cin >> dailyRate;

        }
    }

    cout << "What are the medical charges?: ";
    cin >> medChrg;
    while (medChrg < 0) {
        cout << "Enter a valid medical charge : ";
        cin >> medChrg;
    }
    cout << "What are the services charges?: ";
    cin >> serviceChrg;
    while (medChrg < 0) {
        cout << "Enter a valid medical charge : ";
        cin >> medChrg;
    }

    double calcBillFor(int, float, double, double);
    double calcBillFor(double, double);

    cout << "The charges for the patient will be " << totalChrg;
    system("pause");

}


char getPatientType(char) {

    cout << "What is the type of Patient, type I for Impatient, and O for 
Outpatient?: ";
    cin >> patientType;
    if (patientType != 'I' || patientType != 'i' || patientType != 'o' || 
patientType != 'O')
    {
        cout << "Enter a valid patient type: ";
        cin >> patientType;

    }
    return patientType;

}

I am also supposed to utilize the ability of C ++ being able to overload, so these two functions are called with the same name but different parameters.

double calcBillFor(int, float, double, double) {
    totalChrg = (dailyRate * daysHospital) + medChrg + serviceChrg;
    return totalChrg;
}

double calcBillFor(double, double) {
    totalChrg = (medChrg + serviceChrg);
    return totalChrg;
}

The system does not show I have any errors so I am confused as to why the entire code does not run correctly. If there is anything I am missing or logical errors please help.

AmbieGirl
  • 119
  • 1
  • 7
  • 4
    Define the variables in the functions where they are used. Define the arguments (with *names*). And *call* the functions (which you don't do now). All in all it seems you need to take a few steps back, go back to whatever book, tutorial or class you're using (or get [a couple of new good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282)) and study more about functions. – Some programmer dude Feb 28 '19 at 05:29
  • This code has a lot of globals and because of that a whole lot of confusion. The key to effective C++ is keeping variable scope as narrow as possible, that is, declare them close to where they're used and define formal methods for sharing, like returning from a function or passing in as an argument. – tadman Feb 28 '19 at 05:51

4 Answers4

0

Its not giving error because the function getPatientType() is defined after the main , might be for some other work, ( might be used inside other functions which are defined after getPatientType() function... )

Its not giving error also because of this "char getPatientType(char); " . means you are declaring or protyping a function . And defined it after some time.

in order to solve your problem

declare getPatientType() function outside of main body.. , but when you are calling this function don't forget to pass parameter of char type as per function parameter data type.

or

define the getPatientType() function inside the main body.

sha111
  • 113
  • 1
  • 12
0

The correct way to call the function is to call: char patientType = getPatientType(); inside int main(). Your function signature should be char getPatientType() { ... }. You aren’t providing any parameters to the function. The user is providing parameters inside the function. You need to put this function at the top of your file OR declare it at the top. You can declare it by putting char getPatientType(); at the top. This tells your program, trust me I’m going to tell you what this function does by the end of my program. Inside your function you need to define a char patientType;. This variable is a place for you to put the content of cin.

You should really consider calling your two functions different names. They calculate the same thing but require different values. It will be hard for you to remember what you need to provide and will get messier the bigger your program gets. e.g. calcBillOverDays. You could also separate the functionality. calcServiceCharge() and calcDailyCharge()

0

This:

int main()
{
    char getPatientType(char);

    if (patientType == 'y' || patientType == 'Y') 

Is not an invocation of the getPatientType function, it's a forward declaration of the function inside the scope of main.

Also, since getPatientType does not actually take any input parameters, the (char) part is not needed

Better:

char getPatientType(); // forward declaration

int main()
{

    patientType = getPatientType();  // actually call the function

    if (patientType == 'y' || patientType == 'Y') 

Actual getPatientType function defined as follows:

char getPatientType() {

    cout << "What is the type of Patient, type I for Impatient, and O for 
Outpatient?: ";
     <rest of code not shown for brevity>
}
selbie
  • 100,020
  • 15
  • 103
  • 173
0

Okay, where to begin. There is a lot wrong with this code. First off, you never want to use global variables (variables which aren't inside of a function or class) unless there is no other way to program it which is extremely rare. Delete them from the global scope completely.

// delete all of these...
int numdays;
float dailyRate;
double medChrg;
double totalChrg;
char doAgain;
char patientType;
int daysHospital;
double serviceChrg;

Instead, make them be parameters for your functions. For example, this is how you would write them as parameters:

double calcBillFor(int daysHospital, float dailyRate, double medChrg, double serviceChrg)
{
    ...
}

You can see how this still allows the function to work, but the only thing that has access to the variables is the function itself. We don't want everything to have access to this data. I will allow you to fix the other calcBillFor function with the appropriate variables (make sure they are deleted from the global scope once you have made them parameters).

The first line in main() is char getPatientType(char);. That is not an appropriate way to call a function. That is a declaration and it's being declared inside of main() which is illogical. The purpose of this declaration is to say "hey there's a function somewhere called getPatientType and it takes a char as its parameter, and it returns a char, but I don't know anything about it other than that."

The reason that a declaration is used in your code is because the actual function getPatientType is below main(). Because it is below main(), main() cannot know it even exists without a function declaration. That's why you would do something like this:

char getPatientType(char);
/*   "Hey, compiler! Somewhere there's a function called getPatientType
 *    that takes a char and returns a char, but that's all I know about
 *    it right now."
 */
int main() { ... }

Thus you see how function declarations are outside of and above the main function. That makes it so that when you actually call getPatientType inside of main, the compiler won't yell at you because it has never even heard of getPatientType before. That is because you told it to expect getPatientType to be later down in the code, so it stays happy.

However, if we look at getPatientType later down in the code, we quickly learn that it doesn't even need a char passed to it! It could have been written like this instead:

char getPatientType() {
    char patientType;
    cout << "What is the type of Patient, type I for Impatient, and O for Outpatient?: ";
    cin >> patientType;
    if (patientType != 'I' || patientType != 'i' || patientType != 'o' || 
        patientType != 'O')
    {
        cout << "Enter a valid patient type: ";
        cin >> patientType;
    }
    return patientType;
}

Since we changed how getPatientType looks we also need to change that declaration we made earlier above main().

char getPatientType();
/*   "Hey, compiler! Somewhere there's a function called getPatientType
 *    that takes NOTHING and returns a char, but that's all I know about
 *    it right now."
 */
int main() { ... }

This is how you would call such a function:

char patientType = getPatientType();
// patientType now stores our input

The rest is up to you. I hope this cleared some things up for you.