I write a C++ program, which stores the information about of the company’s employees.
First I create a class named as Employee having following attributes.
1: id
2:name
3: sal
4: address
Take input of all the four attributes from the user using four different setter functions.
Class has following member functions.
1: display ()
2: checkmanger()
Member function display() will display all the records of employee using four different getter functions. Call all the four getter functions in this member function to display the records of employee.
Make a member function checkmanager() that will find out whether the employee is manager or not if he has salary greater than or equal to 15,000 then he will be considered as a manager and if his salary is less than 15,000 then he will be considered as programmer.
Where employee salary cannot be less than zero.
Write setters and getters for all the four data members to set and get values. Write also constructor for the class.
Also write destructor for the class Firm.
Sample Output
1)
Enter Employee id: 201
Enter Employee name: Akram
Enter Employee salary: 20000
Enter Employee address: Lahore
Employee id: 201
Employee name: Akram
Employee salary: 20000
Employee address: Lahore

The employee is manger.
2)
Enter Employee id: 402
Enter Employee name: Ali
Enter Employee salary: 10000
Enter Employee address: Lahore

Employee id: 402
Employee name: Ali
Employee salary: 10000
Employee address: Lahore

The employee is Programmer.

My program compiles but does not given required results. Please anybody checks, where is my mistake and corrects it. Or write this in a better way. I am new in c++ and not much concept of classes Thanks.

//Header Files
#include<iostream.h>                     
#include<conio.c>         

//class definition 
class Employee{
    //hidden part of the class Employee
    private:
    
       int id;
    
       char name;
    
       int sal;
    
       char address;

    public:
    
     void setid(int);
     void setname(char);
     void setsal(int);
     void setaddress(char);
   
     int getid();
     char getname();
     int getsal();
     char getaddress();
     //to input data
     void input();
     //to display data values
     void display();
     
     void checkmanger();

     int numberCheck(char temp[]);
    
     Employee();
     
     ~Employee();
       
         };
        
   
Employee::Employee()
    {
    id=0;
    name=0;
    sal=0;
    address=0;
    cout<<"\n \t \t     constructor called"<<endl;
    cout<<"\t \t   ===================== \n"<<endl;
    }

   
Employee::~Employee()
    {
    cout<<"\t \t The object has been destroyed"<<endl;
    cout<<"\t \t ============================ \n"<<endl;
    getch();
    }


  
void Employee::display()

    {
    input();
        
        cout<<"\t===================================================="<<"\n"<<endl;
        cout<<"\t Employee id:      "<<getid()<<"\n"<<endl;
        cout<<"\t Employee name:      "<<getname()<<"\n"<<endl;
        cout<<"\t Employee salary:      "<<getsal()<<"\n"<<endl;
        cout<<"\t Employee address:      "<<getaddress()<<"\n"<<endl;
          
      } 
       
   
void Employee::setid(int r)
    {
    id=r;
    }
   
              
int Employee::getid()
    {
    return id;
    }
      
void Employee::setname(char n)
    {
    name=n;
    }
     
char Employee::getname()
    {
    return name;
    }
      
void Employee::setsal(int s)
    {
    sal=s;
   }
   
int Employee::getsal()
    {
    return sal;
    }
 
void Employee::setaddress(char a)
    {
     address=a;
    }
    
    
char Employee::getaddress()
    {
    return address;
    }

void Employee::checkmanger()
    {
    int salary;
    if(salary<=0)
        cout<<"\t The employee is programmer"<<"\n"<<endl;
    else
        cout<<"\t The Employee managerer:        "<<salary<<"\n"<<endl;
      
    }

//main function  
void main()
    {
   // declaring object of the class Employee
     Employee Emp; 
     //calling function display
        Emp.display();
     //calling function checkmanger
        Emp.checkmanger();
      }

<< moderator edit: added code tags: [code][/code] >>

Dani AI

Generated

posted a good start and already fixed a couple of immediate bugs (removed the undefined call to input() inside display() and stopped using an uninitialized salary local variable). A few more, common C++ pitfalls remain and explain why the program prints wrong or unexpected results.

