从 if 循环输出的数字错误
Wrong number being output from if loops
我必须计算应税收入,出于某种原因,当我输入收入为 5000 并为已婚报税时,它给了我一个稍微错误的数字。这是我的代码:
#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。谁能帮助指出出了什么问题?
你的 if 子句是导致这种行为的原因。编写像if (mBase1 < income <= mBase2)
这样的东西在C++中没有相同的数学含义,因为编译器一次只比较两个值(如果我没记错的话)。
所以写:
相反,if (mBase1 < income && income <= mBase2)
确保他应该比较这两种说法,并确保两者都有效。
更换线路
if (mBase2 < income <= mBase3)
与if (mBase2 < income && income <= mBase3)
已经为您给定的示例(5000 和"m")提供了正确的提示。如果没有它,编译器会这样想:
mBase2
从而覆盖您的calcTax
值
You cannot compare two conditions at a time using if statement
if(a<b<c) is similar to
if( (a<b) || (b<c) )
so in your program you should use && instead of comparing straight away
例:
#include<stdio.h>
int main()
{
if (5 < 6)
{
printf("5 is less than 6n");
}
if (90 >6 )
{
printf("6 is less than 90n");
}
if (5 < 6 <90) // here only half of the condition checking takes place ..till (5<6) and (6<90 )is not verified
{
printf("5 is less than 6 and 6 is less than 90");
}
getchar();
return 0;
}
so change your code to--->
#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) && (income <= mBase2))
{
calcTax = (((income - mBase1) * taxRate2) + mAdd2);
}
if ((mBase2 < income) &&(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位数字cpp
- 在使用堆栈为下一个最大数字编写代码时面临 SIGSEGV(分段错误)
- 错误含义;以二进制形式打印数字
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 我如何在一个 if 语句中声明所有数字我尝试通过其他方式声明所有数字,如果一个接一个,但似乎代码有逻辑错误
- 分布MPI散点错误的数字
- 涉及模运算符优先级的错误以及C++中具有大数字的括号
- Arduino IDE 错误 - 无法找到数字文字运算符"运算符""f900ff"
- 为什么C++显示错误的数字?
- 错误:数字常量 #define BOOT_PROTOCOL 0x00 之前的预期'>'
- 我的代码似乎在查找最大数字时存在语法错误
- Fibbonaci 递归代码返回错误值,始终返回下一个数字
- C++:按数字排序链表错误
- isdigit() 和 isalnum() 给出错误,因为输入是一个常量字符并且无法转换。其他可能查看输入是否为数字的方法?
- 一个数字的提升精神解析器的分段错误
- C++铸造浮子错误地更改了数字
- 错误:数字常数之前的预期无限制ID:std :: array和std ::向量大小分配
- 从 if 循环输出的数字错误
- 我同时使用了莱布尼茨和瓦利斯公式来估算π但输出数字错误,我不知道我哪里出错了
- 对double进行重载比较以允许出现数字错误