C++ Primer Plus Chapter 6 Exercise 5

c plus plusExercise 5 requires we make a looping program to calculate tax, except the tax part is not really conventional. For instance, it is additive throughout different dollar amount. This make our else if structs a little more complicated.  A do-while loop satisfies the requirements of this program nicely, see my source below:

5. The Kingdom of Neutronia, where the unit of currency is the tvarp, has the following
income tax code:
first 5,000 tvarps: 0% tax
next 10,000 tvarps: 10% tax
next 20,000 tvarps: 15% tax
tvarps after 35,000: 20% tax
For example, someone earning 38,000 tvarps would owe 5,000 × 0.00 + 10,000 × 0.10
+ 20,000 × 0.15 + 3,000 × 0.20, or 4,600 tvarps. Write a program that uses a loop to
solicit incomes and to report tax owed. The loop should terminate when the user enters
a negative number or nonnumeric input.

#include <iostream>

using namespace std;

int main()
{
double tvarp = 0.0;
double tax;

do
{
cout << "Enter Tvarps earned this year: \n";
if(tvarp < 0 || !(cin >> tvarp))
{
cout << "Invalid input, non-negative numbers only!\n";
cin.clear();
}
else if (tvarp > 5000 && tvarp <= 15000)
tax = 5000*0 + (tvarp - 5000)*0.10;
else if (tvarp > 15000 && tvarp <= 35000)
tax = 5000*0 + 10000*0.10 + (tvarp - 15000)*0.15;
else if (tvarp > 35000)
tax = 5000*0 + 10000*0.10 + 20000*0.15 + (tvarp - 35000)*0.20;
else tax = 0;

cout << "Your tax is: " << tax << endl;
}
while(tvarp <= 0);

return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s