Key fixes and design notes:

  • Use std::string (or a char array) for name and address. A char holds only one character, not a whole name.
  • Implement input() to actually read values from the user. Do not call input from inside display(); call input() from main() and keep display() const and side-effect free.
  • checkManager() must use the member salary (e.g., sal), not an uninitialized local variable. Enforce sal >= 0 in the setter or input routine.
  • Avoid non-standard headers and calls (conio.h/getch()); prefer <iostream> and <string>. Use int main() and return 0; for portability.
  • Handle mixing operator>> and getline() by calling std::cin.ignore() after operator>> so input lines aren’t skipped.
  • Use a named constant for the manager threshold (e.g., const int MANAGER_THRESHOLD = 15000;) instead of a magic number.

Minimal, modern skeleton (illustrative—does not duplicate previous code):

#include <iostream>
#include <string>

class Employee {
private:
    int id = 0;
    std::string name;
    int sal = 0;
    std::string address;
public:
    void input();      // read id, name, sal, address (use std::getline where needed)
    void display() const;
    void checkManager() const;
    void setSalary(int s) { sal = (s < 0) ? 0 : s; }
};

Troubleshooting tips:

  • Test with boundary values (0, 14999, 15000).
  • Print the raw salary inside checkManager() while debugging.
  • Add input validation loops (repeat until a valid non-negative integer is entered).

Applying the fixes above will produce stable, correct output and make the class easier to extend.

Recommended Answers

All 2 Replies

I write a C++ program, which stores the information about of the company’s employees.
First I create a class named as Employee having following attributes.
1: id
2:name
3: sal
4: address
Take input of all the four attributes from the user using four different setter functions.
Class has following member functions.
1: display ()
2: checkmanger()
Member function display() will display all the records of employee using four different getter functions. Call all the four getter functions in this member function to display the records of employee.
Make a member function checkmanager() that will find out whether the employee is manager or not if he has salary greater than or equal to 15,000 then he will be considered as a manager and if his salary is less than 15,000 then he will be considered as programmer.
Where employee salary cannot be less than zero.
Write setters and getters for all the four data members to set and get values. Write also constructor for the class.
Also write destructor for the class Firm.
Sample Output
1)
Enter Employee id: 201
Enter Employee name: Akram
Enter Employee salary: 20000
Enter Employee address: Lahore
Employee id: 201
Employee name: Akram
Employee salary: 20000
Employee address: Lahore

The employee is manger.
2)
Enter Employee id: 402
Enter Employee name: Ali
Enter Employee salary: 10000
Enter Employee address: Lahore

Employee id: 402
Employee name: Ali
Employee salary: 10000
Employee address: Lahore

The employee is Programmer.

My program compiles but does not given required results. Please anybody checks, where is my mistake and corrects it. Or write this in a better way. I am new in c++ and not much concept of classes Thanks.

//Header Files
#include<iostream.h>                     
#include<conio.c>         

//class definition 
class Employee{
    //hidden part of the class Employee
    private:
    
       int id;
    
       char name;
    
       int sal;
    
       char address;

    public:
    
     void setid(int);
     void setname(char);
     void setsal(int);
     void setaddress(char);
   
     int getid();
     char getname();
     int getsal();
     char getaddress();
     //to input data
     void input();
     //to display data values
     void display();
     
     void checkmanger();

     int numberCheck(char temp[]);
    
     Employee();
     
     ~Employee();
       
         };
        
   
Employee::Employee()
    {
    id=0;
    name=0;
    sal=0;
    address=0;
    cout<<"\n \t \t     constructor called"<<endl;
    cout<<"\t \t   ===================== \n"<<endl;
    }

   
Employee::~Employee()
    {
    cout<<"\t \t The object has been destroyed"<<endl;
    cout<<"\t \t ============================ \n"<<endl;
    getch();
    }


  
