[英]C++ program to determine taxes
因此,該代碼應要求用戶輸入其收入,無論是共同報稅還是單獨報稅,輸出總收入和所得稅,然后詢問用戶是否要重做或退出。 但是由於某種原因,它會迫使您在每次輸入之前繼續輸入兩次。 我知道我可能缺少一些簡單的代碼問題,但我無法弄清楚。 使用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;
}
您在代碼中有一些小錯誤,例如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;
}
因此,我讓它不再要求兩次輸入,因此感謝您的幫助。 在以美元格式輸出收入和所得稅並在末尾選擇“ 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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.