C++ if 语句问题 - 显示错误信息

发布于 2024-11-18 01:42:51 字数 2932 浏览 2 评论 0原文

我的程序在显示不应该显示的不同 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

十年不长 2024-11-25 01:42:51
if (smreturn == "m" || "M")

应该是

if ( (smreturn == "m") || (smreturn == "M") )

在其他情况下很少会出现类似的错误。如果需要比较,则需要对 if 条件中的每个标识符重复进行比较。

if (smreturn == "m" || "M")

should be

if ( (smreturn == "m") || (smreturn == "M") )

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.

半世晨晓 2024-11-25 01:42:51

你的 if 语句是错误的:

if (smreturn == "m" || "M")

试试这个:

if ((smreturn == "m") || (smreturn == "M"))

这个语句有同样的问题:

if (smreturn == "s"||"S")

以同样的方式修复它。

Your if statement is wrong:

if (smreturn == "m" || "M")

Try this:

if ((smreturn == "m") || (smreturn == "M"))

The same issue is with this statement:

if (smreturn == "s"||"S")

Fix it the same way.

一场信仰旅途 2024-11-25 01:42:51

您的错误在于:

if (smreturn == "m" || "M")

使用 || 时需要创建两个条件(或者)。示例:

if (age == 15 || yearOfBirth == 1996) {
    std::cout << "Happy!" << std::endl;
} else {
    std::cout << "Sad!" << std::endl;
}

您可以使用 && (AND) 在相同模式下。

建议:

不要使用

using namespace std;

这不是一个好的做法。您将使用的 STD 的每个函数

std::function

示例:

std::cout << "Hello World" << std::endl;

“endl”是清除缓冲区并添加“\n”。

You error is in:

if (smreturn == "m" || "M")

You need create two conditionals when you use || (OR). Example:

if (age == 15 || yearOfBirth == 1996) {
    std::cout << "Happy!" << std::endl;
} else {
    std::cout << "Sad!" << std::endl;
}

You can use the && (AND) in the same mode.

Recommendation:

Don't use

using namespace std;

Isn't a good pratice. Every function of the STD you will use

std::function

Example:

std::cout << "Hello World" << std::endl;

The "endl" is the clear buffer and add "\n".

夕色琉璃 2024-11-25 01:42:51

smreturn == "m" || “M” 始终为 true,因为 “M” 将隐式转换为 true

您可能想要 smreturn == "m" || smreturn == "M"

smreturn == "m" || "M" is always true, because "M" will implicitly be converted to true.

You probably want smreturn == "m" || smreturn == "M" .

不醒的梦 2024-11-25 01:42:51

您可能的意思是:

if ((smreturn == "m") || (smreturn == "M"))

相等运算符测试相等性。您将其用作一种集合成员资格测试。如果能够编写就太好了

if (smreturn in ["m","M"])

,但那是高级语言的保留。

You probably meant:

if ((smreturn == "m") || (smreturn == "M"))

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

if (smreturn in ["m","M"])

but that's the preserve of higher level languages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文