对于循环输出问题

时间:2016-02-11 19:03:08

标签: c++

should be getting the output on the picture**税表显示三列:年薪,税率和支付的税额。下面的程序显示了计算税表所需的大部分代码

#include <iostream>
using namespace std;
enter code here

int main() {
int INCOME_INCREMENT        = 5000;
int    annualSalary         = 0;
double taxRate              = 0.0;
int    taxToPay             = 0;
int    startingAnnualSalary = 40000;  40000
int    endingAnnualSalary   = 60000;  60000

cout << "Enter first annual salary for the table: " << endl;
cin >> startingAnnualSalary;
cout << "Enter last annual salary for the table: " << endl;
cin >> endingAnnualSalary;
cout << "Enter the increment for the table: " << endl;
cin >> INCOME_INCREMENT;





for (int i = 0 ; i < 9 ; i++ ){


// Determine the tax rate from the annual salary
if (annualSalary <= 0) {
     taxRate = 0.0;
  }
else if (annualSalary <= 20000) {
     taxRate = 0.10;   // 0.10 is 10% written as a decimal
  }
else if (annualSalary <= 50000) {
     taxRate = 0.20;
  }
else if (annualSalary <= 100000) {
     taxRate = 0.30;
  }
else {
     taxRate = 0.40;
  }

taxToPay = static_cast<int>(annualSalary  -taxToPay * taxRate);   //  Truncate tax to an integer amount
cout << "Annual salary: " << annualSalary <<
          "\tTax rate: " << taxRate <<
          "\tTax to pay: " << taxToPay << endl;

// FIXME: Be sure to include the closing brace for the for loop
}
return 0;
}

1 个答案:

答案 0 :(得分:0)

您不能从输入中为annualSalary分配值。

您应该尝试:

#include <iostream>
using namespace std;

int main() {
    int INCOME_INCREMENT        = 5000;
    int    annualSalary         = 0;
    double taxRate              = 0.0;
    int    taxToPay             = 0;
    int    startingAnnualSalary = 40000;  //40000  <-- comment out
    int    endingAnnualSalary   = 60000;  //60000  <-- comment out

    cout << "Enter first annual salary for the table: " << endl;
    cin >> startingAnnualSalary;
    cout << "Enter last annual salary for the table: " << endl;
    cin >> endingAnnualSalary;
    cout << "Enter the increment for the table: " << endl;
    cin >> INCOME_INCREMENT;


    annualSalary = startingAnnualSalary;  // Set the start value


    while(annualSalary <= endingAnnualSalary ){  // Loop until reaching end value 
        // Determine the tax rate from the annual salary
        if (annualSalary <= 0) {
             taxRate = 0.0;
        }
        else if (annualSalary <= 20000) {
             taxRate = 0.10;   // 0.10 is 10% written as a decimal
        }
        else if (annualSalary <= 50000) {
            taxRate = 0.20;
        }
        else if (annualSalary <= 100000) {
            taxRate = 0.30;
        }
        else {
            taxRate = 0.40;
        }

        taxToPay = static_cast<int>(annualSalary * taxRate + 0.5);   //  Correct calculation
                                                                     // Add 0.5 for correct rounding
        cout << "Annual salary: " << annualSalary <<
                  "\tTax rate: " << taxRate <<
                  "\tTax to pay: " << taxToPay << endl;

        annualSalary +=  INCOME_INCREMENT;  // Update annualSalary for next loop
    }
    return 0;
}

输出:

Annual salary: 40000    Tax rate: 0.2   Tax to pay: 8000
Annual salary: 42500    Tax rate: 0.2   Tax to pay: 8500
Annual salary: 45000    Tax rate: 0.2   Tax to pay: 9000
Annual salary: 47500    Tax rate: 0.2   Tax to pay: 9500
Annual salary: 50000    Tax rate: 0.2   Tax to pay: 10000
Annual salary: 52500    Tax rate: 0.3   Tax to pay: 15750
Annual salary: 55000    Tax rate: 0.3   Tax to pay: 16500
Annual salary: 57500    Tax rate: 0.3   Tax to pay: 17250
Annual salary: 60000    Tax rate: 0.3   Tax to pay: 18000

税收制度相当糟糕!如果支付50.000,如果支付52.500,你最终会获得更多的钱: - )