gpt4 book ai didi

c++ - 如何调用一个函数并让它运行并返回一个字符?

转载 作者:太空狗 更新时间:2023-10-29 23:10:46 25 4
gpt4 key购买 nike

在此代码中,它不运行我的第一个函数 getPatientType。此代码直接跳到“医疗费用是多少”,然后询问服务费并返回 0,无论我输入什么。我正在寻求一些帮助来正确启动和运行此代码。

#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()
{

我不确定这是否是调用此函数的正确方法,以及它为什么不运行。

    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;

}

我也是利用了C++可以重载的能力,所以调用这两个函数同名不同参。

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

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

系统没有显示我有任何错误,所以我很困惑为什么整个代码不能正确运行。如果有任何遗漏或逻辑错误,请提供帮助。

最佳答案

调用函数的正确方法是调用:char patientType = getPatientType();int main() 中。您的函数签名应该是 char getPatientType() { ... }。您没有为函数提供任何参数。用户在函数内部提供参数。您需要将此函数放在文件的顶部或在顶部声明它。您可以通过将 char getPatientType(); 放在顶部来声明它。这告诉你的程序,相信我,我会在我的程序结束时告诉你这个函数做了什么。在您的函数中,您需要定义一个 char patientType;。这个变量是你放cin的内容的地方。

你真的应该考虑给你的两个函数起不同的名字。他们计算相同的东西但需要不同的值。您将很难记住需要提供的内容,并且程序越大,就会变得越乱。例如calcBillOverDays。您也可以分离功能。 calcServiceCharge() 和 calcDailyCharge()

关于c++ - 如何调用一个函数并让它运行并返回一个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54918930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com