C++ if 语句问题 - 显示错误信息
我的程序在显示不应该显示的不同 if 语句中的信息时遇到问题。 http://i53.tinypic.com/1zyx68.png
#include <iostream>
#include <string>
using namespace std;
int main( )
{
string smreturn = "";
string jointsingle = "";
double income = 0.00;
double taxedincome = 0.00;
{
cout << "\nPlease enter your taxable income: ";
cin >> income;
while (income <= 0)
{
cout << "\n Please enter a valid positive taxable income: ";
cin >> income;
}
cout << "\n";
cout << "\nPlease press m if married and filing for a joint return, or s if filing for a single return: ";
cin >> smreturn;
while (!(smreturn =="m") && !(smreturn =="M") && !(smreturn =="s") && !(smreturn =="S"))
{
cout << "\nPlease press m if married and filing for a joint return,\n or s if filing for a single return: ";
cin >> smreturn;
}
if (smreturn == "m" || "M")
{
if (income >= 0 && income <= 1726)
{
taxedincome = income * .023;
}
if (income >= 1727 && income <= 3450)
{
taxedincome = (income - 1727) * .033 + 40;
}
if (income >= 3451 && income <= 5176)
{
taxedincome = (income - 3450) * .042 + 97;
}
if (income >= 5177 && income <= 6900)
{
taxedincome = (income - 5176) * .052 + 169;
}
if (income > 8626)
{
taxedincome = (income - 8626) * .07 + 362;
}
cout << "\nYour taxable income is:$" << (income);
cout << "\nAnd you are filing a joint return";
cout << "\nYour income tax will be:$" << (taxedincome);
cout << "\n";
}
if (smreturn == "s"||"S")
{
if (income >= 0 && income <= 863)
{
taxedincome = (income * .023);
}
if (income >= 864 && income <= 1726)
{
taxedincome = (income - 863) * .033 + 20;
}
if (income >= 1727 && income <= 2588)
{
taxedincome = (income - 1726) * .042 + 48;
}
if (income >= 2589 && income <= 3450)
{
taxedincome - (income - 2588) * 0.052 + 85;
}
if (income >= 3451 && income <= 4313)
{
taxedincome - (income - 3450) * 0.06 + 129;
}
if (income > 4313)
{
taxedincome - (income - 4313) * 0.07 + 181;
}
cout << "\nYour taxable income is:$" << (income);
cout << "\nAnd you are filing a single return";
cout << "\nYour income tax will be:$" << (taxedincome);
cout << "\n";
}
} cout << "\n";
string returninfo;
if (smreturn == "s" || "S")
{
returninfo == "single return";
}
else
{
returninfo == "joint return";
}
system("PAUSE");
return 0;
}
它显示来自两个 If 的信息报表,将纳税收入作为单一申报表保存为联合申报表。
I am having issues with my program displaying information from different if statements when they shouldn't be.
http://i53.tinypic.com/1zyx68.png
#include <iostream>
#include <string>
using namespace std;
int main( )
{
string smreturn = "";
string jointsingle = "";
double income = 0.00;
double taxedincome = 0.00;
{
cout << "\nPlease enter your taxable income: ";
cin >> income;
while (income <= 0)
{
cout << "\n Please enter a valid positive taxable income: ";
cin >> income;
}
cout << "\n";
cout << "\nPlease press m if married and filing for a joint return, or s if filing for a single return: ";
cin >> smreturn;
while (!(smreturn =="m") && !(smreturn =="M") && !(smreturn =="s") && !(smreturn =="S"))
{
cout << "\nPlease press m if married and filing for a joint return,\n or s if filing for a single return: ";
cin >> smreturn;
}
if (smreturn == "m" || "M")
{
if (income >= 0 && income <= 1726)
{
taxedincome = income * .023;
}
if (income >= 1727 && income <= 3450)
{
taxedincome = (income - 1727) * .033 + 40;
}
if (income >= 3451 && income <= 5176)
{
taxedincome = (income - 3450) * .042 + 97;
}
if (income >= 5177 && income <= 6900)
{
taxedincome = (income - 5176) * .052 + 169;
}
if (income > 8626)
{
taxedincome = (income - 8626) * .07 + 362;
}
cout << "\nYour taxable income is:$" << (income);
cout << "\nAnd you are filing a joint return";
cout << "\nYour income tax will be:$" << (taxedincome);
cout << "\n";
}
if (smreturn == "s"||"S")
{
if (income >= 0 && income <= 863)
{
taxedincome = (income * .023);
}
if (income >= 864 && income <= 1726)
{
taxedincome = (income - 863) * .033 + 20;
}
if (income >= 1727 && income <= 2588)
{
taxedincome = (income - 1726) * .042 + 48;
}
if (income >= 2589 && income <= 3450)
{
taxedincome - (income - 2588) * 0.052 + 85;
}
if (income >= 3451 && income <= 4313)
{
taxedincome - (income - 3450) * 0.06 + 129;
}
if (income > 4313)
{
taxedincome - (income - 4313) * 0.07 + 181;
}
cout << "\nYour taxable income is:$" << (income);
cout << "\nAnd you are filing a single return";
cout << "\nYour income tax will be:$" << (taxedincome);
cout << "\n";
}
} cout << "\n";
string returninfo;
if (smreturn == "s" || "S")
{
returninfo == "single return";
}
else
{
returninfo == "joint return";
}
system("PAUSE");
return 0;
}
It is displaying the info from both If statements, saving taxedincome as a single return for a joint return.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
应该是
在其他情况下很少会出现类似的错误。如果需要比较,则需要对
if
条件中的每个标识符重复进行比较。should be
And mistakes like these in few other conditions. If you need to compare, you need to repeat it for each identifier in the
if
condition.你的 if 语句是错误的:
试试这个:
这个语句有同样的问题:
以同样的方式修复它。
Your if statement is wrong:
Try this:
The same issue is with this statement:
Fix it the same way.
您的错误在于:
使用 || 时需要创建两个条件(或者)。示例:
您可以使用 && (AND) 在相同模式下。
建议:
不要使用
这不是一个好的做法。您将使用的 STD 的每个函数
示例:
“endl”是清除缓冲区并添加“\n”。
You error is in:
You need create two conditionals when you use || (OR). Example:
You can use the && (AND) in the same mode.
Recommendation:
Don't use
Isn't a good pratice. Every function of the STD you will use
Example:
The "endl" is the clear buffer and add "\n".
smreturn == "m" || “M”
始终为 true,因为“M”
将隐式转换为true
。您可能想要
smreturn == "m" || smreturn == "M"
。smreturn == "m" || "M"
is always true, because"M"
will implicitly be converted totrue
.You probably want
smreturn == "m" || smreturn == "M"
.您可能的意思是:
相等运算符测试相等性。您将其用作一种集合成员资格测试。如果能够编写就太好了
,但那是高级语言的保留。
You probably meant:
The equality operator tests for equality. You were using it as a kind of set membership test. If would be great to be able to write
but that's the preserve of higher level languages.