2017-01-21 42 views
-1

我必须计算应纳税收入,并且由于某种原因,当我输入5000作为收入并为已婚夫妇提交了m时,它会给我一个稍微错误的数字。这里是我的代码:从if循环输出的错误号码

#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。任何人都可以帮忙指出出了什么问题?

+0

我认为你会从使用IDE与调试器中受益。我建议下载Visual Studio或Eclipse。如果您从未调试过代码,请在您选择的IDE中查找关于如何调试的简短YouTube视频。 – plukich

+0

为什么你不提供一个你应该计算你的代码的公式?这样我们可以将您的公式与您的程序如何计算税金并追踪错误进行比较。 –

+0

plukich,我已经在使用Visual Studio。 – riahtron3000

回答

0

您的if-clause是导致此行为的原因。在C++中写入类似if (mBase1 < income <= mBase2)的数学含义不同,因为编译器一次只比较两个值(如果我没有弄错)。

所以写:

if (mBase1 < income && income <= mBase2)而是确保,他应该比较这两个声明确保两个都是有效的。

更换线

if (mBase2 < income <= mBase3)

if (mBase2 < income && income <= mBase3)

已经做的伎俩在给你的给定的例子正确的提示(5000和“m)。如果没有,编译器认为某事如:

mBase2 <收入?那是假的,但收入< = mBase3?这是真的,让进入这个if子句

,从而覆盖你calcTax - 值

0
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 6\n"); 
    } 
    if (90 >6) 
    { 
     printf("6 is less than 90\n"); 
    } 
    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; 
}