Click here to Skip to main content
15,909,503 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Ok writing this program for a week almost now and i have everything the way i need it and it works. Now what I am trying to do (the program calculates income based off of hours worked, taxes, etc) is if the user enters that they worked more than 60 hours in one week the prompt asks them if they want to override the 60 hour limit and continue calculating the result. So when asked to override the 60 hour limit "yes" continues the calculations and program and "no" stops the program. This is the part i am having issues with. My issue is at the END of the first set of nested if loops. here is the code

C++
// Program Project 4.cpp : Defines the entry point for the console application.
//Program that allows the user to take a name, hours worked, and hourly pay and calculate the gross pay, state and federal income tax withholdings as well as overtime pay for hours worked over 40 in 1 week


#include "stdafx.h"											
#include 
#include 
#include 

using namespace std;										

void pause()													
{ 
char junk;
cin.ignore();
cin.get(junk);
}





int main()						
{
	//declare variables

	string name, yes, no; 
	bool answer;
	double GetPay, GetHrs, PrintStub;
	double CalcFICA, CalcFedTax, CalcStateTax, GetRate, CalcFICA2, CalcFedTax2, CalcStateTax2, PrintStub2;
	double OverTime;
	
	
	
	cout << fixed << setprecision (2);	//numbers continue for 2 spaces past decimal point		
	
	//input

	cout << "What is the employees name?: "; //ask for name
	cin >> name;                   //entered input becomes name variable
	cout << "\n\n";
	cout << "What is the hourly rate of pay for this employee?: ";   // ask hourly pay
	cin >> GetRate;                //entered input becomes name variable
	cout << "\n\n";
	cout << "How many hours did this employee work?: ";  //ask for hours worked
	cin >> GetHrs;                 //entered input becomes name variable
	cout << "\n\n";

	//calculations for pay of hours

	GetPay = GetRate * GetHrs;
	OverTime = GetRate * 1.5 + GetPay;
	
	//statements for hours worked	

	{	
		if (GetHrs < 0) //if input hours are less than zero
		{
			cout << "Invalid number of hours"; //state that the input is invalid
		}
		else
			if (GetHrs <= 40) //if hours are less than 40 no overtime calculations needed
			{ 
				cout << name << ", your gross income before taxes is: $" << GetPay << "\n\n\n\n" ;
			}
				else
					if (( GetHrs >= 40) && (GetHrs <= 60)) //if 40 hours to 60 hours then output gross income
					{
						cout << name << ", your gross income before taxes is: $" << OverTime <<"\n\n\n\n";
					}
						else //if more than 60 hours ask if they want to override the limit of 60 hours
						{ 
							cout << "Excessive hours detected. Would you like to override the limit?  y/n ";
						   // cin >> answer;
					
	                 // if (answer = "y" )
					 // {
						  cout << name << ", your gross income before taxes is: $" << OverTime <<"\n\n\n\n";
					  }
					 //} 

	}

	 //statements for the hourly payrate

      {
		if ( GetRate < 5.5)  //if hourly rate of pay is under 5.5 it's slave labor and tell them it's invalid on screen
		{cout << "The hourly wages you have entered is invalid\n\n";
		}
		else 
			if (( GetRate > 5.5) && ( GetRate < 200)) //if hourly rate of pay is over 5.5 or under 200 no output to screen
			{
				cout << "";
			}
			else
				if (GetRate > 200)
				{
					cout << "The hourly wages you have entered are excessively high\n\n"; //if hourly rate of pay is over 200 say excessively high
				}
				}

	cout << "Deductions: \n\n";	

	//statement calculating FICA deductions

	CalcFICA = GetPay * .0765; //Fica rate if hours are less than 40
	CalcFICA2 = OverTime * .0765;// Fica rate if hours are more than 40

	{
		if (GetHrs < 40) //if hours less than 40
		{
			cout << "Your FICA deduction is: $" << CalcFICA << "\n\n"; 
		}
		else             //if hours are 40 or over
			{
				cout << "Your FICA deduction is: $" << CalcFICA2 << "\n\n";
		}}


	//statement for the federal tax withholdings

	CalcFedTax = GetPay * .22; //federal tax rate for no overtime
	CalcFedTax2 = OverTime * .22; //federal tax rate for overtime
	{
		if (GetHrs < 40) //of hours worked are less than 40
		{
			cout << "Your Federal Tax witholdings are: $" << CalcFedTax << "\n\n";
		}
		else       //if hours worked are 40 or over
		{
			cout << "Your Federal Tax withholdings are: $" << CalcFedTax2 << "\n\n";
		}}
	 

	//calculations for State Tax withholdings from pay
	
	CalcStateTax = GetPay * .12;   
	CalcStateTax2 = OverTime * .12;
	{
		if (GetHrs < 40)//if under 40 hours worked
		{
			cout << "Your State Tax withholdings are: $" << CalcStateTax << "\n\n";
		}
		else // if 40 hours or more worked
		{
			cout << "Your State Tax withholdings are: $" << CalcStateTax2 << "\n\n";
		}}

	//statement for Net Pay total after subtracting all withholdings

	PrintStub = GetPay - (CalcFICA + CalcFedTax + CalcStateTax); // PrintStub(NetPay) = Total amount made minus all withholdings
	PrintStub2 = OverTime - (CalcFICA2 + CalcFedTax2 + CalcStateTax2); // PrintStub(NetPay) = Total amount made with overtime included minus all withholdings
	{
		if (GetHrs < 40) //if under 40 hours worked
		{
			cout << "Your Net pay for this week will be: $" << PrintStub;
		}
		else // if 40 hours or more worked
		
		{
			cout << "Your Net pay for this week will be: $" << PrintStub2;
		}}

	//pause

	pause();
	return 0;
}
Posted

