2 Replies - 132 Views - Last Post: 18 September 2012 - 06:05 AM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Initializing an array of objects? String type can't be recognized?

Posted 17 September 2012 - 07:00 PM

According to the author, all the user needs to do is, enter the hours worked, and the employees gross pay will be calculated. Also string type can't seem to be recognized in the header.

The following was of no help: http://www.java2s.co...llitsmethod.htm

header:
#ifndef PayRoll_H 
#define PayRoll_H 
#include <string>
//using namespace std

class PayRoll
{ 
private:

	string name, department, position;
	int id;
	double hours, payRate;

public: 
	PayRoll()
	{
		name = " ";
		id = 0;
		department = " ";
		position = " ";
	}
	
	//~PayRoll ();

	// Set array elements to numArray's index
	void setName(string n)
	{	name = n;		}
	void setDep (string d)
	{	department = d;	}

	void setPos (string p)
	{	position = p;	}

	void setID (int i)
	{	
		id = i;
	}
		
	void setHours (double h)
	{	
		hours = h;
	}

	void setPay (double pr)
	{	
		payRate = pr;
	}
	
	//Accessor fucntions.
	string getName() const
	{	return name;		}

	string getDep() const
	{	return department;	}

	string getPos() const
	{	return position;	}

	int getID()	const
	{	return id;			}

	double getHours() const
	{	return hours;	}

	double getPay()
	{
		if (position == "Team lead")
			payRate = 25;
		else if (position == "Representative")
			payRate = 20;
		else
			payRate = 15;

		return payRate;
	}


	double getGrossPay () const
	{	return hours * payRate;	}


};
#endif 


main
#include <iostream> 
#include <string>
#include "PayRoll.h" 
using namespace  std ; 
int main() 
{ 
	const int Num_Emps = 4;
	int numbers;

	//string name, dep, pos;
	//int id;
	int hours, payRate;

	PayRoll Employee[Num_Emps] = {	{"John Doe", 10, "Technician", "Team lead"}
									{"Mary Smith", 12, "Human Resource", "Agent"}
									{"Karen Benton ", 14, "Human Resource", "Representative"}
									{"Shawn Grey", 16, "Technician", "Agent"}
	
	};
	
	cout << "Enter employee info: " << endl;
	for (int i = 0; i < Num_Emps; i++)
	{
		do
		{
		cout << "Hours worked by " << Employee[i].getName() << ": ";
		cin >> hours;
		} while (hours <= 60);
		Employee[i].setHours(hours);
	}

	cout << "Employee Earnings: \n";
	for (int i = 0; i < Num_Emps; i++)
	{
		cout << "Employee " << i+1 << " data:";
		cout << "\nName: " << Employee[i].getName();
		cout << "\nID  : " << Employee[i].getID();
		cout << "\nDepartment: " << Employee[i].getDep();
		cout << "\nPosition  : " << Employee[i].getPos();
		cout << "\n\nHours worked: " << Employee[i].getHours();
		cout << "\nGross Pay     : " << Employee[i].getGrossPay();
	}
	
	system("pause");
	return 0;
}

This post has been edited by mgrex: 17 September 2012 - 07:03 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Initializing an array of objects? String type can't be recognized?

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: Initializing an array of objects? String type can't be recognized?

Posted 17 September 2012 - 07:08 PM

You need to properly scope the string class. I recommend you use the scope resolution operator::. Everywhere you use string change it to std::string.

Jim
Was This Post Helpful? 2
  • +
  • -

#3 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Initializing an array of objects? String type can't be recognized?

Posted 18 September 2012 - 06:05 AM

Thanks, I've also figured out how to initialize the array objects, which required me to set up a constructor that consisted of arguments. As well as using the class name and parentheses instead of braces for the array initialization.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1