从 if 循环输出的数字错误

Wrong number being output from if loops

本文关键字:数字 错误 输出 if 循环      更新时间:2023-10-16

我必须计算应税收入,出于某种原因,当我输入收入为 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;
}