Including Defined functions, & issue with IF Statement

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 788 Views - Last Post: 19 December 2011 - 08:32 PM Rate Topic: -----

#1 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 09-March 11

Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 11:55 AM

So I have the below where I got working quite nicely I believe. I give two options to choose from on pay status either "E" or "N". "E" works out well, but when I compile and choose "N" it spits out all of the Output before I even allow user to put it in. Any idea why this is?

Also, this is getting pretty hefty, and I wanted to include a 'void' function in here to make things easier and apply some new stuff - I read about void functions, is there an example of how to incorporate that into the below? I was not sure if I should do it with an IF statement or not.

Any assistance is appreciated, thanks


#include <iostream>
#include <string>

using namespace std;

// DECLARED 

int empSsn, salary, bonus, compensation, hourlyPay, hoursWorked, hourlyRate;
int totalRegularPay, overtimeRate, regularRate, overtimePay;
char empPaystatus;
string empName;
double grossPay, totalPay, ssnDeduction, medicareDeduction, finalPay;
const double ssnTax = .042;
const double medicare = .0145;

int main()
{

	cout << "Please enter Employee name: ";
	cin >> empName;

	cout << "Please enter employee Social Security Number: ";
	cin  >> empSsn;

	cout << "What is your current compensation status? \n";
	cout << "(Type 'E' for exempt or 'N' for nonexempt) ";

	cin >> empPaystatus;

// If statement below:

	if (empPaystatus == 'E')
{


	cout << "Please enter your salary: \n$";
	cin  >> salary;

	cout << "Please enter your bonus: \n$";
	cin >> bonus;

	cout << "Please enter any other compensation you have received: (If none, type 0) \n$";
	cin  >> compensation;

    grossPay = salary + bonus + compensation;
}
    if (grossPay <= 108600)
{



    medicareDeduction = grossPay * medicare;
	ssnDeduction = grossPay * ssnTax;
	finalPay = grossPay - (ssnDeduction + medicareDeduction);

	cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
	cout << "Employee Name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee Pay Status: " << empPaystatus<< "\n";
	cout << "Employee Salary: $" << salary<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee Compensation: $" << compensation<< "\n";
	cout << "Employee final pay: $" <<finalPay<< "\n";
}
    else if(grossPay >= 108600)
{


	medicareDeduction = grossPay * medicare;
	finalPay = grossPay - medicareDeduction;

	cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
	cout << "Employee Name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee Pay Status: " << empPaystatus<< "\n";
	cout << "Employee Salary: $" << salary<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee Compensation: $" << compensation<< "\n";
	cout << "Employee final pay: $" <<finalPay<< "\n";

}
///////////////////////////////////////////////////////////////////////////////////////

if (empPaystatus == 'N')
{
    cout << "Please enter your hourly rate of pay \n$";
    cin  >> hourlyRate;

    cout << "Please enter the amount of hours worked \n";
    cin  >> hoursWorked;

    cout << "Please enter the amount received for your bonus \n$";
    cin  >> bonus;

    cout << "Please enter any other compensation you have received: (If none, type 0) \n$";
    cin  >> compensation;

    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;


}
    if (hoursWorked >= 40)
{

    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;
    regularRate = totalRegularPay / hoursWorked;
    overtimeRate = regularRate * 0.5;
    overtimePay = (hoursWorked - 40) * overtimeRate;
    totalPay = totalRegularPay + overtimePay;


    cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
    cout << "Employee name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee pay Status: " << empPaystatus<< "\n";
	cout << "Employee hourly Rate: $" << hourlyRate<< "\n";
	cout << "Employee hours worked: $" << hoursWorked<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee compensation: $" << compensation<< "\n";
	cout << "Employee weekly pay: $" << hourlyPay<< "\n";
	cout << "Employee total pay: $" <<totalRegularPay<< "\n";
	cout << "Employee overtime pay: $" <<overtimePay<< "\n";
	cout << "Employee total pay with overtime: $" <<totalPay<< "\n";
}

    else if(hoursWorked <= 40)

{
    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;
    regularRate = totalRegularPay / hoursWorked;
    overtimeRate = regularRate * 0.5;
    overtimePay = (hoursWorked - 40) * overtimeRate;
    totalPay = totalRegularPay + overtimePay;


    cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
    cout << "Employee name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee pay Status: " << empPaystatus<< "\n";
	cout << "Employee hourly Rate: $" << hourlyRate<< "\n";
	cout << "Employee hours worked: $" << hoursWorked<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee compensation: $" << compensation<< "\n";
	cout << "Employee weekly pay: $" << hourlyPay<< "\n";
	cout << "Employee total pay: $" <<totalRegularPay<< "\n";
	cout << "Employee overtime pay: $" <<overtimePay<< "\n";
	cout << "Employee total pay with overtime: $" <<totalPay<< "\n";
}



return 0;
}