void Employee::display()

    {
    input();
        
        cout<<"\t===================================================="<<"\n"<<endl;
        cout<<"\t Employee id:      "<<getid()<<"\n"<<endl;
        cout<<"\t Employee name:      "<<getname()<<"\n"<<endl;
        cout<<"\t Employee salary:      "<<getsal()<<"\n"<<endl;
        cout<<"\t Employee address:      "<<getaddress()<<"\n"<<endl;
          
      } 
       
   
void Employee::setid(int r)
    {
    id=r;
    }
   
              
int Employee::getid()
    {
    return id;
    }
      
void Employee::setname(char n)
    {
    name=n;
    }
     
char Employee::getname()
    {
    return name;
    }
      
void Employee::setsal(int s)
    {
    sal=s;
   }
   
int Employee::getsal()
    {
    return sal;
    }
 
void Employee::setaddress(char a)
    {
     address=a;
    }
    
    
char Employee::getaddress()
    {
    return address;
    }

void Employee::checkmanger()
    {
    int salary;
    if(salary<=0)
        cout<<"\t The employee is programmer"<<"\n"<<endl;
    else
        cout<<"\t The Employee managerer:        "<<salary<<"\n"<<endl;
      
    }

//main function  
void main()
    {
   // declaring object of the class Employee
     Employee Emp; 
     //calling function display
        Emp.display();
     //calling function checkmanger
        Emp.checkmanger();
      }

<< moderator edit: added code tags: [code][/code] >>

Shahid,

Search for the word "Change" in the following code to see my changes. Also, I added a mock definition for the input function.

Take care,
Bruce

//Header Files
#include<iostream.h> 
#include<conio.h>				// Changed from conio.c

//class definition 
class Employee{
//hidden part of the class Employee
private:

int id;

char name;

int sal;

char address;

public:

void setid(int);
void setname(char);
void setsal(int);
void setaddress(char);

int getid();
char getname();
int getsal();
char getaddress();
//to input data
void input();
//to display data values
void display();

void checkmanger();

int numberCheck(char temp[]);

Employee();

~Employee();

};


Employee::Employee()
{
id=0;
name=0;
sal=0;
address=0;
cout<<"\n \t \t constructor called"<<endl;
cout<<"\t \t ===================== \n"<<endl;
}


Employee::~Employee()
{
cout<<"\t \t The object has been destroyed"<<endl;
cout<<"\t \t ============================ \n"<<endl;
getch();
}



void Employee::display()

{
//input();				// Changed - omitted line because an input funtion is declared but not defined.

cout<<"\t===================================================="<<"\n"<<endl;
cout<<"\t Employee id: "<<getid()<<"\n"<<endl;
cout<<"\t Employee name: "<<getname()<<"\n"<<endl;
cout<<"\t Employee salary: "<<getsal()<<"\n"<<endl;
cout<<"\t Employee address: "<<getaddress()<<"\n"<<endl;

} 



void Employee::input()

{
//input();				// Changed - omitted line because an input funtion is declared but not defined.

//cout<<"\t===================================================="<<"\n"<<endl;
setid(100);
setname('G');
setsal(25000);
setaddress('M');

} 


void Employee::setid(int r)
{
id=r;
}


int Employee::getid()
{
return id;
}

void Employee::setname(char n)
{
name=n;
}

char Employee::getname()
{
return name;
}

void Employee::setsal(int s)
{
sal=s;
}

int Employee::getsal()
{
return sal;
}

void Employee::setaddress(char a)
{
address=a;
}


char Employee::getaddress()
{
return address;
}

void Employee::checkmanger()
{
//int salary;				// Changed - omitted this line
if(sal<=0)					// Changed - salary to sal
cout<<"\t The employee is programmer"<<"\n"<<endl;
else
cout<<"\t The Employee managerer: "<<sal<<"\n"<<endl;			// Changed - salary to sal

}

//main function 
void main()
{
// declaring object of the class Employee
Employee Emp; 
//calling function input
Emp.input();
//calling function display
Emp.display();
//calling function checkmanger
Emp.checkmanger();
}

<< moderator edit: added code tags: [code][/code] >>

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.