Hi everyone, I'm having problems with the output of this coding and I would really appreciate the help.

The assignment requires to show how someone's taxes are distributed by first ask the user for the amount of money (dollar[] and income) and at the same time prompt for the taxes in decimal (tax[]) form. Then it should output the input in a tax table, output the computed amount for 4 brackets, and output the total.

The problem is that my assignment also requires me to use the switch statement. Here's the code:

#include <iostream>
using namespace std;

//Global Amount/Tax Variables
float dollars[2];
float tax[2];
float income, tax_l, total;
int a = 0;

//Function
void input();
void output();

int main()
{
input();
output();

return 0;
}

//Input Function
void input()
{        
        //Inputs
        for (a = 0; a <= 2; a++)
        {
        cout << "Please enter the amount and the tax (0.XX form) for Tax Bracket " << a+1 << " :";
        cin >> dollars[a] >> tax[a];
        
        //Switch Conditions
        switch(a)
        {
 
        case 0:
        {
        if (dollars[0] < 0 || dollars[0] < dollars[1] || dollars[0] < dollars[2])
        {
        cerr << "\aERROR! INPUT FOR BRACKET 1 IS NOT VALID.";
        goto done;             //Leave the loops if the 'if' conditions are met
        }    
        }
        break;
    
        case 1:
        {
        if (dollars[1] < 0 || dollars[1] < dollars[2] || dollars[1] < dollars[0])
        {
        cerr << "\aERROR! INPUT FOR BRACKET 2 IS NOT VALID.";  
        goto done;             //Leave the loops if the 'if' conditions are met
        }    
        }
        break;
    
        case 2:
        {
        if (dollars[2] < 0 || dollars[2] < dollars[1] || dollars[2] < dollars[0])
        {
        cerr << "\aERROR! INPUT FOR BRACKET 3 IS NOT VALID."; 
//Error if 3rd input is less than zero
        goto done;         //Leave the loops if the 'if' conditions are met
        }    
        }
        break;            
        }          
        }
        //Input of the last tax and the income
            cout << "Please enter the tax percentage for the income above " << dollars[2] << " :"; 
            cin >> tax_l;
            cout << "\nPlease enter your annual income :";
            cin >> income;
done: ;
}

void output()
{
        tax[0] *= dollars[0];                                //Tax Computation
        tax[1] *= (dollars[1] - dollars[0]);
        tax[2] *= (dollars[2] - dollars[1]);
        tax_l *= (income - dollars[2]);
        float total = tax[0] + tax[1] + tax[2] + tax_l;

        cout << "Bracket 1 Tax = " << tax[0] << endl;       //Output of Tax Computation
        cout << "Bracket 2 Tax = " << tax[1] << endl;
        cout << "Bracket 3 Tax = " << tax[2] << endl;
        cout << "Bracket 4 Tax = " << tax_l << endl;
        cout << "Total Tax     = " << total;

Test amount is 14999 0.02
24999 0.03
34999 0.05
0.06
56000

Only Bracket 2 Tax output is correct and I also found that insertion of the output in the input() function still gives the same wrong outputs.

Recommended Answers

All 3 Replies

I'm not exactly sure what outcome you are looking for, but I immediately see a problem with your arrays.

judging from your loop:

for (a = 0; a <= 2; a++)
        {
        cout << "Please enter the amount and the tax (0.XX form) for Tax Bracket " << a+1 << " :";
        cin >> dollars[a] >> tax[a];

you need to declare your arrays to be:

float dollars[3];
float tax[3];

and replace all your goto statements with return. Very poor style.

The outcome I was looking for was the correct arithmetic (that are whole numbers) for the calculations. Instead I got a decimal and x10 ^ ? (e???)

And thank you very much. I didn't realized you could use return in this situation. ^^;

Try using fixed in a cout statement.

for example:

cout << "Total Tax     = " << fixed << total;

or on whichever line you are getting the answer in scientific notation

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.