This post has been edited by jimblumberg: 19 December 2011 - 12:02 PM
Reason for edit:: Fixed Code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Including Defined functions, & issue with IF Statement

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 665
  • View blog
  • Posts: 2,296
  • Joined: 31-December 10

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 11:59 AM

You should put the "if(empPaystatus == 'N')" as an else if() to the if-statement that checks if empPaystatus == 'E'. The reason you're getting output before that is because you put an if-statement right after the if-statement that checks if empPaystatus == 'E', which outputs stuff. And could you please fix your "code" tags, you deleted a '[' at the end.

This post has been edited by vividexstance: 19 December 2011 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 09-March 11

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 12:10 PM

Ok I just changed it and it looks like it is good to go. Do you guys make heavy use of void functions or even the return function in your codes? I am looking to include it in this to make it easier, and start to incorporate it in future programs

#include <iostream>
#include <string>

using namespace std;

// DECLARED

int empSsn, salary, bonus, compensation, hourlyPay, hoursWorked, hourlyRate;
int totalRegularPay, overtimeRate, regularRate, overtimePay;
char empPaystatus;
string empName;
double grossPay, totalPay, ssnDeduction, medicareDeduction, finalPay;
const double ssnTax = .042;
const double medicare = .0145;

int main()
{

	cout << "Please enter Employee name: ";
	cin >> empName;

	cout << "Please enter employee Social Security Number: ";
	cin  >> empSsn;

	cout << "What is your current compensation status? \n";
	cout << "(Type 'E' for exempt or 'N' for nonexempt) ";

	cin >> empPaystatus;

// If statement below:

	if (empPaystatus == 'E')
{


	cout << "Please enter your salary: \n$";
	cin  >> salary;

	cout << "Please enter your bonus: \n$";
	cin >> bonus;

	cout << "Please enter any other compensation you have received: (If none, type 0) \n$";
	cin  >> compensation;

    grossPay = salary + bonus + compensation;
}

else if (empPaystatus == 'N')

{
    cout << "Please enter your hourly rate of pay \n$";
    cin  >> hourlyRate;

    cout << "Please enter the amount of hours worked \n";
    cin  >> hoursWorked;

    cout << "Please enter the amount received for your bonus \n$";
    cin  >> bonus;

    cout << "Please enter any other compensation you have received: (If none, type 0) \n$";
    cin  >> compensation;

    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;


}

    else if (grossPay <= 108600)
{


    medicareDeduction = grossPay * medicare;
	ssnDeduction = grossPay * ssnTax;
	finalPay = grossPay - (ssnDeduction + medicareDeduction);

	cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
	cout << "Employee Name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee Pay Status: " << empPaystatus<< "\n";
	cout << "Employee Salary: $" << salary<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee Compensation: $" << compensation<< "\n";
	cout << "Employee final pay: $" <<finalPay<< "\n";
}

    else if(grossPay >= 108600)
{

	medicareDeduction = grossPay * medicare;
	finalPay = grossPay - medicareDeduction;

	cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
	cout << "Employee Name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee Pay Status: " << empPaystatus<< "\n";
	cout << "Employee Salary: $" << salary<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee Compensation: $" << compensation<< "\n";
	cout << "Employee final pay: $" <<finalPay<< "\n";

}
///////////////////////////////////////////////////////////////////////////////////////


    if (hoursWorked >= 40)
{

    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;
    regularRate = totalRegularPay / hoursWorked;
    overtimeRate = regularRate * 0.5;
    overtimePay = (hoursWorked - 40) * overtimeRate;
    totalPay = totalRegularPay + overtimePay;


    cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
    cout << "Employee name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee pay Status: " << empPaystatus<< "\n";
	cout << "Employee hourly Rate: $" << hourlyRate<< "\n";
	cout << "Employee hours worked: $" << hoursWorked<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee compensation: $" << compensation<< "\n";
	cout << "Employee weekly pay: $" << hourlyPay<< "\n";
	cout << "Employee total pay: $" <<totalRegularPay<< "\n";
	cout << "Employee overtime pay: $" <<overtimePay<< "\n";
	cout << "Employee total pay with overtime: $" <<totalPay<< "\n";
}

    else if(hoursWorked <= 40)

{
    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;
    regularRate = totalRegularPay / hoursWorked;
    overtimeRate = regularRate * 0.5;
    overtimePay = (hoursWorked - 40) * overtimeRate;
    totalPay = totalRegularPay + overtimePay;


    cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
    cout << "Employee name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee pay Status: " << empPaystatus<< "\n";
	cout << "Employee hourly Rate: $" << hourlyRate<< "\n";
	cout << "Employee hours worked: $" << hoursWorked<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee compensation: $" << compensation<< "\n";
	cout << "Employee weekly pay: $" << hourlyPay<< "\n";
	cout << "Employee total pay: $" <<totalRegularPay<< "\n";
	cout << "Employee overtime pay: $" <<overtimePay<< "\n";
	cout << "Employee total pay with overtime: $" <<totalPay<< "\n";
}

return 0;
}


This post has been edited by xkaijinx: 19 December 2011 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

#4 Mr. House  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 30
  • Joined: 18-July 11

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 12:24 PM

Yes, some void functions would work nicely here to improve code readability and structure.
Also, try to make your variables more local; don't make them all globals.
Was This Post Helpful? 0
  • +
  • -

#5 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 09-March 11

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 12:27 PM

Could you digress? The whole "void" is confusing me. When I tried to put it in the code would not even compile, even though I was trying to follow the syntax. Really confusing!
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 665
  • View blog
  • Posts: 2,296
  • Joined: 31-December 10

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 12:30 PM

Post the code you have so we can see what you're talking about. It's definitely a good idea to get into breaking your program into smaller problems to solve. Then write functions for each small problem. A good piece of advice is to have a function do only one thing and to do it well.
Was This Post Helpful? 0
  • +
  • -

#7 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 09-March 11

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 12:35 PM

The code I am looking to break up using the void function is below..

#include <iostream>
#include <string>

using namespace std;

// DECLARED

int empSsn, salary, bonus, compensation, hourlyPay, hoursWorked, hourlyRate;
int totalRegularPay, overtimeRate, regularRate, overtimePay;
char empPaystatus;
string empName;
double grossPay, totalPay, ssnDeduction, medicareDeduction, finalPay;
const double ssnTax = .042;
const double medicare = .0145;

int main()
{

	cout << "Please enter Employee name: ";
	cin >> empName;

	cout << "Please enter employee Social Security Number: ";
	cin  >> empSsn;

	cout << "What is your current compensation status? \n";
	cout << "(Type 'E' for exempt or 'N' for nonexempt) ";

	cin >> empPaystatus;

// If statement below:

	if (empPaystatus == 'E')
{


	cout << "Please enter your salary: \n$";
	cin  >> salary;

	cout << "Please enter your bonus: \n$";
	cin >> bonus;

	cout << "Please enter any other compensation you have received: (If none, type 0) \n$";
	cin  >> compensation;

    grossPay = salary + bonus + compensation;
}

else if (empPaystatus == 'N')

{
    cout << "Please enter your hourly rate of pay \n$";
    cin  >> hourlyRate;

    cout << "Please enter the amount of hours worked \n";
    cin  >> hoursWorked;

    cout << "Please enter the amount received for your bonus \n$";
    cin  >> bonus;

    cout << "Please enter any other compensation you have received: (If none, type 0) \n$";
    cin  >> compensation;

    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;


}

    else if (grossPay <= 108600)
{


    medicareDeduction = grossPay * medicare;
	ssnDeduction = grossPay * ssnTax;
	finalPay = grossPay - (ssnDeduction + medicareDeduction);

	cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
	cout << "Employee Name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee Pay Status: " << empPaystatus<< "\n";
	cout << "Employee Salary: $" << salary<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee Compensation: $" << compensation<< "\n";
	cout << "Employee final pay: $" <<finalPay<< "\n";
}

    else if(grossPay >= 108600)
{

	medicareDeduction = grossPay * medicare;
	finalPay = grossPay - medicareDeduction;

	cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
	cout << "Employee Name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee Pay Status: " << empPaystatus<< "\n";
	cout << "Employee Salary: $" << salary<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee Compensation: $" << compensation<< "\n";
	cout << "Employee final pay: $" <<finalPay<< "\n";

}
///////////////////////////////////////////////////////////////////////////////////////


    if (hoursWorked >= 40)
{

    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;
    regularRate = totalRegularPay / hoursWorked;
    overtimeRate = regularRate * 0.5;
    overtimePay = (hoursWorked - 40) * overtimeRate;
    totalPay = totalRegularPay + overtimePay;


    cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
    cout << "Employee name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee pay Status: " << empPaystatus<< "\n";
	cout << "Employee hourly Rate: $" << hourlyRate<< "\n";
	cout << "Employee hours worked: $" << hoursWorked<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee compensation: $" << compensation<< "\n";
	cout << "Employee weekly pay: $" << hourlyPay<< "\n";
	cout << "Employee total pay: $" <<totalRegularPay<< "\n";
	cout << "Employee overtime pay: $" <<overtimePay<< "\n";
	cout << "Employee total pay with overtime: $" <<totalPay<< "\n";
}

    else if(hoursWorked <= 40)

{
    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;
    regularRate = totalRegularPay / hoursWorked;
    overtimeRate = regularRate * 0.5;
    overtimePay = (hoursWorked - 40) * overtimeRate;
    totalPay = totalRegularPay + overtimePay;


    cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
    cout << "Employee name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee pay Status: " << empPaystatus<< "\n";
	cout << "Employee hourly Rate: $" << hourlyRate<< "\n";
	cout << "Employee hours worked: $" << hoursWorked<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee compensation: $" << compensation<< "\n";
	cout << "Employee weekly pay: $" << hourlyPay<< "\n";
	cout << "Employee total pay: $" <<totalRegularPay<< "\n";
	cout << "Employee overtime pay: $" <<overtimePay<< "\n";
	cout << "Employee total pay with overtime: $" <<totalPay<< "\n";
}

return 0;
}


Was This Post Helpful? 0
  • +
  • -

#8 Mr. House  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 30
  • Joined: 18-July 11

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 12:46 PM

Here is reworked code that has been split into 2 large functions:

#include <iostream>
#include <string>

using namespace std;

// DECLARED

const double ssnTax = .042;
const double medicare = .0145;

void CompensationExempt(string empName, int empSsn, char empPaystatus);
void CompensationNonexempt(string empName, int empSsn, char empPaystatus);

int main()
{

	cout << "Please enter Employee name: ";
	string empName;
	cin >> empName;
	cin.ignore();

	cout << "Please enter employee Social Security Number: ";
	int empSsn;
	cin  >> empSsn;
	cin.ignore();

	cout << "What is your current compensation status? \n";
	cout << "(Type 'E' for exempt or 'N' for nonexempt) ";
	char empPaystatus;
	cin >> empPaystatus;
	cin.ignore();

// If statement below:

	if (empPaystatus == 'E')
{
	CompensationExempt(empName, empSsn, empPaystatus);
}
///////////////////////////////////////////////////////////////////////////////////////

	if (empPaystatus == 'N')
{
    CompensationNonexempt(empName, empSsn, empPaystatus);
}

return 0;
}

void CompensationExempt(string empName, int empSsn, char empPaystatus)
{
	double grossPay, ssnDeduction, medicareDeduction, finalPay;
	int salary, bonus, compensation;

	cout << "Please enter your salary: \n$";
	cin  >> salary;

	cout << "Please enter your bonus: \n$";
	cin >> bonus;

	cout << "Please enter any other compensation you have received: (If none, type 0) \n$";
	cin  >> compensation;

    grossPay = salary + bonus + compensation;

    if (grossPay <= 108600)
{

    medicareDeduction = grossPay * medicare;
	ssnDeduction = grossPay * ssnTax;
	finalPay = grossPay - (ssnDeduction + medicareDeduction);

	cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
	cout << "Employee Name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee Pay Status: " << empPaystatus<< "\n";
	cout << "Employee Salary: $" << salary<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee Compensation: $" << compensation<< "\n";
	cout << "Employee final pay: $" <<finalPay<< "\n";
}
    else if(grossPay >= 108600)
{


	medicareDeduction = grossPay * medicare;
	finalPay = grossPay - medicareDeduction;

	cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
	cout << "Employee Name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee Pay Status: " << empPaystatus<< "\n";
	cout << "Employee Salary: $" << salary<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee Compensation: $" << compensation<< "\n";
	cout << "Employee final pay: $" <<finalPay<< "\n";

}

}

void CompensationNonexempt(string empName, int empSsn, char empPaystatus){

	double totalPay;
	int bonus, compensation, hourlyPay, hoursWorked, hourlyRate;
	int totalRegularPay, overtimeRate, regularRate, overtimePay;

	cout << "Please enter your hourly rate of pay \n$";
    cin  >> hourlyRate;

    cout << "Please enter the amount of hours worked \n";
    cin  >> hoursWorked;

    cout << "Please enter the amount received for your bonus \n$";
    cin  >> bonus;

    cout << "Please enter any other compensation you have received: (If none, type 0) \n$";
    cin  >> compensation;

    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;



    if (hoursWorked >= 40)
{

    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;
    regularRate = totalRegularPay / hoursWorked;
    overtimeRate = regularRate * 0.5;
    overtimePay = (hoursWorked - 40) * overtimeRate;
    totalPay = totalRegularPay + overtimePay;


    cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
    cout << "Employee name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee pay Status: " << empPaystatus<< "\n";
	cout << "Employee hourly Rate: $" << hourlyRate<< "\n";
	cout << "Employee hours worked: $" << hoursWorked<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee compensation: $" << compensation<< "\n";
	cout << "Employee weekly pay: $" << hourlyPay<< "\n";
	cout << "Employee total pay: $" <<totalRegularPay<< "\n";
	cout << "Employee overtime pay: $" <<overtimePay<< "\n";
	cout << "Employee total pay with overtime: $" <<totalPay<< "\n";
}

    else if(hoursWorked <= 40)

{
    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;
    regularRate = totalRegularPay / hoursWorked;
    overtimeRate = regularRate * 0.5;
    overtimePay = (hoursWorked - 40) * overtimeRate;
    totalPay = totalRegularPay + overtimePay;


    cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
    cout << "Employee name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee pay Status: " << empPaystatus<< "\n";
	cout << "Employee hourly Rate: $" << hourlyRate<< "\n";
	cout << "Employee hours worked: $" << hoursWorked<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee compensation: $" << compensation<< "\n";
	cout << "Employee weekly pay: $" << hourlyPay<< "\n";
	cout << "Employee total pay: $" <<totalRegularPay<< "\n";
	cout << "Employee overtime pay: $" <<overtimePay<< "\n";
	cout << "Employee total pay with overtime: $" <<totalPay<< "\n";
}

}



As you can see, the main function is now easier to follow. This could be further broken down into functions that calculate the stats instead of putting them into such large functions.

I have also moved the variables out of being global and into local, except for the constants (which could also be moved).

This post has been edited by Mr. House: 19 December 2011 - 12:47 PM

Was This Post Helpful? 1
  • +
  • -

#9 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 09-March 11

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 12:57 PM

Thanks alot, I will study this to enhance my understanding.

Could you tell me if I understand correctly what you mean by moving the global and local variables?

The global variables were the ones which were originally at the top of the program. You moved those into the void function as a formal parameter and because of that they are considered local?
Was This Post Helpful? 0
  • +
  • -

#10 Mr. House  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 30
  • Joined: 18-July 11

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 01:08 PM

View Postxkaijinx, on 19 December 2011 - 12:57 PM, said:

Thanks alot, I will study this to enhance my understanding.

Could you tell me if I understand correctly what you mean by moving the global and local variables?

The global variables were the ones which were originally at the top of the program. You moved those into the void function as a formal parameter and because of that they are considered local?


Well, a variable is considered "local" when only certain portions of a program can access it. For example, in main() I have empName, empSsn and empPaystatus declared. Only main can use these variables (unless they are passed into other functions as parameters, like with the 2 void functions I created).

Likewise, I have 7 variables declared in CompensationExempt(). Main() or CompensationNonExempt() cannot use those variables unless they are passed or returned to them.

Example


//Incorrect
void Function1(){
 int i, j;
 i = 1;
 j = 1;
}

void Function 2(){
 j = 0; //variable j not declared in this scope
 i = 5; //variable i not declared in this scope
}

//Correct

void Function1(){
 int i, j;
 i = 1;
 j = 1;
 Function2(i, j);
}

void Function2(int i, int j){
  i = 0; //now in scope when Function1 is called
  j = 5; //now in scope when Function1 is called
}



Was This Post Helpful? 0
  • +
  • -

#11 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 665
  • View blog
  • Posts: 2,296
  • Joined: 31-December 10

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 01:10 PM

View PostMr. House, on 19 December 2011 - 02:46 PM, said:

Here is reworked code that has been split into 2 large functions:

#include <iostream>
#include <string>

using namespace std;

// DECLARED

const double ssnTax = .042;
const double medicare = .0145;

void CompensationExempt(string empName, int empSsn, char empPaystatus);
void CompensationNonexempt(string empName, int empSsn, char empPaystatus);

int main()
{

	cout << "Please enter Employee name: ";
	string empName;
	cin >> empName;
	cin.ignore();

	cout << "Please enter employee Social Security Number: ";
	int empSsn;
	cin  >> empSsn;
	cin.ignore();

	cout << "What is your current compensation status? \n";
	cout << "(Type 'E' for exempt or 'N' for nonexempt) ";
	char empPaystatus;
	cin >> empPaystatus;
	cin.ignore();

// If statement below:

	if (empPaystatus == 'E')
{
	CompensationExempt(empName, empSsn, empPaystatus);
}
///////////////////////////////////////////////////////////////////////////////////////

	if (empPaystatus == 'N')
{
    CompensationNonexempt(empName, empSsn, empPaystatus);
}

return 0;
}

void CompensationExempt(string empName, int empSsn, char empPaystatus)
{
	double grossPay, ssnDeduction, medicareDeduction, finalPay;
	int salary, bonus, compensation;

	cout << "Please enter your salary: \n$";
	cin  >> salary;

	cout << "Please enter your bonus: \n$";
	cin >> bonus;

	cout << "Please enter any other compensation you have received: (If none, type 0) \n$";
	cin  >> compensation;

    grossPay = salary + bonus + compensation;

    if (grossPay <= 108600)
{

    medicareDeduction = grossPay * medicare;
	ssnDeduction = grossPay * ssnTax;
	finalPay = grossPay - (ssnDeduction + medicareDeduction);

	cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
	cout << "Employee Name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee Pay Status: " << empPaystatus<< "\n";
	cout << "Employee Salary: $" << salary<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee Compensation: $" << compensation<< "\n";
	cout << "Employee final pay: $" <<finalPay<< "\n";
}
    else if(grossPay >= 108600)
{


	medicareDeduction = grossPay * medicare;
	finalPay = grossPay - medicareDeduction;

	cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
	cout << "Employee Name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee Pay Status: " << empPaystatus<< "\n";
	cout << "Employee Salary: $" << salary<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee Compensation: $" << compensation<< "\n";
	cout << "Employee final pay: $" <<finalPay<< "\n";

}

}

void CompensationNonexempt(string empName, int empSsn, char empPaystatus){

	double totalPay;
	int bonus, compensation, hourlyPay, hoursWorked, hourlyRate;
	int totalRegularPay, overtimeRate, regularRate, overtimePay;

	cout << "Please enter your hourly rate of pay \n$";
    cin  >> hourlyRate;

    cout << "Please enter the amount of hours worked \n";
    cin  >> hoursWorked;

    cout << "Please enter the amount received for your bonus \n$";
    cin  >> bonus;

    cout << "Please enter any other compensation you have received: (If none, type 0) \n$";
    cin  >> compensation;

    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;



    if (hoursWorked >= 40)
{

    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;
    regularRate = totalRegularPay / hoursWorked;
    overtimeRate = regularRate * 0.5;
    overtimePay = (hoursWorked - 40) * overtimeRate;
    totalPay = totalRegularPay + overtimePay;


    cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
    cout << "Employee name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee pay Status: " << empPaystatus<< "\n";
	cout << "Employee hourly Rate: $" << hourlyRate<< "\n";
	cout << "Employee hours worked: $" << hoursWorked<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee compensation: $" << compensation<< "\n";
	cout << "Employee weekly pay: $" << hourlyPay<< "\n";
	cout << "Employee total pay: $" <<totalRegularPay<< "\n";
	cout << "Employee overtime pay: $" <<overtimePay<< "\n";
	cout << "Employee total pay with overtime: $" <<totalPay<< "\n";
}

    else if(hoursWorked <= 40)

{
    hourlyPay = hourlyRate * hoursWorked;
    totalRegularPay = hourlyPay + bonus + compensation;
    regularRate = totalRegularPay / hoursWorked;
    overtimeRate = regularRate * 0.5;
    overtimePay = (hoursWorked - 40) * overtimeRate;
    totalPay = totalRegularPay + overtimePay;


    cout << "The employee data is displayed below: \n"; // SHOWS ALL OUTPUT BELOW
    cout << "Employee name: " << empName<< "\n";
	cout << "Employee SSN: " << empSsn<< "\n";
	cout << "Employee pay Status: " << empPaystatus<< "\n";
	cout << "Employee hourly Rate: $" << hourlyRate<< "\n";
	cout << "Employee hours worked: $" << hoursWorked<< "\n";
	cout << "Employee Bonus: $" << bonus<< "\n";
	cout << "Employee compensation: $" << compensation<< "\n";
	cout << "Employee weekly pay: $" << hourlyPay<< "\n";
	cout << "Employee total pay: $" <<totalRegularPay<< "\n";
	cout << "Employee overtime pay: $" <<overtimePay<< "\n";
	cout << "Employee total pay with overtime: $" <<totalPay<< "\n";
}

}



As you can see, the main function is now easier to follow. This could be further broken down into functions that calculate the stats instead of putting them into such large functions.

I have also moved the variables out of being global and into local, except for the constants (which could also be moved).

Just so you know in the future, don't just give the person the code. Explain to them what they need to do, and then let them try it out themselves and if they have any questions, they can post again. I know you're just trying to help, but you would be more of a help by letting the person do it on their own, and just answer their questions.

Onto the OP's question. When you declare variables outside of any function (including main), you're declaring those variables to be "global" to the whole program. So any function can access and change those variables. It's better to make variables "local" to a function (scope) than to make them "global". To make a variable "local", you just declare it inside of whatever function needs it. This way you can be sure nothing is messing up the values. To be able to access these variables in other functions, you just make the function take arguments, and pass the variables to the function.

This post has been edited by vividexstance: 19 December 2011 - 01:12 PM

Was This Post Helpful? 1
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 01:34 PM

Quote


void Function1(){
 int i, j;
 i = 1;
 j = 1;
 Function2(i, j);
}

void Function2(int i, int j){
  i = 0; //now in scope when Function1 is called
  j = 5; //now in scope when Function1 is called
}



Just a few comments, a variable's scope is the scope at which a handle has access to a specific place in memory. Passing variables by value, as you showed, creates different variables with different memory locations - you could call them copies.



void Function1(){
 int i, j; // i and j go out of scope at the end of this function
 i = 1;
 j = 1;
 Function2(i, j); //i and j are copied to different variables with their own scope
}

//i and j are copies of the parameters passed in Function1()
void Function2(int i, int j) //i and j are copies of the parameters passed in Function1() and 
{
  //this has no effect on variables in Function1()
  i = 0; 
  j = 5; 
}



To prevent the copying of memory when you pass parameters, you could pass variable by reference - or use a pointer (same thing).
void Function1(){
 int i, j; // i and j go out of scope at the end of this function
 i = 1;
 j = 1;
 Function2(i, j); //i and j are passed by reference, by memory location.
}

void Function2(int &i, int &j) //i and j both point to the memory defined in Function1()
{
  //this has effect on variables in Function1()
  i = 0; 
  j = 5; 
}


This post has been edited by Loundy: 19 December 2011 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#13 xkaijinx  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 09-March 11

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 03:56 PM

What does the cin.ignore() Mean?

Also with what you've seen from my top program could I put like "basic knowledge of C++ on resume" etc ? for like a junior debugger or just to have as not a requirement of a position but as a "good thing to have"
Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 04:01 PM

Have you tried typing cin.ignore in a search engine like Google?
Was This Post Helpful? 0
  • +
  • -

#15 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 665
  • View blog
  • Posts: 2,296
  • Joined: 31-December 10

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 04:11 PM

View Postxkaijinx, on 19 December 2011 - 05:56 PM, said:

What does the cin.ignore() Mean?

Also with what you've seen from my top program could I put like "basic knowledge of C++ on resume" etc ? for like a junior debugger or just to have as not a requirement of a position but as a "good thing to have"

I'm not trying to offend you, but by the fact that you had to ask what cin.ignore() means, then you don't have a basic knowledge of C++.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2