我必须计算应纳税收入,并且由于某种原因,当我输入5000作为收入并为已婚夫妇提交了m时,它会给我一个稍微错误的数字。这里是我的代码:从if循环输出的错误号码
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double calcTax = 0;
double income = 0;
char choice;
do {
//Prompt user for income
cout << "Please enter in your taxable income.\nThis must be a positve number:" << endl;
cin >> income;
while (income < 0)
{
cout << "Please enter a positive value" << endl;
cin >> income;
}
//Ask if user is filing as single or married. Continue until correct input is received
char status;
cout << "Please enter an 'm' if married and filing jointly or an 's' if single and filing alone:" << endl;
cin >> status;
while ((status != 'm') && (status != 's'))
{
cout << "You have pressed an incorrect key. Please enter an 'm' if married and filing jointly or an 's' if single and fiing alone:" << endl;
cin >> status;
}
double sBase1 = 863,
sBase2 = 2588,
sBase3 = 4313,
mBase1 = 1726,
mBase2 = 5176,
mBase3 = 8626;
double sAdd2 = 25,
sAdd3 = 85,
sAdd4 = 181,
mAdd2 = 40,
mAdd3 = 175,
mAdd4 = 390;
double taxRate1 = 0.023,
taxRate2 = 0.033,
taxRate3 = 0.052,
taxRate4 = 0.075;
//Calculate tax if user is single, based on income tier
if (status == 's')
{
if (income <= sBase1)
{
calcTax = income * taxRate1;
}
if (sBase1 < income <= sBase2)
{
calcTax = ((income - sBase1)) * taxRate2 + sAdd2;
}
if (sBase2 < income <= sBase3)
{
calcTax = ((income - sBase2) * taxRate3) + sAdd3;
}
if (sBase3 < income)
{
calcTax = ((income - sBase3) * taxRate4) + sAdd4;
}
}
//Calculate tax if user is married, based on income tier
if (status == 'm')
{
if (income <= mBase1)
{
calcTax = income * taxRate1;
}
if (mBase1 < income <= mBase2)
{
calcTax = ((income - mBase1)) * taxRate2 + mAdd2;
}
if (mBase2 < income <= mBase3)
{
calcTax = ((income - mBase2) * taxRate3) + mAdd3;
}
if (mBase3 < income)
{
calcTax = ((income - mBase3) * taxRate4) + mAdd4;
}
}
//Display user's tax
cout << "$" << fixed << setprecision(2) << calcTax << endl;
//Ask if user would like to calculate another tax
//If so, repeat until done
char response;
cout << "Would you like to perform another tax calculation (y or n)?:" << endl;
cin >> choice;
while (choice != 'n' && choice != 'y')
{
cout << "Please enter y or n" << endl;
cin >> choice;
}
} while (choice == 'y');
return 0;
}
5000结婚应该返回148.04,而是我得到165.85。任何人都可以帮忙指出出了什么问题?
我认为你会从使用IDE与调试器中受益。我建议下载Visual Studio或Eclipse。如果您从未调试过代码,请在您选择的IDE中查找关于如何调试的简短YouTube视频。 – plukich
为什么你不提供一个你应该计算你的代码的公式?这样我们可以将您的公式与您的程序如何计算税金并追踪错误进行比较。 –
plukich,我已经在使用Visual Studio。 – riahtron3000