我不確定爲什麼CHARGE_1被評估爲.80,無論我輸入它的數量是1還是19.其他if語句也是如此。我不斷得到奇怪的數字,如.80和.64作爲輸出。請幫我弄清楚這有什麼問題。在C++中的簡單If語句練習中的邏輯錯誤
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int MONTHLY_FEE = 10;
const double CHARGE_1 = .10;
const double CHARGE_2 = .08;
const double CHARGE_3 = .06;
const double CHARGE_4 = .04;
int numChecks;
double charge1Total = numChecks * CHARGE_1;
double charge2Total = numChecks * CHARGE_2;
double charge3Total = numChecks * CHARGE_3;
double charge4Total = numChecks * CHARGE_4;
cout << "Please enter how many checks you have used.\n";
cout << "and I will tell you how much your charge is. ";
cin >> numChecks;
if((numChecks > 0) && (numChecks < 20))
{
cout << "Your total is $" << fixed << setprecision(2) <<
(charge1Total + MONTHLY_FEE) << ".";
}
else if ((numChecks >= 20) && (numChecks <= 39))
{
cout << "Your total is $" << fixed << setprecision(2) <<
(charge2Total + MONTHLY_FEE) << ".";
}
else if ((numChecks >= 40) && (numChecks <= 59))
{
cout << "Your total is $" << fixed << setprecision(2) <<
(charge3Total + MONTHLY_FEE) << ".";
}
else if (numChecks >=60)
{
cout << "Your total is $" << fixed << setprecision(2) <<
(charge4Total + MONTHLY_FEE) << ".";
}
}
你有檢查號碼之前計算你的總數。把它放在** cin >> numChecks; **行後面。 –