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.