Including Defined functions, & issue with IF Statement

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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 - 06:38 PM

Ok maybe I sounded a bit off on that question. I am self studying C++ and have been using the main content in the chapters to run code.

What I should really have asked was...

I do not see the examples in my book using cin.ignore() in the examples it provides so I was wondering what use you guys have of it? Just gives a brief section on it.

It is defined as... "When you want to process only partial data you can use the stream function ignore to discard a portion of the input."

So for the code... are we discarding the pay status that the user input? Because later on in the code I output what the user chose. So I am confused about the use of this. I will be more clear next time

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

Was This Post Helpful? 0
  • +
  • -

#17 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 - 07:19 PM

Basically it removes the "Enter" character (/n) from the cin buffer than can interfere with future cins.
Was This Post Helpful? 0
  • +
  • -

#18 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 - 07:43 PM

Thanks all.

Now that we used the void function I created a do while statement. I wanted to make it repeat to go back to the beginning. I am just wondering where to insert this. I put the do{ right after the main and the while at the end but no luck. Then I tried it at each separation between the void functions and no go as well.

 
do{
		cout <<"Again?"; 
		cin >> response; 
	} while (response == 'y');

Was This Post Helpful? 0
  • +
  • -

#19 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 - 07:53 PM

You would have to put it around the entire contents of the main function (Not the function itself, but within the main function). Keep the prompt asking you to start over again (don't forget to declare a char variable). You could also add all possible responses like "Y", "yes" or "Yes".
Was This Post Helpful? 1
  • +
  • -

#20 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 - 08:18 PM

Thanks, I am able to loop the entire program now, however when I type anything other than 'Y' it continues to loop anyways. I see what you mean by putting it inside the main function, which worked. I did the same with the while loop as well you can see below, however it loops for everything now.


#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);
char response;


int main()
{

	do{
	
    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);
}
	cout <<"\n\nWould you like to enter additional data? (Type 'y' or 'Y'):\n"; 
    cin >> response;

	} while (response == 'y' | 'Y');
	system("pause");

	return 0; 


}


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

    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 Medicare Deduction: $" << medicareDeduction<< "\n";
	cout << "Employee SSN Deduction: $" << ssnDeduction<< "\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 Medicare Deduction: $" << medicareDeduction<< "\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 regular rate: $" << regularRate << "\n";
	cout << "Employee overtime rate: $" << overtimeRate<< "\n";
    cout << "Employee total pay: $" <<totalRegularPay<< "\n";
    cout << "Employee overtime pay (per hour): $" <<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;

    // SHOWS ALL OUTPUT BELOW
	cout << "The employee data is displayed below: \n"; 
    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 regular rate: $" << regularRate << "\n";
	cout << "Employee overtime rate: $" << overtimeRate<< "\n";
    cout << "Employee overtime pay (per hour): $" <<overtimePay<< "\n";
    cout << "Employee total pay with overtime: $" <<totalPay<< "\n";


}
	
}


Was This Post Helpful? 0
  • +
  • -

#21 jimblumberg  Icon User is online

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,303
  • Joined: 25-December 09

Re: Including Defined functions, & issue with IF Statement

Posted 19 December 2011 - 08:32 PM

On of the problems may be in the following snippet:
	} while (response == 'y' | 'Y');

In C/C++ you need to have complete comparisons on each side of your operators, also you are using the Bitwise OR when you should be using the Logical OR see this link for the C/C++ operators.
	} while (response == 'y' || response == 'Y');



Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2