我的程序显示来自不同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语句的信息,将taxedincome保存为联合回报的单一回报。
答案 0 :(得分:3)
if (smreturn == "m" || "M")
应该是
if ( (smreturn == "m") || (smreturn == "M") )
在其他一些条件下这些错误。如果需要进行比较,则需要为if
条件中的每个标识符重复它。
答案 1 :(得分:3)
您的if语句错误:
if (smreturn == "m" || "M")
试试这个:
if ((smreturn == "m") || (smreturn == "M"))
同样的问题是这句话:
if (smreturn == "s"||"S")
以同样的方式修复它。
答案 2 :(得分:1)
您的错误是:
if (smreturn == "m" || "M")
使用||时需要创建两个条件(要么)。例如:
if (age == 15 || yearOfBirth == 1996) {
std::cout << "Happy!" << std::endl;
} else {
std::cout << "Sad!" << std::endl;
}
您可以使用&amp;&amp; (AND)采用相同的模式。
<强>建议:强>
不要使用
using namespace std;
不是一个好的实践。您将使用的STD的每个功能
std::function
示例:
std::cout << "Hello World" << std::endl;
“endl”是清除缓冲区并添加“\ n”。
答案 3 :(得分:0)
smreturn == "m" || "M"
始终为真,因为"M"
会隐式转换为true
。
您可能想要smreturn == "m" || smreturn == "M"
。
答案 4 :(得分:0)
你可能意味着:
if ((smreturn == "m") || (smreturn == "M"))
等于运算符测试相等性。您使用它作为一种集合成员资格测试。如果能写的话会很棒
if (smreturn in ["m","M"])
但这是高级语言的保留。