Okay... I think I know my problem here
tax = rate*income/100;
is not working because I did not set those with specific value
Like: rate(15) or income(4000)
Well... is there any way to make this program so that it would be functional?
I asked my professor for help, but he does not know anymore than I do.
Any help will be apprecated. Save me!
p.s. if you anything related to programming projects from "Problem Solving: The object of programming, 4th edition" by walter savitch, please send it to me.
Result:
Enter the annual income: $4000.00
Enter the state tax rate: 15%
tax rate = 15%
tax = $0.00
net annual income = $4000.00
Code:
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
//Class for amounts of money in U.S. currency.
class Money
{
public:
friend Money operator +(const Money& amount1, const Money& amount2);
friend Money operator -(const Money& amount1, const Money& amount2);
friend Money operator *(const Money& amount1, const Money& amount2);
friend Money operator /(const Money& amount1, const Money& amount2);
friend istream& operator >>(istream& ins, Money& the_object);
friend ostream& operator <<(ostream& outs, const Money& a_percent);
friend bool operator ==(const Money& amount1, const Money& amount2);
Money(long dollars, int cents);
Money(long dollars);
Money( );
double get_value( ) const;
double value;
void input(istream& ins);
void output(ostream& outs) const;
private:
long all_cents;
};
int digit_to_int(char c);
int main( )
{
Money income, tax, total, rate;
cout << "Enter the annual income: ";
income.input(cin);
cout << "Enter the state tax rate: ";
cin >> rate;
cout << "tax rate = ";
cout << rate;
cout << endl;
tax = rate*income/100;
cout << "tax = ";
tax.output(cout);
cout << endl;
cout << "net annual income = ";
total = income - tax;
total.output(cout);
int exit;
cin >> exit;
return 0;
}
Money operator -(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents = amount1.all_cents - amount2.all_cents;
return temp;
}
Money operator +(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents = amount1.all_cents + amount2.all_cents;
return temp;
}
Money operator *(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents = (amount1.all_cents*amount2.all_cents);
return temp;
}
Money operator /(const Money& amount1, const Money& amount2)
{
Money temp;
temp.all_cents = amount1.all_cents / amount2.all_cents;
return temp;
}
bool operator ==(const Money& amount1, const Money& amount2)
{
return (amount1.all_cents == amount2.all_cents);
}
Money::Money(long dollars, int cents)
{
if(dollars*cents < 0) //If one is negative and one is positive
{
cout << "Illegal values for dollars and cents.\n";
exit(1);
}
all_cents = dollars*100 + cents;
}
Money::Money(long dollars) : all_cents(dollars*100)
{
//Body intentionally blank.
}
Money::Money( ) : all_cents(0)
{
//Body intentionally blank.
}
double Money::get_value( ) const
{
return (all_cents * 0.01);
}
//Uses iostream, cctype, cstdlib:
void Money::input(istream& ins)
{
char one_char, decimal_point,
digit1, digit2; //digits for the amount of cents
long dollars;
int cents;
bool negative;//set to true if input is negative.
ins >> one_char;
if (one_char == '-')
{
negative = true;
ins >> one_char; //read '$'
}
else
negative = false;
//if input is legal, then one_char == '$'
ins >> dollars >> decimal_point >> digit1 >> digit2;
if ( one_char != '$' || decimal_point != '.'
|| !isdigit(digit1) || !isdigit(digit2) )
{
cout << "Error illegal form for money input\n";
exit(1);
}
cents = digit_to_int(digit1)*10 + digit_to_int(digit2);
all_cents = dollars*100 + cents;
if (negative)
all_cents = -all_cents;
}
//Uses cstdlib and iostream:
void Money::output(ostream& outs) const
{
long positive_cents, dollars, cents;
positive_cents = labs(all_cents);
dollars = positive_cents/100;
cents = positive_cents%100;
if (all_cents < 0)
outs << "-$" << dollars << '.';
else
outs << "$" << dollars << '.';
if (cents < 10)
outs << '0';
outs << cents;
}
int digit_to_int(char c)
{
return ( int(c) - int('0') );
}
istream& operator >>(istream& ins, Money& the_object)
{
char percent_sign;
ins >> the_object.value;
ins >> percent_sign;
return ins;
}
ostream& operator <<(ostream& outs, const Money& a_percent)
{
outs << a_percent.value << '%';
return outs;
}