2017-04-22 12 views
-1

私のC++教科書には、税金を計算するプログラムを書くように求められています。私はそうしましたが、プログラムが実行されると、私は "あなたは結婚していますか?真/偽"という行のみを表示し、テキストを入力すると、さらにいくつかの行が出力され、プログラムは直ちに終了します。ユーザーにもう少し質問をして、入力を変数に格納してから操作することになっていますが、プログラムはそれを実行する前に終了します。間違いはどこですか?どうもありがとうございます。関数を使用した練習C++プログラムのデバッグVisual Studio 2015

// ch7progExercise5.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <iostream> 

using namespace std; 

void getData(bool& marriedVal, int& childVal, int& salaryVal, double& pensionVal); 
double taxAmount(bool marriedVal, int childVal, int salaryVal, double pensionVal); 

bool married; 
int children, grossSalary; 
double pensionPercent; 

int main() 
{ 
    getData(married, children, grossSalary, pensionPercent); 
    taxAmount(married, children, grossSalary, pensionPercent); 
    return 0; 
} 

void getData(bool& marriedVal, int& childVal, int& salaryVal, double& pensionVal) 
{ 
    cout << "Are you married? True/False" << endl; 
    cin >> marriedVal; 
    if(marriedVal) 
    { 
     cout << "How many children under the age of 14 do you have?" << endl; 
     cin >> childVal; 
    } 
    cout << "What is your gross salary? If married, provide combined income." << endl; 
    cin >> salaryVal; 
    cout << "What percentage of your gorss income did you contribute to a pension fund?" << endl; 
    cin >> pensionVal; 
} 

double taxAmount(bool marriedVal, int childVal, int salaryVal, double pensionVal) 
{ 
    double standardExemption, pension, taxRate, tax, taxableIncome; 
    int numPeople, personalExemption; 
    if (marriedVal) 
    { 
     standardExemption = 7000; 
     numPeople = 2; 
    } 
    else 
    { 
     standardExemption = 4000; 
     numPeople = 1; 
    } 
    numPeople += childVal; 
    personalExemption = 1500 * numPeople; 
    pension = pensionVal*salaryVal; 
    taxableIncome = salaryVal - (standardExemption + pension + personalExemption); 
    if (taxableIncome < 15000) 
    { 
     taxRate = 0.15; 
     tax = taxRate*taxableIncome; 
    } 
    else if (taxableIncome < 40000) 
    { 
     taxRate = 0.25; 
     tax = 2250 + taxRate*(taxableIncome - 15000); 
    } 
    else if (taxableIncome > 40000) 
    { 
     taxRate = 0.35; 
     tax = 8460 + taxRate*(taxableIncome - 40000); 
    } 
    else 
     cout << "Invalid income" << endl; 
    cout << tax << endl; 
    return tax; 
} 
+3

デバッガを使用して調べてください。 https://msdn.microsoft.com/en-us/library/k0k771bt.aspx – UnholySheep

+0

デフォルトでは、ストリームは 'bool'に0と1を使います。 true/falseを有効にするには、['cin >> boolalpha'](http://en.cppreference.com/w/cpp/io/manip/boolalpha)を使用する必要があります。 –

答えて

0

は、真の使用いけない、これは、コマンドプロンプトでcout << "Are you married? True/False" << endl; を実行されますとき1. を使用し、応答としてあなたは真の代わりに1を入れていることを確認してください。これはあなたの問題を解決します。

+0

はい、これは動作します。どうもありがとう! –

+0

問題はありません。問題の答えとして選択してください。 – BlooB

+0

答えとしてどのように選ぶのですか? –

関連する問題