2017-08-28 4 views
0

問題はmainのどこかにあります。誰かが私を助けてくれますか?私はこれを何時間も働いており、ループを止めることはありません。私は、このコードがループしている倫理的な理由について考えることはできません。私はこの巨大な段落を持っている理由は、stackOverflowが私に詳細を公開することができないためです。私が 'n'を押すとうまくいくのですが、 'y'を押すと無限にステータスを要求します。私は 'y'を入力しても 'n'を与える​​と完全に正常に動作するとループするのです

#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'); 
} 

答えて

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'); 
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'); 

にあなたの注意を払います終了しないでください。外側のループが完了すると、内側のループが処理を続けます。

解決策はこの2つの条件のいずれかを変更し、あなたの好みの方法を選択することができます。内側のループから&& rerun != 'y'を削除することをお勧めします。

+0

内部ループは、そのような理由からです。入力を3文字のいずれかに制限します。問題は、再実行が完了した後に再実行変数がリセットされないということです。 – ivo

関連する問題