2017-01-25 9 views
-6

私はプログラミングが初めてで、C++を学び始めたばかりです。私は、税金を払って人の純所得を決定するプログラムを作ろうとしています。ただし、プログラムが正常に動作しません。コンパイルして実行しますが、「年間経費」を求めて終了します。このコードには論理エラーがありますか?

#include <stdio.h> 
#include <iostream> 
#include <stdlib.h> 
using namespace std; 

int main() 
{ 

    float grossIncome, expenses, adjustedGIncome, taxRate, taxAmount, netIncome; //Declaration of variables 
    char again = 'y'; 
    while(again == 'y') //while loop used to rerun the program without having to recompile 
    { 
     system("reset"); 
     cout << "\nEnter your Gross Annual Income: "; //User enters his gross income 
     cin >> grossIncome; 
     cout << "\nEnter your annual expenses: "; //User enters his annual expenses 
     cin >> expenses; 

     adjustedGIncome = grossIncome - expenses; //gross income adjusted for taxes 
     return adjustedGIncome; 

     if (adjustedGIncome >= 415050) //if and else if statements used to determine tax percentage 
     { 
      taxRate = 0.396; 
      return taxRate; 
     } 
     else if (adjustedGIncome >= 413350 && adjustedGIncome < 415050) 
     { 
      taxRate = 0.35; 
      return taxRate; 
     } 
     else if (adjustedGIncome >= 190150 && adjustedGIncome < 413350) 
     { 
      taxRate = 0.33; 
      return taxRate; 
     } 
     else if (adjustedGIncome >= 91150 && adjustedGIncome < 190150) 
     { 
      taxRate = 0.28; 
      return taxRate; 
     } 
     else if (adjustedGIncome >= 37650 && adjustedGIncome < 91150) 
     { 
      taxRate = 0.25; 
      return taxRate; 
     } 
     else if (adjustedGIncome >= 9275 && adjustedGIncome < 37650) 
     { 
      taxRate = 0.15; 
      return taxRate; 
     } 
     else 
     { 
      taxRate = 0.1; 
      return taxRate; 
     } 

     taxAmount = adjustedGIncome * taxRate; //tax amount determined so that the net income can be determined 
     netIncome = adjustedGIncome - taxAmount; 

     cout << "\nAdjusted Gross Income: " << adjustedGIncome; //displays adjusted gross income 
     cout << "\nTax Rate: " << taxRate; //displays tax rate depending on adjusted gross income 
     cout << "\nTax Amount: " << taxAmount; //displays tax amount 
     cout << "\n\nNet Income: " << netIncome; //displays net income 
     cout << "\n\nRun this program again? (Y or N): "; //allows user to rerun program 
     cin >> again; 
     again =tolower(again); 
    } 
    system("reset"); 
} 
+2

あなたはreturn文を返しますreturn adjustedGIncome; – user1438832

+2

あなたは 'return'と' break'の間で混乱し、 'return'ステートメントを誤って使用しているようです。 – Shreevardhan

+0

なぜリセットを使用していますか? –

答えて

1

あなたは間違ってあなたのコード内のreturnステートメントを使用しています。 returnステートメントは、関数からの制御を呼び出されたものに戻します。この場合、主な機能を終了します。

この行に達した瞬間、return adjustedGIncome;はプログラムが終了し、この時点を超えて進歩することはありません。この行を削除し、税率を決定するためのif/elseツリーのすべての支店で同様のreturnのステートメントを見つけます。それらをすべて削除します。

関連する問題