用函数调试练习 C++ 程序 Visual Studio 2015
Debugging a practice C++ program with functions Visual Studio 2015
我的 C++ 教科书让我写一个程序来计算欠税。我这样做了,但是当程序执行时,我只看到 "Are you married? True/False" 行,一旦我输入文本,就会再输出几行,程序立即结束。它应该再问用户几个问题并将输入存储在变量中,然后再进行操作,但程序在它有机会这样做之前就退出了。错误在哪里?太感谢了。
// 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;
}
不要使用 true,使用 1。
执行时 cout << "Are you married? True/False" << endl;
在命令提示符中,确保将 1 而不是 true 作为响应。这将解决您的问题。
我的 C++ 教科书让我写一个程序来计算欠税。我这样做了,但是当程序执行时,我只看到 "Are you married? True/False" 行,一旦我输入文本,就会再输出几行,程序立即结束。它应该再问用户几个问题并将输入存储在变量中,然后再进行操作,但程序在它有机会这样做之前就退出了。错误在哪里?太感谢了。
// 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;
}
不要使用 true,使用 1。
执行时 cout << "Are you married? True/False" << endl;
在命令提示符中,确保将 1 而不是 true 作为响应。这将解决您的问题。