There is something wrong with this program. The code is ok, the program runs, but it stops after asking the user how much their income is. Where am I going wrong? (Sorry, it's really long, but I can't figure out where the problem is)

#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>

using namespace std;

double percent(double,int,int,int,int,int);
double tax(double,int,int,int,int,int);
bool isvalid(string);

int main ()
{
    cout << "If you are filing taxes as a single, enter s.\n";
           cout << "If you are filing taxes as married filing jointly, enter mj.\n";
           cout << "If you are filing taxes as married filing separately, enter ms.\n";

           string method;
           string s, mj, ms;
           cin >> method;

    cout << "What is your total taxable income? ";
    string total;
    cin >> total;

    while (!isvalid(total))
           {
               if (isvalid(total))
               {
                   cout << "The income is valid." << endl;
               }
               else 
               {
                   cout << "The income entered is invalid. Please enter a valid amount.";
                   cin >> total;
               }

           }

    double income;
    income=atof(total.c_str());    

    if (method==s)
    {
        cout << "Percentage Bracket: " << percent(income,8375,34000,82400,171850,373650) << "%" << endl;
        cout << "Income Tax: $" << tax(income,8375,34000,82400,171850,373650) << endl;
        cout << fixed << setprecision(2) << "Actual Percentage of Tax: " << (tax(income,8375,34000,82400,171850,373650)/income)*100 << "%" << endl;
    }

    else if (method==mj)
    {
        cout << "Percentage Bracket: " << percent(income,16750,68000,137300,209250,373650) << "%" << endl;
        cout << "Income Tax: $" << tax(income,16750,68000,137300,209250,373650) << endl;
        cout << fixed << setprecision(2) << "Actual Percentage of Tax: " << (tax(income,16750,68000,137300,209250,373650)/income)*100 << "%" << endl;       
    }

    else if (method==ms)
    {
        cout << "Percentage Bracket: " << percent(income,8375,34000,68650,104625,186825) << "%" << endl;
        cout << "Income Tax: $" << tax(income,8375,34000,68650,104625,186825) << endl;
        cout << fixed << setprecision(2) << "Actual Percentage of Tax: " << (tax(income,8375,34000,68650,104625,186825)/income)*100 << "%" << endl;     
    }

    return 0;
}

bool isvalid(string str)
{
    int first=0;
    if (str.length()==0) return false;
    if (str.at(0)=='-') return false;
    if (str.length() && str.at(first)==0) return false;
    for (int i=first; i<str.length(); i++)
    {
        if (!isdigit(str.at(i))) return false;
    }

    return true;
}

double percent(double x,int c1,int c2,int c3,int c4,int c5)
{
    double b;
    if (x<c1)
    {
        b=10;
    }
    else if (x<c2)
    {
        b=15;
    }
    else if (x<c3)
    {
        b=25;
    }
    else if (x<c4)
    {
        b=28;
    }
    else if (x<c5)
    {
        b=33;
    }
    else 
    {
        b=35;
    }

    return b;
}          
double tax(double x,int c1,int c2,int c3,int c4,int c5)
{
    double tax;
    if (x<c1)
    {
        tax=.1*x;
    }
    else if (x<c2)
    {
        tax=(.1*c1)+(.15*(x-c1));
    }
    else if (x<c3)
    {
        tax=(.1*c1)+(.15*(c2-c1))+(.25*(x-c2));
    }
    else if (x<c4)
    {
        tax=(.1*c1)+(.15*(c2-c1))+(.25*(c3-c2))+(.28*(x-c3));
    }
    else if (x<c5)
    {
        tax=(.1*c1)+(.15*(c2-c1))+(.25*(c3-c2))+(.28*(c4-c3))+(.33*(x-c4));
    }
    else 
    {
        tax=(.1*c1)+(.15*(c2-c1))+(.25*(c3-c2))+(.28*(c4-c3))+(.33*(c5-c4))+(.35*(x-c5));
    }

    return tax;
}

Dani AI

Generated

As later noted, the symptom you described — the program apparently “stopping” after asking for income — is a classic logic/input bug rather than a runtime crash. The most likely culprit in the posted code is comparing the method string to other string variables that were never initialized. Comparing against an empty string will always fail, so none of the if/else branches run and the program appears to do nothing. The simplest fix is to compare to string literals, for example if (method == "s") (or initialize your constants to "s", "mj", "ms").

A couple of other fragile spots to watch for:

  • The isvalid routine only accepts digits, so inputs like 50000.50, 50,000, or $50000 will be rejected. Use std::stod with error checking, or parse with std::stringstream and verify the stream state to accept decimal amounts safely. Also drop the odd str.at(first) == 0 check — it’s unnecessary and can be misleading.
  • Guard against division by zero when you compute (tax / income) * 100. Check income > 0 before doing that calculation and handle zero explicitly.

Practical hardening tips: normalize the filing-status input (convert to lower case and trim whitespace), use clearer variable names (e.g., filingStatus instead of s), and separate parsing/validation from tax-calculation logic (so validation and computation are easier to test). Consider defining bracket cutoffs and rates as named constants or a small data structure instead of repeating magic numbers. These changes make the program more robust and easier to debug if similar symptoms reappear.

Forget it. I figured it out.

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.