Multiple object creation in C++

Weenis

I said WEENIS, not...
Joined
Apr 10, 2006
Messages
4,807
So I don't know C++ really, been looking to pick it up and convenient time the gf changed her major to CS and is learning C++.. well I know a heavily modified version of C and Java.. but I'm tired and can't seem to figure out the issue here.

Essentially you're supposed to create a console app that stores 3 pieces of information as "data members". First/Last names and monthly salary as strings/ints. Class should have constructor that initializes the three data members (I think this is the issue here. but my brain hurts and I'm tired). Provide a 'set' and 'get' function for each data member. if the monthly salary is not positive, set it to 0. Write a test program that demonstrates class Employee's capabilities.

Create two Employee objects and display each object's yearly salary. Then give each employee a 10 % raise and display each employee's yearly salary again.

I have it so everything works fine once, with the first employee, but when you have it ask for the second employee's names it jumps past the first name and then goes to enter last name and salary.. thats why I'm confused about it.

I'd appreciate if someone could point out the issue.. never touched C++ until last weekend for an hour or two..

CODE:

Code:
#include "stdafx.h"
#include <iostream>
#include <string> // program uses C++ standard string class


using namespace std;

class Employee // name of 
{

public:
	//function that sets the firstName of the Employee object
	 void setFirstName( string name )
	 {
	   firstName = name; //store the first name in the object
	 
	 }
	//function that gets the firstName of the Employee object
	 string getFirstName()
	 {
		 return firstName; //return the object's firstName

	 }

	//function that sets the lastName of the Employee object
	 void setLastName( string name )
	 {
	   lastName = name; //store the last name in the object
	 
	 }
	//function that gets the lastName of the Employee object
	 string getLastName()
	 {
		 return lastName; //return the object's firstName

	 }
	
	//function that sets the monthlySalary of the Employee object
	 void setMonthlySalary( int salary )
	 {
	 
      monthlySalary = salary; //store the monthlySalary in the object
	 
	 }

	//function that gets the monthlySalary of the Employee object
	 int getMonthlySalary()
	 {
		 return monthlySalary; //return the object's firstName

	 }

	 void displayMessage()
	 {
	  int yearlySalary = getMonthlySalary()*12;
	 //block of output to display Employee information
	cout<< "Employee First Name: " <<endl;
	cout<<"\n";
	cout<< getFirstName();
	cout<< "Employee Last Name: " <<endl;
	cout<<"\n";
	cout<< getLastName();
	cout<< "Employee Yearly Salary: " <<endl;
	cout<<"\n";
	cout<< yearlySalary;
	cout<<"\n";
	 
	 
	 }

	 	 void displayMessage2()
	 {

	  int yearlySalary = getMonthlySalary()*12;
	  double raisedSalary = yearlySalary *1.1;
	 //block of output to display Employee information
	cout<< "Employee First Name: " <<endl;
	cout<< getFirstName();
	cout<<"\n";
	cout<< "Employee Last Name: " <<endl;
	cout<< getLastName();
	cout<<"\n";
	cout<< "Employee Yearly Salary: " <<endl;
	cout<< raisedSalary;
	cout<<"\n";
	 
	 
	 }




private:
	string firstName; // first name of Employee
	string lastName; // last name of Employee
	int monthlySalary; //monthly salary of Employee
	int yearlySalary; //yearly salary of Employee
	double raisedSalary; //raised salary of employee

};


int main()
{
	
	string firstNameOfEmployee,firstNameOfEmployee2; // string to store the first name of the employee
	string lastNameOfEmployee,lastNameOfEmployee2; // string to store the last name of the employee
	int monthlySalary,monthlySalary2; //int to store the monthly salary of the employee

	Employee Employee_One; //creates an Employee object with the name Employee_One;
	Employee Employee_Two; //creates an Employee object with the name Employee_Two;

	//prompt for input and set first name of employee one
	cout<< "\n Please enter the first name for employee one:" << endl;
	getline( cin, firstNameOfEmployee); //read a first name of employee one with blanks
	Employee_One.setFirstName(firstNameOfEmployee); // sets the first name

	//prompt for input and set last name of employee one
	cout<< "\n Please enter the last name for employee one:" << endl;
	getline( cin, lastNameOfEmployee); //read a first name of employee one with blanks
	Employee_One.setLastName(lastNameOfEmployee); // sets the first name

	//prompt for input and set monthly salary of employee one
	cout<< "\n Please enter the monthly salary for employee one:" << endl;
	std::cin >> monthlySalary; //read a montly salary of employee one 
	Employee_One.setMonthlySalary(monthlySalary); // sets the first name

	//statements to test the class functionality by displaying results using get function after being set above
	
	cout<< "Employee One Information:" <<endl;
	Employee_One.displayMessage();
	
	//statement to give each employee ten percent raise and display their name and the raised salary
	cout<< "Employee One Information - Post 10% Raise:" <<endl;
	Employee_One.displayMessage2();


	//prompt for input and set first name of employee two
	cout<< "\n Please enter the first name for employee two:" << endl;
	getline( cin, firstNameOfEmployee2); //read a first name of employee one with blanks
	Employee_Two.setFirstName(firstNameOfEmployee2); // sets the first name

	//prompt for input and set last name of employee two
	cout<< "\n Please enter the last name for employee two:" << endl;
	getline( cin, lastNameOfEmployee2); //read a first name of employee one with blanks
	Employee_Two.setLastName(lastNameOfEmployee2); // sets the first name

	//prompt for input and set monthly salary of employee two
	cout<< "\n Please enter the monthly salary for employee two:" << endl;
	std::cin >> monthlySalary2; //read a montly salary of employee two 
	Employee_Two.setMonthlySalary(monthlySalary2); // sets the first name


	//statements to test the class functionality by displaying results using get function after being set above
	
	cout<< "Employee Two Information:" <<endl;
	Employee_Two.displayMessage();

	//statement to give each employee ten percent raise and display their name and the raised salary
	cout<< "Employee Two Information - Post 10% Raise:" <<endl;
	Employee_Two.displayMessage2();
	






	return 0;
}
 
After you read the monthly salary that is submitted by the user for employee one (std::cin >> monthlySalary), add the following line:

std::cin.ignore();

Why?
http://augustcouncil.com/~tgibson/tutorial/iotips.html

Basically, std::cin read the number into the monthly salary but left the newline character in the buffer, so by the time you get to asking for the first name of employee 2, boom, newline is there and read. So in essence, the newline character from the salary input is input into the first name of employee 2.

I tested your code with my additional line above, and it worked (was able to enter all info).
 
Back
Top