Task: Write a function called DisplayEmployeeRec() that takes EmployeeRec as a parameter and displays the value using the following format:
NAME:
ID:
PAYRATE:
HOURS WORKED:
GROSS PAY:

This is what I have so far:

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

using namespace std;

struct EmployeeRec
{
	string name;
	string empId;
	double payRate;
	double hours;
	double grossPay;
};

EmployeeRec GetEmployeeRec(); // function prototype
void DisplayEmployeeRec(EmployeeRec a, EmployeeRec b); //function prototype


int main()
{
	EmployeeRec A;
	EmployeeRec B;
	
	GetEmployeeRec();
	DisplayEmployeeRec(A,B);
	
	cin.get(); cin.get();
	return 0;
}

EmployeeRec GetEmployeeRec()
{	
	EmployeeRec A;
	EmployeeRec B;

	cout << fixed << showpoint << setprecision(2);

	cout << "Name please enter employee1 name: "<< endl;
	cin >> A.name;
	cout << "Enter Employee ID: " <<endl;
	cin >> A.empId;
	cout << "Enter the employee payrate: "; 
	cin >> A.payRate;
	cout << "Enter the hours worked by this employee: ";
	cin >> A.hours;
	A.grossPay = A.hours * A.payRate;
	cout << "Gross Pay: " << A.grossPay << endl << endl;
	
	
	cout << "Name please enter employee2 name: " << endl;
	cin >> B.name;
	cout << "Enter employee2 ID: " << endl;
	cin >> B.empId;
	cout << "Enter the employee2 payrate: ";
	cin >> B.payRate;
	cout << "Enter the hours worked by this employee: ";
	cin >> B.hours;
	B.grossPay = B.hours * B.payRate;
	cout << "Gross Pay: " << B.grossPay << endl;

	return(A,B);
	
}

void DisplayEmployeeRec(EmployeeRec a, EmployeeRec b)
{
	EmployeeRec A;
	EmployeeRec B;

	cout<<"Name: "<<A.name<<endl;
	cout<<"ID: "<<A.empId<<endl;
	cout<<"Pay Rate: "<<A.payRate<<endl;
	cout<<"Hours Worked: "<<A.hours<<endl;
	cout<<"Gross Pay: "<<A.grossPay<<endl;

	cout<<"Name: "<<B.name<<endl;
	cout<<"ID: "<<B.empId<<endl;
	cout<<"Pay Rate: "<<B.payRate<<endl;
	cout<<"Hours Worked: "<<B.hours<<endl;
	cout<<"Gross Pay: "<<B.grossPay<<endl;


}

I'm unsure if this is the correct way in writing this program. Please leave a comment.

From the next post please use code tags.

Well, By the code it seems like you have understood the usage of Struct's and How to handle structs.

But you seem to have become confused upon something known as "Scope or Resolution".

Consider the following Example.

#include <iostream>
using namespace std;

void func( );

int main ()
{
   int a =0;
  func();
cout<< a <<endl;
}

void func()
{
cout<< "Enter a Value ";
 cin>> a ;
}

The Above code would generate an error stating that 'a' is not declared.
This is because the declaration of variable 'a' is not in the scope of the function 'func'.

Now consider the second slightly edited version.

#include <iostream>
using namespace std;

void func( );

int main ()
{
   int a=0;
  func();
cout<< a <<endl;
}

void func()
{
  int a; //Declaration of a Local Variable.
cout<< "Enter a Value ";
 cin>> a ;
}

Now, this program would compile. But Even when you enter a non-zero value, The program would output 0, This is because cin>>a assigns the inputed value to the Local Variable and not to the variable in main();

So in order to circumvent this problem you can do 2 things.

Either declare the variable in Global ie: Outside any Function , Or Send Arguments as References.

Secondly, Now use the same thought and put thought on what exactly are you doing in your Display function.

From Which variable you are extracting values out from.

Edited 3 Years Ago by happygeek: fixed formatting

While I am still new at C++, it seems to me that for your GetEmployeeRec function, you're going to want to pass your EmployeeRec objects to the function as references. i.e.:

getEmployeeRec(EmployeeRec& A, EmployeeRec& B)

That way the function can directly manipulate the members of your objects. Also, you have defined A and B outside of the function, so by passing the references, you don't need to define them within the function.

-D

This article has been dead for over six months. Start a new discussion instead.