Hello
I'm new to programming in general and this was one of the assignments I did. I was suppose to create a class called employee that includes three pieces of information as data members - first name, last name, monthly salary. Then was suppose to write a test program that demonstrates class Employee's capabilities. SO create two Employee objects and display each object's yearly salary. Then give each Employee a 10 percent raise and display each Employee's yearly salary again.

Heres the .cpp code

#include<iostream>
#include "employee.h"
using namespace std;



Employee e1,e2;		// create two objects for class employee
char *gets();		// Function DEFINED to return a new string entered by the user
void print();		// Function to print the details of the 2 employees.

int main() // Main starts here
{			

int temp;			// temp variable to hold salary of employee


				// 1st employee details entered by user

cout <<"\n Enter the first name of 1st employee:: ";
e1.setName(gets());			
cout <<"\n Enter the last name of 1st employee:: ";
e1.setLastName(gets());
cout <<"\n Enter the salary of 1st employee:: ";
cin >>temp;
e1.setSalary(temp);

				// 2nd employee details entered by user

cout <<"\n Enter the first name of 2nd employee:: ";
e2.setName(gets());
cout <<"\n Enter the last name of 2nd employee:: ";
e2.setLastName(gets());
cout <<"\n Enter the salary of 2nd employee:: ";
cin >>temp;
e2.setSalary(temp);

				// OUTPUT STARTS

cout <<"\n\n\n \t\t\t:: Output starts here ::";

print();								// call function to print the details of the two employees

e1.setSalary((int)e1.getSalary()*1.1);		// Increment salary by 10%
e2.setSalary((int)e2.getSalary()*1.1);

cout <<"\n\n\n \t\t\t:: Salary Incremented ::";

print();								// Again print details after increment
return 0;	// finish main()
}

char *gets(){ 
char n[21]; 
scanf("%s",n); 
return n; 
}

void print(){
cout <<"\n Name of 1st employee: "<<e1.getName();
cout <<"\n Last Name of 1st employee: "<<e1.getLastName();
cout <<"\n Salary of 1st employee: "<<e1.getSalary()*12;
cout <<"\n\n Name of 2nd employee: "<<e2.getName();
cout <<"\n Last Name of 2nd employee: "<<e2.getLastName();
cout <<"\n Salary of 2nd employee: "<<e2.getSalary()*12 <<endl;

}

and heres the employee.h code

class Employee
{

private:
char *name,*lastname; 			// String of name and last name;
int salary;

public:

Employee()						// Constructor to initialize default values.
{
salary=0;
name="";
lastname="";
}

char *getName(){
return name;		// return pointer to the name;
}

void setName(char* n){
name=n;				//set name to the name passed in parameter.
}

char *getLastName(){
return lastname;	// return pointer to the lastname;
}

void setLastName(char* n){
lastname=n;			//set lastname to the name passed in parameter.
}

int getSalary(){
return salary;		// return salary from object.
}

void setSalary(int s){

if (s<0)			// check if salary is 0 or not.
	salary=0;
else
	salary=s;

	}


};

I didn't think I did anything wrong but when I run the program I get the screenshot posted. Is that something that is wrong with my code? Also I'm using Visual C++ IDE

Also hopefully this doesn't fall under your warning for homework help, I really want to learn this but don't know whats going on.

Recommended Answers

All 5 Replies

Hi
It is due you are trying to access a value pointed by pointer outside the scope.

#include<iostream>
#include "employee.h"
using namespace std;
 
 
 
Employee e1,e2;		// create two objects for class employee
char *gets(char*);		// Function DEFINED to return a new string entered by the user
void print();		// Function to print the details of the 2 employees.
 
int main() // Main starts here
{			
 
int temp;			// temp variable to hold salary of employee
char n[21]; 
 
				// 1st employee details entered by user
 
cout <<"\n Enter the first name of 1st employee:: ";
e1.setName(gets(n));			
cout <<"\n Enter the last name of 1st employee:: ";
e1.setLastName(gets(n));
cout <<"\n Enter the salary of 1st employee:: ";
cin >>temp;
e1.setSalary(temp);
 
				// 2nd employee details entered by user
 
cout <<"\n Enter the first name of 2nd employee:: ";
e2.setName(gets(n));
cout <<"\n Enter the last name of 2nd employee:: ";
e2.setLastName(gets(n));
cout <<"\n Enter the salary of 2nd employee:: ";
cin >>temp;
e2.setSalary(temp);
 
				// OUTPUT STARTS
 
cout <<"\n\n\n \t\t\t:: Output starts here ::";
 
print();								// call function to print the details of the two employees
 
e1.setSalary((int)e1.getSalary()*1.1);		// Increment salary by 10%
e2.setSalary((int)e2.getSalary()*1.1);
 
cout <<"\n\n\n \t\t\t:: Salary Incremented ::";
 
print();								// Again print details after increment
return 0;	// finish main()
}
 
