所以代码应该要求用户输入他们的收入,无论是联合还是单独提交,输出总收入和所得税,然后询问用户是否要再次或退出。但由于某种原因,它会强制您在移动之前输入两次输入。我知道我可能错过了代码中的一些简单问题,但我无法弄明白。使用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 taxes\n " << 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 number\n ";
}
}
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/m\n ";
}
}
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;
}
答案 0 :(得分:0)
您的代码中存在一些小错误,例如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 number\n ";
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/m\n ";
cin >> file;
}
答案 1 :(得分: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 taxes\n " << 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 number\n ";//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/m\n ";
}
}
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
}