A
arnuld
Do you have some suggestions for improvement. I have just written the half
of the program yet. Will continue the next half after suggestions for
improvement:
/* A program to find the Income Tax of an individual as per Indian Tax
Slabs:
Yearly Income slab (Rs.) Tax Rate (%)
Up to 1,50,000 NIL
Up to 1,80,000 (for women) NIL
Up to 2,25,000 NIL
(for resident individual of 65 years or above)
1,50,001 - 300,000 10
3,00,001 - 500,000 20
5,00,001 upwards 30
A surcharge of 10 per cent of the total tax liability is applicable where the total income exceeds Rs 1,000,000.
*
* DESIGN: 1st, we will write code to get information of the user
* 2nd, we calculate the Tax.
*
*
* VERSION 1.0
*
*/
#include <iostream>
#include <vector>
#include <string>
#include <limits>
#include <cfloat>
void get_info( long double&, int&, std::string& );
void get_payee_income( long double& );
void get_payee_age( int& );
void get_payee_sex( std::string& );
int main()
{
// const int ms = 150000;
// const int ws = 180000;
// const int ss = 225000;
long double income;
int age;
std::string sex;
get_info(income, age, sex);
std::cout << "your age = "
<< age
<< ", Your Sex = "
<< sex
<< ", Your income = "
<< income
<< std::endl;
//calculate_tax();
return 0;
}
void get_info(long double& i, int& a, std::string& s)
{
get_payee_income( i );
get_payee_age( a );
get_payee_sex( s );
}
void get_payee_income( long double& x )
{
const int min_income = 0;
const long double max_income = ULONG_MAX;
/* a combination of long double and ULONG_MAX is a catch, because if you put "unsigned long int max_income" on Left hand side
and user inputs value larger then ULONG_MAX then program, automagically will convert (cast ?) the value to some lower value
to satify the Left Hand Side and that will make it behave strange in some ways, doing some information loss. So I used
the maximum I have got, long double.
Is this the correct solution ? .. I don't think so.. I don't want to print the income in e+06 format. It needs to be a number.
*/
while ( (std::cout << "Enter your income: ") &&
(!(std::cin >> x)) ||
( x < min_income ) ||
( x > max_income ))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Don't play games. Enter a valid figure\n";
}
}
/* get age of the user, I have made sure it can handle stupid users */
void get_payee_age( int& age )
{
const int calments_age = 122;
const int min_age_for_work = 18;
while( ( std::cout << "Please enter your age: ") &&
(!(std::cin >> age)) ||
(age >= calments_age) ||
( age < min_age_for_work ) )
{
std::cout << "1- Oldest man lived on this Earth was Jeanne Calment of France (1875-1997)\n"
<< "who died at age 122 years and 164 days\n\n"
<< "2- Minimum age of work is "
<< min_age_for_work
<< "\n\n"
<< "3- Nthign other than numbers is accepted\n\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
/* get whether the user is a man or woman */
void get_payee_sex( std::string& t )
{
std::cout << "Please enter whether you are a man or a woman (only in small letters please): ";
std::cin >> t;
const std::string m = "man";
const std::string w = "woman";
int condm = t.compare(m);
int condw = t.compare(w);
while( condm )
{
if( 0 == condw ) break;
std::cout << "You must be either man or woman.I hope you have mistyped. Try Aagin: ";
std::cin >> t;
condm = t.compare(m);
condw = t.compare(w);
}
}
of the program yet. Will continue the next half after suggestions for
improvement:
/* A program to find the Income Tax of an individual as per Indian Tax
Slabs:
Yearly Income slab (Rs.) Tax Rate (%)
Up to 1,50,000 NIL
Up to 1,80,000 (for women) NIL
Up to 2,25,000 NIL
(for resident individual of 65 years or above)
1,50,001 - 300,000 10
3,00,001 - 500,000 20
5,00,001 upwards 30
A surcharge of 10 per cent of the total tax liability is applicable where the total income exceeds Rs 1,000,000.
*
* DESIGN: 1st, we will write code to get information of the user
* 2nd, we calculate the Tax.
*
*
* VERSION 1.0
*
*/
#include <iostream>
#include <vector>
#include <string>
#include <limits>
#include <cfloat>
void get_info( long double&, int&, std::string& );
void get_payee_income( long double& );
void get_payee_age( int& );
void get_payee_sex( std::string& );
int main()
{
// const int ms = 150000;
// const int ws = 180000;
// const int ss = 225000;
long double income;
int age;
std::string sex;
get_info(income, age, sex);
std::cout << "your age = "
<< age
<< ", Your Sex = "
<< sex
<< ", Your income = "
<< income
<< std::endl;
//calculate_tax();
return 0;
}
void get_info(long double& i, int& a, std::string& s)
{
get_payee_income( i );
get_payee_age( a );
get_payee_sex( s );
}
void get_payee_income( long double& x )
{
const int min_income = 0;
const long double max_income = ULONG_MAX;
/* a combination of long double and ULONG_MAX is a catch, because if you put "unsigned long int max_income" on Left hand side
and user inputs value larger then ULONG_MAX then program, automagically will convert (cast ?) the value to some lower value
to satify the Left Hand Side and that will make it behave strange in some ways, doing some information loss. So I used
the maximum I have got, long double.
Is this the correct solution ? .. I don't think so.. I don't want to print the income in e+06 format. It needs to be a number.
*/
while ( (std::cout << "Enter your income: ") &&
(!(std::cin >> x)) ||
( x < min_income ) ||
( x > max_income ))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Don't play games. Enter a valid figure\n";
}
}
/* get age of the user, I have made sure it can handle stupid users */
void get_payee_age( int& age )
{
const int calments_age = 122;
const int min_age_for_work = 18;
while( ( std::cout << "Please enter your age: ") &&
(!(std::cin >> age)) ||
(age >= calments_age) ||
( age < min_age_for_work ) )
{
std::cout << "1- Oldest man lived on this Earth was Jeanne Calment of France (1875-1997)\n"
<< "who died at age 122 years and 164 days\n\n"
<< "2- Minimum age of work is "
<< min_age_for_work
<< "\n\n"
<< "3- Nthign other than numbers is accepted\n\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
/* get whether the user is a man or woman */
void get_payee_sex( std::string& t )
{
std::cout << "Please enter whether you are a man or a woman (only in small letters please): ";
std::cin >> t;
const std::string m = "man";
const std::string w = "woman";
int condm = t.compare(m);
int condw = t.compare(w);
while( condm )
{
if( 0 == condw ) break;
std::cout << "You must be either man or woman.I hope you have mistyped. Try Aagin: ";
std::cin >> t;
condm = t.compare(m);
condw = t.compare(w);
}
}