我的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;
}
使用你的調試器找出? https://msdn.microsoft.com/en-us/library/k0k771bt.aspx – UnholySheep
默認情況下,流使用0和1作爲'bool'。要啓用真/假,你必須使用['cin >> boolalpha'](http://en.cppreference.com/w/cpp/io/manip/boolalpha)。 –