为什么这个循环当我给'y'作为输入但是当我给'n'时运行完全正常

时间:2017-08-28 03:42:29

标签: c++

这个问题主要在某个方面。有人请帮助我。我已经工作了几个小时,它不会停止循环。我想不出为什么这个代码会循环的任何道德原因。我在这里有这个巨大段落的原因是因为stackOverflow不会让我在没有更多细节的情况下发布这个问题。如果我按下“n”,它会正常运行,但如果我按下“y”,它会无休止地循环询问归档状态。

#include <iostream>
#include <math.h> 
using namespace std;

void CalculateTaxAmount(char status, double amount){
   int taxRate = 0;
   int subAmount = 0;
   int addAmount = 0;
   switch (status){
      case 's':
         {
            if (amount < 864){taxRate = 0; subAmount = 0; addAmount = 0;}
            else
               if (amount < 2589){taxRate = 1; subAmount = 863; addAmount = 25;}
               else
                  if (amount < 4313){taxRate = 2; subAmount = 2588; addAmount = 85;}
                  else {taxRate = 3; subAmount = 4313; addAmount = 181;}
         }
         //end single
         break;
      case 'm':
         {
            if (amount < 1727){taxRate = 0; subAmount = 0; addAmount = 0;}
            else
               if (amount < 5177){taxRate = 1; subAmount = 1726; addAmount = 40;}
               else
                  if (amount < 8626){taxRate = 2; subAmount = 5176; addAmount = 175;}
                  else {taxRate = 3; subAmount = 8626; addAmount = 390;}
         }
         //end married
         break;
      default:
         cout << "something went wrong" << endl;
         break;
   }
   double rate = 0;
   switch (taxRate){
      case 0: rate = 0.023; break;
      case 1: rate = 0.033; break;
      case 2: rate = 0.052; break;
      case 3: rate = 0.075; break;
   }
   double tax = (amount - subAmount) * rate + addAmount;
   tax = roundf(tax * 100) / 100;
   cout << tax << endl;
}

void calculateTax(){
   double taxableIncome = -1;
   // get the taxable income and make sure it is a positive amount. 
   while (taxableIncome < 0){
      cout << "Please enter in your taxable income.\n(This must be a positive value):" << endl;
      cin >> taxableIncome >> skipws;
   }
   char filingStatus = 'n';
   // get the filing status and make sure it is valid
   while(filingStatus != 'm' && filingStatus != 's'){
      cout << "Please enter m if married and filing joint return,\nor s if filing a single return:" << endl;
      cin >> filingStatus;
      cin.ignore(100, '\n');
   }
   cout << "Your taxable income is $" << taxableIncome << endl;
   // print out marital status
   switch(filingStatus){
      case 's':
         cout << "and you are filing a single return." << endl;
         break;
      case 'm':
         cout << "and you are filing a joint return." << endl;
         break;
      default: 
         cout << "something went wrong" << endl;
         break;
   }
   cout << "Your income tax will be $";
   CalculateTaxAmount(filingStatus, taxableIncome);
}

int main(){
   char rerun = 'a';
   do {
      calculateTax();
      while (rerun != 'q' && rerun != 'n' && rerun != 'y'){
         cout << "Would you like to do another calculation (y or n)?" << endl;
         cin >> rerun >> skipws;
      }
   }while (rerun == 'y');
}

2 个答案:

答案 0 :(得分:1)

似乎你有2个循环,条件相反:

int main(){
   char rerun = 'a';
   do {
      calculateTax();
      while (rerun != 'q' && rerun != 'n' && rerun != 'y'){
         cout << "Would you like to do another calculation (y or n)?" << endl;
         cin >> rerun >> skipws;
      }
   }while (rerun == 'y');
}

关注

   do {
      while (/* ... && */ rerun != 'y'){
         //...
      }
   }while (rerun == 'y');

如果内循环完成外循环无法完成,如果外循环可以完成 - 内循环保持处理......

因此解决方案将改变这两个条件之一,您可以选择您喜欢的方式。我建议从内循环中删除&& rerun != 'y'

答案 1 :(得分:0)

它循环,因为当你输入'y'时,重新运行设置为'y'并且你有:

while (rerun == 'y');

在此之后再也不会发生变化。

固定代码:

char rerun;
do {
  rerun = 'a';
  calculateTax();
  while (rerun != 'q' && rerun != 'n' && rerun != 'y'){
     cout << "Would you like to do another calculation (y or n)?" << endl;
      cin >> rerun >> skipws;
  }
} while (rerun == 'y');