超载医院项目C ++

时间:2017-03-28 07:55:46

标签: c++

所有初学者和超越大师,以及朋友。我有一个评估用户输入的问题,当你输入正数时代码看起来很完美,当你输入负数时出现问题,它会验证;并要求重新输入值,但是,在用户重新输入新值后,它仍保留旧值,并且循环保持为真(否定)。为什么变量的值不会改变? 我在这里发布我的代码。我感兴趣的是,这与我的大学任务有关。无论如何,我做了很多代码。代码只为你理解我的问题。非常感谢。

/*



 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:
 • 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.
 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.
 */
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <cctype>



using namespace std;

void getJudgeData();
void inHospital();
void outHospital();
void checkIn(double dailyRate, double daysInHospital, double hospitalService, double hospitalMedication);
void checkOut(double hospitalService, double hospitalMedication);
void inQuestion();
void outQuestion();


double daysInHospital;
double dailyRate;
double hospitalService;
double hospitalMedication;
string patientType;
string name;






int main () {
    cout<<"What is your name? \n";
    getline(cin, name);
    cout<<"Are you IN patient or OUT\n (Type \"in\" or \"out\") \n ";
    cin>>patientType;
    patientType = patientType;

        if (patientType =="in") {
        inHospital();

    }
    else if (patientType =="out") {
        outHospital();

    }
    else {
    cout<<"Please run again! Probably you didn't enter in or out correctly in lower case!";
    }



    return 0;


}



void inHospital() {
    inQuestion();

    checkIn(dailyRate, daysInHospital, hospitalService, hospitalMedication);

    cout<<  "Dear Mr "<<name<<endl;
    cout<<  "Your bill is described below         : \n";
    cout<<  "You are the patient type             : "<<patientType<<endl;
    cout<< "Total days you stayed at hospital is  : " << daysInHospital<<endl;
    cout<<"Your daily Rate is                     : $" <<dailyRate<<endl;
    cout<<"Total for Hospital service is         : $"<<hospitalService<<endl;
    cout<<"Total for Hospital Medication is      : $"<<hospitalMedication<<endl;
    cout<<"Grand total is                         : "<< (daysInHospital*dailyRate+hospitalMedication+hospitalService) <<endl;

}

void outHospital() {

    outQuestion();

    checkOut(hospitalService, hospitalMedication);

    cout<<"Dear Mr "<<name<<endl;
    cout<<"Your bill is described below      \t: \n";
    cout<<"You are the patient type          \t: "<<patientType<<endl;
    cout<<"Total for Hospital service is       \t: $"<<hospitalService<<endl;
    cout<<"Total for Hospital Medication is    \t: $"<<hospitalMedication<<endl;
    cout<<"Grand total is                      \t: $"<< (hospitalMedication+hospitalService) <<endl;

}



// ************ CHECH or VALIDATION PART************

void checkIn(double dailyRate, double daysInHospital, double hospitalService, double hospitalMedication) {

    while (dailyRate<0 || daysInHospital<0 || hospitalService<0 || hospitalMedication<0) {
        cout<< "Please re-enter the values they must not be less than 0 \n";
        inQuestion();

    }
}



void checkOut(double hospitalService, double hospitalMedication) {

    while (hospitalService<0 || hospitalMedication<0) {
        outQuestion();
        cout<<hospitalMedication<<endl;
        cout<<hospitalService<<endl;

    }

}

// ********* QUESTIONS FOR ASK
void inQuestion() {

    cout<< "How many days spent in hospital? "<<endl;
    cin>> daysInHospital;

    cout<< "What is the daily rate for IN Patient \n";
    cin>>dailyRate;

    cout<<"What is amount of charges for hospital services?  \n";
    cin>>hospitalService;

    cout<<"Hospital medication charges?  \n";
    cin>>hospitalMedication;
}

void outQuestion() {
    cout<<"What is amount of charges for hospital services?  \n";
    cin>>hospitalService;

    cout<<"Hospital medication charges?  \n";
    cin>>hospitalMedication;
}

2 个答案:

答案 0 :(得分:0)

您有一些全局变量,并将它们作为参数传递。传递变量的函数,用参数隐藏变量。您的全局变量已更改,但您仍在检查参数。由于你的函数无论如何都会看到全局变量,所以不要将它们作为参数传递:

/*



 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:
 • 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.
 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.
 */
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <cctype>



using namespace std;

void getJudgeData();
void inHospital();
void outHospital();
void checkIn();
void checkOut();
void inQuestion();
void outQuestion();


double daysInHospital;
double dailyRate;
double hospitalService;
double hospitalMedication;
string patientType;
string name;






int main () {
    cout<<"What is your name? \n";
    getline(cin, name);
    cout<<"Are you IN patient or OUT\n (Type \"in\" or \"out\") \n ";
    cin>>patientType;
    patientType = patientType;

        if (patientType =="in") {
        inHospital();

    }
    else if (patientType =="out") {
        outHospital();

    }
    else {
    cout<<"Please run again! Probably you didn't enter in or out correctly in lower case!";
    }



    return 0;


}



void inHospital() {
    inQuestion();

    checkIn();

    cout<<  "Dear Mr "<<name<<endl;
    cout<<  "Your bill is described below         : \n";
    cout<<  "You are the patient type             : "<<patientType<<endl;
    cout<< "Total days you stayed at hospital is  : " << daysInHospital<<endl;
    cout<<"Your daily Rate is                     : $" <<dailyRate<<endl;
    cout<<"Total for Hospital service is         : $"<<hospitalService<<endl;
    cout<<"Total for Hospital Medication is      : $"<<hospitalMedication<<endl;
    cout<<"Grand total is                         : "<< (daysInHospital*dailyRate+hospitalMedication+hospitalService) <<endl;

}

