Having some problems getting this code to do what I want it to do. I can get it to perform some of it correctly. But the main problem is 2 fold. First I need to get the user information (fname, lname, ssn, marital status) and then enter their income and have it display the total taxes and then repeat until the sentinel value of 99 is entered at the last name prompt. Any ideas? I'm new so dont rip me one if I miss something blatantly obvious. But the problem I'm having is that it when entering the income it gets stuck asking the income until it crashes or I exit. Whats the issue I'm not seeing?
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
constdouble SINGLE_LEVEL1 = 21450.00;
constdouble SINGLE_LEVEL2 = 51900.00;
constdouble SINGLE_TAX1 = 3217.50;
constdouble SINGLE_TAX2 = 11743.50;
constdouble MARRIED_LEVEL1 = 35800.00;
constdouble MARRIED_LEVEL2 = 86500.00;
constdouble MARRIED_TAX1 = 5370.00;
constdouble MARRIED_TAX2 = 19566.00;
constdouble RATE1 = 0.15;
constdouble RATE2 = 0.28;
constdouble RATE3 = 0.31;
float income;
double tax = 0;
string lastName;
string firstName;
string ssn;
char marital_status;
bool marital_status_ok;
cout << "Please enter your last name: ";
cin >> lastName;
cout << "Please enter your first name: ";
cin >> firstName;
cout << "Please enter your Social Security Number (No spaces or dashes please): ";
cin >> ssn;
cout << "Please enter s for single, m for married: ";
cin >> marital_status;
if (marital_status != 's', 'S', 'm','M')
{ // start main marital status entry check
marital_status_ok = false;
if (marital_status_ok == true)
{ // end outter if marital status true
while (marital_status_ok = true)
{ // start while for marital status
while (marital_status == 's' || 'S')
{ // start while S
cout << "Please enter your income: "; // single tax income set
cin >> income;
if (income <= SINGLE_LEVEL1)
tax = RATE1 * income;
elseif (income <= SINGLE_LEVEL2)
tax = SINGLE_TAX1
+ RATE2 * (income - SINGLE_LEVEL1);
else
tax = SINGLE_TAX2
+ RATE3 * (income - SINGLE_LEVEL2);
} // end while S
while (marital_status = 'm' || 'M');
{ // start while M
cout << "Please enter your income: "; // married tax income set
cin >> income;
if (income <= MARRIED_LEVEL1)
tax = RATE1 * income;
elseif (income <= MARRIED_LEVEL2)
tax = MARRIED_TAX1
+ RATE2 * (income - MARRIED_LEVEL1);
else
tax = MARRIED_TAX2
+ RATE3 * (income - MARRIED_LEVEL2);
} // end while M
cout << "The tax is $" << tax << "\n"; // output tax amount
} // end while martial status true
} // end if marital status is true
} // end if marital status check != s,S,m,M
else (marital_status_ok = false);
{ // start error message
cout <<"That is not a valid input. Please enter an S for single or an M for married.";
} // end error message
system("PAUSE"); // pause screen
return 1;
} // end main function
But the problem I'm having is that it when entering the income it gets stuck asking the income until it crashes or I exit
It doesn't appear that you're mixing formatted and unformatted input, so that isn't likely to be the problem. On the other hand, you never check or handle input operation failure, so that seems a likely source.
Ok thank you I will read up on that. There are a couple things I haven't got to yet. This is where I was at, thus far. Thank you though for the info though, I'll look into that.
Ok so I dumped the nested if/else and I'm setting it up as a switch statement. But I for some reason cant figure out how to get it to work. Also how would I set up the sentinel under last name, since last name is a string and the sentinel has to be an int 99 under last name. How do I tackle this. I think I am just thinking into it way too much.
Ok so I fixed that issue. Its getting the income and calculating the tax but once the tax is done its asking for income again instead of going back to the beginning and starting over
Its like its stuck in the loop and Im not sure of how to get it out and keep it asking for another person to enter their info until the sentinel (99) found in the last name prompt exits
Ok got the 99 part. Now I need to figure out how to break out of the switch statement at the income prompt. :/ I can get the first input but it keeps looping