After removing your commented braces, this is your first nested if... I think you might see a possible cause:
C++
{	
	if (GetHrs < 0) //if input hours are less than zero
	{
		cout << "Invalid number of hours"; //state that the input is invalid
	}
	else if (GetHrs <= 40) //if hours are less than 40 no overtime calculations needed
	{ 
		cout << name << ", your gross income before taxes is: $" << GetPay << "\n\n\n\n" ;
	}
	else
		if (( GetHrs >= 40) && (GetHrs <= 60)) //if 40 hours to 60 hours then output gross income
		{
			cout << name << ", your gross income before taxes is: $" << OverTime <<"\n\n\n\n";
		}
		else //if more than 60 hours ask if they want to override the limit of 60 hours
		{ 
			cout << "Excessive hours detected. Would you like to override the limit?  y/n ";
			cout << name << ", your gross income before taxes is: $" << OverTime <<"\n\n\n\n";
		}

}
 
Share this answer
 
Comments
xaviorin 14-Dec-11 20:46pm    
Yeah i see what the issue is as in its not working because i deleted the code i was trying because none of it worked. My intention was to assign the variable "answer" as their input of y or n and then have y or n equal true/false or 0/1 and trying to have it work that way... but i could never get it to work. I was also considering an if else loop but when i put that together it also gave me a runtime error so im lost.

something like if (answer = y)
continue with calculations
else
Break
I dont know what i need to do to make it actually work though because that idea failed as well
xaviorin 14-Dec-11 20:47pm    
if else statement*
"answer" is defined to be a boolean and you are comparing it to a string of characters ("y"). That will never work. You probably need to read the data as a string or a char and do the proper compare.

(side note, I don't know if "cin" can input a boolean that isn't 0/1. Does it recognize 'y'/'Y' or 'n'/'N' as well? Just for my information. I generally do not use cin / cout.)
 
Share this answer
 
Comments
xaviorin 14-Dec-11 20:48pm    
Sorry some of my comment was that applied to your solution is in the comment for krmed's solution
change your if condition to

C++
if (answer)
 {
     cout << name << ": your gross income before taxes is: $" << OverTime << "\n\n\n\n";
 }



You should take your input as 0 or 1 from user and not y/n.
 
Share this answer
 
v2
Comments
Chuck O'Toole 15-Dec-11 1:58am    
Oh, I don't know about this. Isn't it more natural to ask a person a question and have then enter "y" or "n" (or the whole words "yes" or "no"). Humans don't generally answer yes/no questions with 0/1. It's up to a programmer to make thing easier on the human, not the other way around. Fix the code to read a string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900