void outHospital() {

    outQuestion();

    checkOut();

    cout<<"Dear Mr "<<name<<endl;
    cout<<"Your bill is described below      \t: \n";
    cout<<"You are the patient type          \t: "<<patientType<<endl;
    cout<<"Total for Hospital service is       \t: $"<<hospitalService<<endl;
    cout<<"Total for Hospital Medication is    \t: $"<<hospitalMedication<<endl;
    cout<<"Grand total is                      \t: $"<< (hospitalMedication+hospitalService) <<endl;

}



// ************ CHECH or VALIDATION PART************

void checkIn() {

    while (dailyRate<0 || daysInHospital<0 || hospitalService<0 || hospitalMedication<0) {
        cout<< "Please re-enter the values they must not be less than 0 \n";
        inQuestion();

    }
}



void checkOut() {

    while (hospitalService<0 || hospitalMedication<0) {
        outQuestion();
        cout<<hospitalMedication<<endl;
        cout<<hospitalService<<endl;

    }

}

// ********* QUESTIONS FOR ASK
void inQuestion() {

    cout<< "How many days spent in hospital? "<<endl;
    cin>> daysInHospital;

    cout<< "What is the daily rate for IN Patient \n";
    cin>>dailyRate;

    cout<<"What is amount of charges for hospital services?  \n";
    cin>>hospitalService;

    cout<<"Hospital medication charges?  \n";
    cin>>hospitalMedication;
}

void outQuestion() {
    cout<<"What is amount of charges for hospital services?  \n";
    cin>>hospitalService;

    cout<<"Hospital medication charges?  \n";
    cin>>hospitalMedication;
}

答案 1 :(得分:0)

我也有这个实验室。我的Visual Studio 2017和全局变量中的Mine函数完全不需要。

#include <iostream>
using namespace std;

void inpatient(bool&, int&, double&, double&, double&, double&); 
void outpatient(bool&, double&, double&, double&);

int main()
{   
    bool patient; //if bool is true, the patient in an inpatient. If false, the patient is an outpatient
    int days; //days spent in hospital
    double rate, //daily rate for inpatient
           medCharge_IP, //inpatient medication charges
           hsCharge_IP, //inpatient hospital services charges
           medCharge_OP, //outpatient medication charges
           hsCharge_OP, // outpatient hospital services charges
           totalCharge; //total charges of hospital stay

    cout << "If the patient admitted was an in - patient, please enter 1. If the patient was an out - patient, please enter 0" << endl; 
    cin >> patient;

    if (patient == true) //If the boolean variable is true, the program will see the patient as an inpatient
    {   
        inpatient(patient, days, rate, medCharge_IP, hsCharge_IP, totalCharge); //Returns the variable data
        cout << "The total charges for the hospital visit accumulate to " << totalCharge << " dollars." << endl;
    }

    if (patient == false) //If the boolean variable is false, the program will see the patient as an outpatient
    {
        outpatient(patient, medCharge_OP, hsCharge_OP, totalCharge); //Returns the variable data
        cout << "The total charges for the hospital visit accumulate to " << totalCharge << " dollars." << endl;
    }

    system("PAUSE"); //This is needed because on some computers, the program will finish too fast
    return 0;
}

void inpatient(bool& patient, int& days, double& rate, double& medCharge_IP, double& hsCharge_IP, double& totalCharge)
{
    cout << "How many days did the patient spend in the hospital ?" << endl;
    cin >> days;
    if (days < 0) //A validation check is required in the program requirements 
    {   cout << "How many days did the patient spend in the hospital ? (Only positive numbers allowed)" << endl;
        cin >> days;    }

    cout << "What is the daily rate for an inpatient stay ?" << endl;
    cin >> rate;
    if (rate < 0) //A validation check is required in the program requirements
    {   cout << "What is the daily rate for an inpatient stay ? (Only positive numbers allowed)" << endl;
        cin >> rate;    }

    cout << "What were the medication charges for the stay ?" << endl;
    cin >> medCharge_IP;
    if (medCharge_IP < 0) //A validation check is required in the program requirements
    {   cout << "What were the medication charges for the stay ? (Only positive numbers allowed)" << endl;
        cin >> medCharge_IP;    }

    cout << "What were the charges for hospital services ?" << endl;
    cin >> hsCharge_IP;
    if (hsCharge_IP < 0) //A validation check is required in the program requirements
    {   cout << "What were the charges for hospital services ? (Only positive numbers allowed)" << endl;
        cin >> hsCharge_IP;     }

    totalCharge = (days*rate) + hsCharge_IP + medCharge_IP; //calculating the total charges for an inpatient stay
}

void outpatient(bool& patient, double& medCharge_OP, double& hsCharge_OP, double& totalCharge)
{
    cout << "What were the medication charges for the stay ?" << endl;
    cin >> medCharge_OP;
    if (medCharge_OP < 0) //A validation check is required in the program requirements
    {   cout << "What were the medication charges for the stay ? (Only positive numbers allowed)" << endl;
        cin >> medCharge_OP;    }

    cout << "What were the charges for hospital services ?" << endl;
    cin >> hsCharge_OP;
    if (hsCharge_OP < 0) //A validation check is required in the program requirements
    {   cout << "What were the charges for hospital services ? (Only positive numbers allowed)" << endl;
        cin >> hsCharge_OP;     }

    totalCharge = hsCharge_OP + medCharge_OP; //calculating the total charges for an outpatient stay

}