1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
// Empty Project.cpp
#include <iostream>
#include <iomanip>
#include <conio.h> // for _getch()
#include <chrono>
#include <thread>
void RequiredData(char &maritalStatus, int &numberOfChildren, double &grossSalary, double &pContributedToPension,
double &taxableAmount, double &sDeduction, double &tax, double &numberOfDeduction);
void TaxCalculator(double tax);
// Moved these here. They work better as golbal variabls
const double MARRIED_DEDUCTION = 9000.00;
const double SINGLE_DEDUCTION = 3000.00;
const double PERSONAL_EXECPTION = 1800.00;
int main()
{
// Used to make the program wait for a number of seconds.
// std::this_thread::sleep_for(std::chrono::seconds(3)); // include "chrono" and "thread" header files
// std::this_thread::sleep_for(std::chrono::milliseconds(1000));
// Initalize all variabls.
char maritalStatus{ ' ' };
int numberOfChildren{ 0 };
double grossSalary{ 0.0 };
double pContributedToPension{ 0.0 }; // <--- Careful pContributedToPension the beginning "p" could be preceived a meaning a pointer.
double taxableAmount{ 0.0 };
double sDeduction{ 0.0 };
double tax{ 0.0 };
double numberOfDeduction{ 0.0 };
// Added this line to test the program.
// The way I believe the function should be called
RequiredData(maritalStatus, numberOfChildren, grossSalary, pContributedToPension, taxableAmount,
sDeduction, tax, numberOfDeduction);
// A similar type of call to "TaxCalculator" would be needed.
// ****** Used to kep the console window open during testing.
std::cout << "\n\n\n\nFin Press any key to continue -- > ";
_getch();
return 0; // <--- Added
} // End main
//void RequiredData() // <--- Your original.
void RequiredData(char &maritalStatus, int &numberOfChildren, double &grossSalary, double &pContributedToPension,
double &taxableAmount, double &sDeduction, double &tax, double &numberOfDeduction)
{
// ***********************************************************
// duplication defination of a global variable not needed here.
//const double MARRIED_DEDUCTION = 9000.00;
//const double SINGLE_DEDUCTION = 3000.00;
//const double PERSONAL_EXECPTION = 1800.00;
// ***********************************************************
// These variavles should only be defined once and not here. Consider passing them as a refrence
// to the function. Otherwise their values will be lost when the function goes out of scope.
// passed into function. Commented these variables out due to the changes I made.
//char maritalStatus;
//int numberOfChildren;
//double grossSalary;
//double pContributedToPension;
//double taxableAmount;
//double sDeduction;
//double tax;
//double numberOfDeduction{ 0.0 };
std::cout << std::fixed << std::showpoint;
std::cout << std::setprecision(2);
std::cout << "Enter marital status: m or M (married), s or S (single): ";
std::cin >> maritalStatus;
std::cout << std::endl;
// You might conider using maritalStatus = toupper(maritalStatus); and in the if statement
// if(maritalStatus == 'M') or just if(toupper(maritalStatus) == 'M')
if (maritalStatus == 'm' || maritalStatus == 'M')
{
std::cout << "Enter number of children under the age of 14: ";
std::cin >> numberOfChildren;
std::cout << std::endl;
}
if (maritalStatus == 'm' || maritalStatus == 'M')
{
numberOfDeduction = 2 + numberOfChildren;
sDeduction = MARRIED_DEDUCTION;
}
else
{
numberOfDeduction = 1;
sDeduction = SINGLE_DEDUCTION;
std::cout << "Enter gross salary for the year: ";
std::cin >> grossSalary;
std::cout << std::endl;
std::cout << "Enter percentage of salary contributed to pension (0-9 percent): ";
std::cin >> pContributedToPension;
std::cout << std::endl;
}
}
void TaxCalculator(double tax) // <--- Changed to match the proto type at the beginning of the program.
// tax should be passed as a referenc or rturned when the function ends.
// But rturnning tax is just a guess for now.
{
// These variavles should only be defined once and not here. Consider passing them as a refrence
// to the function.
double grossSalary{ 0.0 };
double pContributedToPension{ 0.0 };
double taxableAmount{ 0.0 };
double sDeduction{ 0.0 };
double tax; // <--- Redefination of what was passed to the function.
double numberOfDeduction{ 0.0 };
const double PERSONAL_EXECPTION = 1800.00; // should be defined as a global variable and not neded here.
// consider order of precedence some () may be needed.
taxableAmount = grossSalary - sDeduction -
PERSONAL_EXECPTION * numberOfDeduction -
grossSalary * pContributedToPension / 100;
if (taxableAmount < 0)
taxableAmount = 0;
if (0 <= taxableAmount && taxableAmount <= 15000)
tax = taxableAmount * .15;
else if (15001 <= taxableAmount && taxableAmount <= 40000)
tax = 2250 + (taxableAmount - 15000) * .25;
else
tax = 8460 + (taxableAmount - 40000) * .35;
std::cout << std::endl;
std::cout << "Your tax for the year is: " << tax;
}
|