简体   繁体   中英

C++ program to determine taxes

so the code is supposed to ask user to input their income, whether they are filing jointly or singly, output total income and income tax, and then ask user if they want to do it again or exit. But for some reason it forces you to enter each input twice before it moves on. I know I am probably missing some simple problem with the code, but I can't figure it out. Using Visual Studio, Please Help!

int main()
{
    double income = 0;
    double taxRate = 0;
    double add = 0;
    double subtract = 0;
    double incomeTax = 0;
    string taxStatus = "";
    string file = "";
    string answer = "";
    top:
    cout << "\nPlease enter your income to calculate your taxes\n " << endl;
    cin >> income;
    while (!(cin >> income)) //get input
    {
        if (isdigit(income))
        {
            break;
        }

        else
        {
            //if input fails, run this
            cin.clear();
            cin.sync();
            cout << "\nSorry, that was not a valid number. Please enter a valid number\n ";
        }
    }
    cin.ignore(80, '\n');

    cout << "\nIf you will be filing singly, enter 's'. If you will be filing jointly, enter 'm'\n ";
    cin >> file;
    while (!(cin >> file)) //get input
    {
        if (file == "s" || "m")
        {
            cin.ignore(80, '\n');
            break;
        }

        else
        {
            //if input fails, run this
            cin.clear();
            cin.sync();
            cout << "\nThat is not a correct input, please enter s/m\n ";
        }
    }

        if (file == "s")
        {
            if (income <= 863)
            {
                taxRate = .022;
                subtract = 0;
                add = 0;
            }

            if (income >= 864 & income <= 2588)
            {
                taxRate = .033;
                subtract = 863;
                add = 25;
            }

            if (income >= 2589 & income <= 4313)
            {
                taxRate = .062;
                subtract = 2588;
                add = 85;
            }

            if (income > 4313)
            {
                taxRate = .075;
                subtract = 4313;
                add = 181;
            }
        }

        if (file == "m")
        {
            if (income <= 1726)
            {
                taxRate = .022;
                subtract = 0;
                add = 0;
            }

            if (income >= 1727 & income <= 5176)
            {
                taxRate = .033;
                subtract = 1726;
                add = 40;
            }

            if (income >= 5177 & income <= 8626)
            {
                taxRate = .062;
                subtract = 5176;
                add = 175;
            }

            if (income > 8626)
            {
                taxRate = .075;
                subtract = 8626;
                add = 390;
            }
        }

    incomeTax = ((income - subtract) * taxRate) + add;

    cout << "\nYour taxable income is " << income << endl;

    if (file == "s")
    {
        taxStatus = "Singly";
    }
    if (file == "m")
    {
        taxStatus = "Jointly";
    }
    cout << "\nand you are filing " << taxStatus << endl;

    cout << "\nThat means your income tax will be " << incomeTax << endl;

    cout << "\nWould you like to conduct another operation, y/n?\n ";
    cin >> answer;
    while (!(cin >> answer)) //get input
    {
        if (answer == "y")
        {
            goto top;
        }
        if (answer == "n")
        {
            break;
        }
        else
        {
            //if input fails, run this
            cin.clear();
            cin.sync();
            cout << "\nThat is not a correct input, please enter y/n. ";
        }
    }
    cin.ignore(80, '\n');

    system("PAUSE");

    return 0;
}