char *gets(char* n){ 
scanf("%s",n); 
return n; // return pointer 
}
 
void print(){
cout <<"\n Name of 1st employee: "<<e1.getName();
cout <<"\n Last Name of 1st employee: "<<e1.getLastName();
cout <<"\n Salary of 1st employee: "<<e1.getSalary()*12;
cout <<"\n\n Name of 2nd employee: "<<e2.getName();
cout <<"\n Last Name of 2nd employee: "<<e2.getLastName();
cout <<"\n Salary of 2nd employee: "<<e2.getSalary()*12 <<endl;
 
}

Please try to make your program more modular and use OOP rules. Outside functions should not access your class member data etc.

If I were you, I'd avoid using char arrays ... what happened to string?

//Employee.h
//
//
#include<iostream>
#include<string>
using namespace std;
      class Employee
      {
      private:
      string name,lastname; // String of name and last name;
      int salary;
      public:
      Employee() // Constructor to initialize default values.
      {
      salary=0;
      name="";
      lastname="";
      }
      string getName(){
      return name; // return pointer to the name;
      }
      void setName(string n){
      name=n; //set name to the name passed in parameter.
      }
      string getLastName(){
      return lastname; // return pointer to the lastname;
      }
      void setLastName(string n){
      lastname=n; //set lastname to the name passed in parameter
      }
      int getSalary(){
      return salary; // return salary from object.
	  }
      void setSalary(int s){
      if (s<0) // check if salary is 0 or not.
      salary=0;
      else
      salary=s;
      }
      };

//main.cpp
//
//
#include<iostream>
      #include "employee.h"
      #include<string>
      using namespace std;
       
       
       
      Employee e1,e2; // create two objects for class employee
      string gets(); // Function DEFINED to return a new string entered by the user
      void print(); // Function to print the details of the 2 employees.
       
      int main() // Main starts here
      {
      
      int temp; // temp variable to hold salary of employee
       
       
      // 1st employee details entered by user
       
      cout <<"\n Enter the first name of 1st employee:: ";
      e1.setName(gets());
      cout <<"\n Enter the last name of 1st employee:: ";
      e1.setLastName(gets());
      cout <<"\n Enter the salary of 1st employee:: ";
      cin >>temp;
      e1.setSalary(temp);
       
      // 2nd employee details entered by user
       
      cout <<"\n Enter the first name of 2nd employee:: ";
      e2.setName(gets());
      cout <<"\n Enter the last name of 2nd employee:: ";
      e2.setLastName(gets());
      cout <<"\n Enter the salary of 2nd employee:: ";
      cin >>temp;
      e2.setSalary(temp);
  
      
      // OUTPUT STARTS
       
      cout <<"\n\n\n \t\t\t:: Output starts here ::";
       
      print(); // call function to print the details of the two employees
      
      e1.setSalary((int)e1.getSalary()*1.1); // Increment salary by 10%
      e2.setSalary((int)e2.getSalary()*1.1);
      
      cout <<"\n\n\n \t\t\t:: Salary Incremented ::";
       
      print(); // Again print details after increment
      return 0; // finish main()
      }
       
      string gets(){
      string n;
      cin>>n;
      return n;
      }
      void print(){
      cout <<"\n Name of 1st employee: "<<e1.getName();
      cout <<"\n Last Name of 1st employee: "<<e1.getLastName();
      cout <<"\n Salary of 1st employee: "<<e1.getSalary()*12;
      cout <<"\n\n Name of 2nd employee: "<<e2.getName();
      cout <<"\n Last Name of 2nd employee: "<<e2.getLastName();
      cout <<"\n Salary of 2nd employee: "<<e2.getSalary()*12 <<endl;
      }

Hope this helped!!

commented: Agreed. +7
commented: i'm aware this is really old... but why did you multiply by 1.1 instead of 0.1 (for 10% == 0.1 right?) +0

>It is due you are trying to access a value pointed by pointer outside the scope.

So... did you bother testing your code before posting it? Sure it prints now - but perhaps pointing member variables to the same memory location throughout the entire program isn't the best idea (i.e., char n[21]).


@o/p - With the suggestions from the previous post, you should see how adding a few more variables to your program should fix things up. However, keep in mind that changing the values of your main() variables (assuming you'll be doing char arrays) will in fact change the value of your members.

E.g.,

//pseudo
char temp[10];

cout <<"\n Enter the first name of 1st employee:: ";
e1.setName(gets(n));			
cout <<"\n Enter the last name of 1st employee:: ";
e1.setLastName(gets(n));

Sample Input:
Billy
Bob

The above code will indeed change your employee's last name to Bob, but will also change your first name to Bob.


edit> I have to agree with tkud. Much cleaner than trying to use char[]

Thank you all for your help!

I was char* because I saw I couple of examples using this. So now I'm wondering when should I use char* as opposed to std::string? Everything is now using string, and it works great now. Anyone know why char* was bugging out?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.