我得到的錯誤未初始化的局部變量calculateTax使用,我試圖用int來定義它,它不工作,我擔心,如果我添加像int calculateTax = 0它將覆蓋函數我爲變量創建。未初始化的本地變量calculateTax使用
我是一個總的小白,當涉及到編程,所以我希望有一些明顯的我失蹤,任何幫助將非常感激
這裏是代碼:
//Gross pay calculation
//additional tax rate calculation
#include <iostream>
#include <iomanip>
using namespace std;
//Setting global contants
const double PAY_RATE = 22.55;
const double BASE_HOURS = 40.0;
const double OT_MULTIPLIER = 1.5;
//Prototype functions
double getBasePay (double);
double getOvertimePay(double);
double getCalculateTax(double);
int taxRate;
int main()
{
double hours,
basePay,
overtime = 0.0,
taxRate,
taxOwed,
calculateTax,
totalPay;
//Input hours worked
cout << "How many hours did you work? ";
cin >> hours;
//Input Tax rate
cout << "What is the percent of your tax rate? ";
cin >> taxRate;
//get base pay
basePay = getBasePay(hours);
//Get OT if applicable
if (hours > BASE_HOURS)
overtime = getOvertimePay(hours);
//calculate total pay
totalPay = basePay + overtime;
//calculate Tax rate
taxOwed = calculateTax;
// Setting output format
cout << setprecision(2) << fixed << showpoint;
//Display calculated pay
cout << "Base pay: $" << basePay << endl
<< "Overtime pay: $" << overtime << endl
<< "Total pay: $" << totalPay << endl
<< "Taxes owed: $" << taxOwed << endl;
//Adding Pause before creating functions
char c;
cout << "Press any key to end program: ";
cin >> c;
return 0;
}
//#############################################
// Get base pay function accepts hours worked #
// and returns pay for non OT hours #
//#############################################
double getBasePay(double hoursWorked)
{
double basePay;
// determine base pay
if (hoursWorked > BASE_HOURS)
basePay = BASE_HOURS * PAY_RATE;
else
basePay = hoursWorked * PAY_RATE;
return basePay;
}
//##############################################
// The get overtime function accepts hours #
//then returns the OT pay if applicable #
//##############################################
double getOvertimePay(double hoursWorked)
{
double overtimePay;
//Determine OT pay
if (hoursWorked > BASE_HOURS)
{
overtimePay = (hoursWorked - BASE_HOURS) *
PAY_RATE * OT_MULTIPLIER;
}
else
overtimePay = 0.0;
return overtimePay;
}
//##########################################
//this taxes function calculates tax owed #
// based on the total pay regardless of OT #
//##########################################
double getCalculateTax(double totalPay)
{
double calculateTax;
calculateTax = (taxRate/100) * totalPay;
return calculateTax;
}
'主()'定義了'雙calculateTax'(即未初始化),並沒有按'taxOwed = calculateTax'前分配給它。這是*未定義的行爲*。 'main()'的變量和'getCalculateTax'中的同名變量之間沒有關係。從'main()'頂部的變量列表中刪除'calculateTax',一旦你知道'totalPay',你可以說'double calculateTax = getCalculateTax(totalPay);'所以它立即被初始化。 –