You have some minor mistakes in your code like if(file == "s"|| "m" this is incorrect it should be if(file == "s" || file == "m") and you have one & this is a reference it takes the address of variables or bitwise. what you want is two && which means and
Here do this:

while (!(isdigit(income))) // does after cin >> income;
{
        //if input fails, run this
        cin.clear();
        cin.sync();
        cout << "\nSorry, that was not a valid number. Please enter a valid number\n ";
        cin >> income
}

while (!(file == "s" || file == "m")) //this goes after cin >> file;
{
        cin.clear();
        cin.sync();
        cout << "\nThat is not a correct input, please enter s/m\n ";
        cin >> file;
}

So, I got it to stop asking for inputs twice, so thanks for your help with that. I am still having issues outputting income and income tax in dollar format and returning to the start of the program when 'y' is selected at the end. Any suggestions?

int main()
{
    double income = 0; //double stores income input
    double taxRate = 0;//double stores tax rate
    double add = 0;//double stores value added
    double subtract = 0;//double stores value to be subtracted
    double incomeTax = 0;//double stores final income tax
    string taxStatus = "";//string stores status of single or joint
    string file = "";//string stores values of s or m
    string answer = "";//string stores values of y or n
    top:
    //asks user to input income value
    cout << "\nPlease enter your income to calculate your taxes\n " << endl;
    while (!(cin >> income)) //get input
    {
        if (isdigit(income) & income > 0)//if value is positive or numbers, exits loop
        {
            break;
        }

        else//if value is not valid, runs this
        {
            //if input fails, run this
            cin.clear();//clears line
            cin.sync();
            cout << "\nSorry, that was not a valid number. Please enter a valid number\n ";//asks user to input valid input
        }
    }
    cin.ignore(80, '\n');

    //asks user to enter whether filing singly or jointly
    cout << "\nIf you will be filing singly, enter 's'. If you will be filing jointly, enter 'm'\n ";
    while (!(cin >> file)) //get input
    {
        if (file == "s" || "m")//if input is valid, save and break loop
        {
            cin.ignore(80, '\n');
            break;
        }

        else//if value not valid, print message and repeat
        {
            //if input fails, run this
            cin.clear();
            cin.sync();
            cout << "\nThat is not a correct input, please enter s/m\n ";
        }
    }

        if (file == "s")//loop determines double values for single filing
        {
            if (income <= 863)//if income is under this value, use these settings
            {
                taxRate = .022;
                subtract = 0;
                add = 0;
            }

            if (income >= 864 & income <= 2588)//if income is between these values, use these settings
            {
                taxRate = .033;
                subtract = 863;
                add = 25;
            }

            if (income >= 2589 & income <= 4313)//if income is between these values, use these settings
            {
                taxRate = .062;
                subtract = 2588;
                add = 85;
            }

            if (income > 4313)//if income is above this value, use these settings
            {
                taxRate = .075;
                subtract = 4313;
                add = 181;
            }
        }

        if (file == "m")//sets double values if joint filing
        {
            if (income <= 1726)//if income is under this value, use these settings
            {
                taxRate = .022;
                subtract = 0;
                add = 0;
            }

            if (income >= 1727 & income <= 5176)//if income is between these values, use these settings
            {
                taxRate = .033;
                subtract = 1726;
                add = 40;
            }

            if (income >= 5177 & income <= 8626)//if income is between these values, use these settings
            {
                taxRate = .062;
                subtract = 5176;
                add = 175;
            }

            if (income > 8626)//if income is above this value, use these settings
            {
                taxRate = .075;
                subtract = 8626;
                add = 390;
            }
        }

    incomeTax = ((income - subtract) * taxRate) + add;//determines value of income tax

    cout << "\nYour taxable income is " << income << endl;//prints total income

    if (file == "s")//prints 'single' if 's' is selected
    {
        taxStatus = "Singly";
    }
    if (file == "m")//prints 'jointly' if 'm' selected
    {
        taxStatus = "Jointly";
    }
    cout << "\nand you are filing " << taxStatus << endl;//prints filing status

    cout << "\nThat means your income tax will be " << incomeTax << endl;//prints actual income tax

    cout << "\nWould you like to conduct another operation, y/n?\n ";//asks if user wants to do another filing
    while (!(cin >> answer)) //get input
    {
        if (answer == "y")//returns to start of program if yes
        {
            goto top;
        }
        if (answer == "n")//breaks loop if no
        {
            break;
        }
        else//if not valid answer, print message and run again
        {
            //if input fails, run this
            cin.clear();
            cin.sync();
            cout << "\nThat is not a correct input, please enter y/n. ";//asks user to try again
        }
    }
    cin.ignore(80, '\n');

    system("PAUSE");

    return 0;//ends program
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM