我創建了一個函數來確定cableBoxFeesTotal
的值,然後我將它返回給本地變量cableBoxFeesTotal
。當我嘗試在函數外部使用cableBoxFeesTotal
的值時,值爲0.00,而不是函數返回的值。我一直在爲此工作好幾天,似乎無法弄清楚我做錯了什麼。請告訴我如何解決這個問題。謝謝!我的函數不會將值返回給C++中的本地變量
我不想發佈整個代碼..這是作業,但我沒有添加下面的兩個部分。
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
#include <cmath>
using namespace std;
const double BASIC_CABLE = 43.00;
const double PREMIUM_CABLE = 58.00;
const double BASIC_BOX = 7.50;
const double PREMIUM_BOX = 11.25;
const double PREMIUM_CHANNEL_FEE = 9.99;
const double EMPLOYEE_DISCOUNT_AMOUNT = .14;
double feeFunction (int cableBoxes, double serviceLevel, double cableBoxFeesTotal, double BASIC_BOX, double PREMIUM_BOX, char B, char P)
{
if (cableBoxes > 2 && serviceLevel == 'B')
{
cableBoxFeesTotal = ((cableBoxes - 2) * BASIC_BOX);
cout << "Cable Box Fees: " << setw(15) << cableBoxFeesTotal << endl;
}
else if (cableBoxes > 2 && serviceLevel == 'P')
{
cableBoxFeesTotal = ((cableBoxes - 2) * PREMIUM_BOX);
cout << "Cable Box Fees: " << setw(15) << cableBoxFeesTotal << endl;
}
return (cableBoxFeesTotal);
}
int main()
{
string employee;
bool hasPremium;
char H;
char B;
char P;
char premiumChannel;
char serviceLevel;
int cableBoxes = 0 ;
double customerID;
double monthlyRateTotal = 0.00;
double cableBoxFeesTotal = 0.00;
double premiumChannelTotal = 0.00;
double subtotal = 0.00;`enter code here`
double employeeDiscount = 0.00;
double total = 0.00;
feeFunction (cableBoxes, serviceLevel, cableBoxFeesTotal, BASIC_BOX, PREMIUM_BOX, B, P);
cout << cableBoxFeesTotal << endl;
if (hasPremium == true)
{
subtotal = (cableBoxFeesTotal + monthlyRateTotal + PREMIUM_CHANNEL_FEE);
cout << "Subtotal:" << setw(15) << subtotal << endl;
}
else if (hasPremium == false)
{
subtotal = (monthlyRateTotal + cableBoxFeesTotal);
cout << "Subtotal:" << setw(15) << subtotal << endl;
}
}
您的代碼段似乎損壞了...... – 2011-05-29 14:10:26
顯然,您在代碼中缺少對函數「feeFunction」的函數調用。 – cpx 2011-05-29 14:15:18
這是功課嗎?如果是這樣,請標記爲這樣。 – 2011-05-29 14:17:00