C++确定税收的程序

C++ program to determine taxes

本文关键字:程序 C++      更新时间:2023-10-16

所以代码应该要求用户输入他们的收入,无论是联合还是单独申报,输出总所得税和所得税,然后询问用户是否要再次提交或退出。但是由于某种原因,它会迫使您在继续之前输入每个输入两次。我知道我可能错过了一些简单的代码问题,但我无法弄清楚。使用Visual Studio,请帮忙!

int main()
{
double income = 0;
double taxRate = 0;
double add = 0;
double subtract = 0;
double incomeTax = 0;
string taxStatus = "";
string file = "";
string answer = "";
top:
cout << "nPlease enter your income to calculate your taxesn " << endl;
cin >> income;
while (!(cin >> income)) //get input
{
if (isdigit(income))
{
break;
}
else
{
//if input fails, run this
cin.clear();
cin.sync();
cout << "nSorry, that was not a valid number. Please enter a valid numbern ";
}
}
cin.ignore(80, 'n');
cout << "nIf you will be filing singly, enter 's'. If you will be filing jointly, enter 'm'n ";
cin >> file;
while (!(cin >> file)) //get input
{
if (file == "s" || "m")
{
cin.ignore(80, 'n');
break;
}
else
{
//if input fails, run this
cin.clear();
cin.sync();
cout << "nThat is not a correct input, please enter s/mn ";
}
}
if (file == "s")
{
if (income <= 863)
{
taxRate = .022;
subtract = 0;
add = 0;
}
if (income >= 864 & income <= 2588)
{
taxRate = .033;
subtract = 863;
add = 25;
}
if (income >= 2589 & income <= 4313)
{
taxRate = .062;
subtract = 2588;
add = 85;
}
if (income > 4313)
{
taxRate = .075;
subtract = 4313;
add = 181;
}
}
if (file == "m")
{
if (income <= 1726)
{
taxRate = .022;
subtract = 0;
add = 0;
}
if (income >= 1727 & income <= 5176)
{
taxRate = .033;
subtract = 1726;
add = 40;
}
if (income >= 5177 & income <= 8626)
{
taxRate = .062;
subtract = 5176;
add = 175;
}
if (income > 8626)
{
taxRate = .075;
subtract = 8626;
add = 390;
}
}
incomeTax = ((income - subtract) * taxRate) + add;
cout << "nYour taxable income is " << income << endl;
if (file == "s")
{
taxStatus = "Singly";
}
if (file == "m")
{
taxStatus = "Jointly";
}
cout << "nand you are filing " << taxStatus << endl;
cout << "nThat means your income tax will be " << incomeTax << endl;
cout << "nWould you like to conduct another operation, y/n?n ";
cin >> answer;
while (!(cin >> answer)) //get input
{
if (answer == "y")
{
goto top;
}
if (answer == "n")
{
break;
}
else
{
//if input fails, run this
cin.clear();
cin.sync();
cout << "nThat is not a correct input, please enter y/n. ";
}
}
cin.ignore(80, 'n');
system("PAUSE");
return 0;
}

所以,我让它停止两次要求输入,所以感谢您的帮助。我仍然遇到以美元格式输出所得税和所得税的问题,并在最后选择"y"时返回程序的开始。有什么建议吗?

int main()
{
double income = 0; //double stores income input
double taxRate = 0;//double stores tax rate
double add = 0;//double stores value added
double subtract = 0;//double stores value to be subtracted
double incomeTax = 0;//double stores final income tax
string taxStatus = "";//string stores status of single or joint
string file = "";//string stores values of s or m
string answer = "";//string stores values of y or n
top:
//asks user to input income value
cout << "nPlease enter your income to calculate your taxesn " << endl;
while (!(cin >> income)) //get input
{
if (isdigit(income) & income > 0)//if value is positive or numbers, exits loop
{
break;
}
else//if value is not valid, runs this
{
//if input fails, run this
cin.clear();//clears line
cin.sync();
cout << "nSorry, that was not a valid number. Please enter a valid numbern ";//asks user to input valid input
}
}
cin.ignore(80, 'n');
//asks user to enter whether filing singly or jointly
cout << "nIf you will be filing singly, enter 's'. If you will be filing jointly, enter 'm'n ";
while (!(cin >> file)) //get input
{
if (file == "s" || "m")//if input is valid, save and break loop
{
cin.ignore(80, 'n');
break;
}
else//if value not valid, print message and repeat
{
//if input fails, run this
cin.clear();
cin.sync();
cout << "nThat is not a correct input, please enter s/mn ";
}
}
if (file == "s")//loop determines double values for single filing
{
if (income <= 863)//if income is under this value, use these settings
{
taxRate = .022;
subtract = 0;
add = 0;
}
if (income >= 864 & income <= 2588)//if income is between these values, use these settings
{
taxRate = .033;
subtract = 863;
add = 25;
}
if (income >= 2589 & income <= 4313)//if income is between these values, use these settings
{
taxRate = .062;
subtract = 2588;
add = 85;
}
if (income > 4313)//if income is above this value, use these settings
{
taxRate = .075;
subtract = 4313;
add = 181;
}
}
if (file == "m")//sets double values if joint filing
{
if (income <= 1726)//if income is under this value, use these settings
{
taxRate = .022;
subtract = 0;
add = 0;
}
if (income >= 1727 & income <= 5176)//if income is between these values, use these settings
{
taxRate = .033;
subtract = 1726;
add = 40;
}
if (income >= 5177 & income <= 8626)//if income is between these values, use these settings
{
taxRate = .062;
subtract = 5176;
add = 175;
}
if (income > 8626)//if income is above this value, use these settings
{
taxRate = .075;
subtract = 8626;
add = 390;
}
}
incomeTax = ((income - subtract) * taxRate) + add;//determines value of income tax
cout << "nYour taxable income is " << income << endl;//prints total income
if (file == "s")//prints 'single' if 's' is selected
{
taxStatus = "Singly";
}
if (file == "m")//prints 'jointly' if 'm' selected
{
taxStatus = "Jointly";
}
cout << "nand you are filing " << taxStatus << endl;//prints filing status
cout << "nThat means your income tax will be " << incomeTax << endl;//prints actual income tax
cout << "nWould you like to conduct another operation, y/n?n ";//asks if user wants to do another filing
while (!(cin >> answer)) //get input
{
if (answer == "y")//returns to start of program if yes
{
goto top;
}
if (answer == "n")//breaks loop if no
{
break;
}
else//if not valid answer, print message and run again
{
//if input fails, run this
cin.clear();
cin.sync();
cout << "nThat is not a correct input, please enter y/n. ";//asks user to try again
}
}
cin.ignore(80, 'n');
system("PAUSE");
return 0;//ends program
}

你的代码中有一些小错误,比如if(file == "s"|| "m"这是不正确的,它应该是if(file == "s" || file == "m")的,你有一个&这是一个引用,它采用变量的地址或按位。 你想要的是两个&&这意味着和
在这里这样做:

while (!(isdigit(income))) // does after cin >> income;
{
//if input fails, run this
cin.clear();
cin.sync();
cout << "nSorry, that was not a valid number. Please enter a valid numbern ";
cin >> income
}
while (!(file == "s" || file == "m")) //this goes after cin >> file;
{
cin.clear();
cin.sync();
cout << "nThat is not a correct input, please enter s/mn ";
cin >> file;
}