-1

So my issue is that I am trying to create a tax program that gets a user input for income, status, and then prints out the results. I can get that all done with no errors. Once I try to get it inside of a while loop that will prompt the user "Do you want to calculate again?" the user can input y/n. "n" finishing the program and "y" needs to repeat the program and the user inputs information again and so on. I can't get the program to let the user input new values in for the variables. It will repeat the results only. Can someone help me with figuring that out? I have tried everything I can and have checked where I can and what others do, doesn't work for me. Any help is appreciated!

#include <iostream>
#include <string>
using namespace std;
double income, incomeTax;
char maritalStatus;
char again = 'Y';




int main() {
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    income = -1;
    //Taxable Singles
    const double ts1 = 863;
    const double ts2 = 2588;
    const double ts3 = 4313;
    //Taxable Marriage
    const double tm1 = 1726;
    const double tm2 = 5176;
    const double tm3 = 8626;
    //Tax Rates
    const double tr1 = .023;
    const double tr2 = .033;
    const double tr3 = .052;
    const double tr4 = .075;
    double t1, t2, t3;
    //Add variable
    const double as1 = 25, as2 = 85, as3 = 181;
    const double am1 = 40, am2 = 175, am3 = 390;
    //Addition variable simplified
    double a1, a2, a3;
    //const strings
    const string s = "single", m = "joint";
    string status;

    int i = 0;

    //Beginning of Loop
    while (again == 'y' || again == 'Y') {

        while (income < 0) {
            cout << "Please enter your taxable Income." << "\n (This must be a positive value): " << endl;
            cin >> income;

            if (cin.fail() || income < 0) {
                cout << "Please try again. Needs to be a positive number." << endl;
                cin.clear();
                cin.ignore(40, '\n');
                income = -1;
            }
        }
        while (maritalStatus == false) {
            cout << "Please enter m if married and filing joint return," <<
                "\n or s if filing a single return: ";
            cin >> maritalStatus;

            if (maritalStatus != 's' && maritalStatus != 'm') {
                cout << "Please try again. Needs to be a 's' or 'm'." << endl;
                cin.clear();
                cin.ignore(40, '\n');
                maritalStatus = false;
            }
        }
        if (maritalStatus == 's') {
            t1 = ts1;
            t2 = ts2;
            t3 = ts3;
            a1 = as1;
            a2 = as2;
            a3 = as3;
            status = s;

        }
        else if (maritalStatus == 'm') {
            t1 = tm1;
            t2 = tm2;
            t3 = tm3;
            a1 = am1;
            a2 = am2;
            a3 = am3;
            status = m;
        }
        if (income > 0 && income <= t1) {
            incomeTax = (income - (0)) * tr1;
        }
        else if (income > t1 && income <= t2) {
            incomeTax = (income - (t1 - 1)) * tr2 + a1;
        }
        else if (income > t2 && income <= t3) {
            incomeTax = (income - (t2 - 1)) * tr3 + a2;
        }
        else if (income > t3) {
            incomeTax = (income - (t3 - 1)) * tr4 + a3;
        }

        cout << "Your taxable income is " << "$" << income << endl;
        cout << "and you're filing a" << status << " return." << endl;
        cout << "Your income tax will be " << "$" << incomeTax << endl;

        cout << "Go again? (y/n) ";
        cin >> again; //change control variable

    }// end loop

        system("pause");
        return 0;

}
Shirojin
  • 11
  • 3
  • Step through the code by hand. If you write down the values of the variables, you'll see your problem fairly easily. – Silvio Mayolo Jan 22 '18 at 05:17
  • You should've reduced this whole thing to the most basic loop (i.e. remove all the stuff that isn't asking the user to go again). There is way too much code here to look through. If you have just 10 lines, you'll probably find the problem yourself or at least anyone else will have an easier time finding it. You can [edit] your question at any time. – Tas Jan 22 '18 at 05:18
  • I was able to get it figured out after looking at what was going on. Though for some reason it doesn't round correctly.... – Shirojin Jan 23 '18 at 03:03

2 Answers2

1

You should probably have a separate function like void doTaxes() contain your business logic, then in main() you can just make the call while your condition is true. You can set your condition variable to false inside doTaxes, or even better have doTaxes() be an int or bool type and make set your condition variable based off the return value.

bool isTaxLoop(true);

void doTaxes(...);

int main()
{
    while(isTaxLoop)
    {
        doTaxes();
    }
    return EXIT_SUCCESS;
}
Justin Randall
  • 2,243
  • 2
  • 16
  • 23
0

You have to reset income for the second pass, as shown below.

income = -1;
while(again == 'y' || again == 'Y') 
{
    while(income < 0) 
    {
        ...
    }
    ...
    cout << "Go again? (y/n) ";
    cin >> again; 
    income = -1; //< -- reset income ****
}// end loop
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77