I dont know wats wrong with my code but what am trying to do is for salary its an addition of DA and HRA. Here is my code.

#include <iostream.h>

class teacher{
private:
      char name[20];
      char subject[10];
      float DA , HRA ;
      float salary;
public:
      void getdata();
      void display();
      int payrol();

     			};

   int teacher::payrol(){
      salary =  DA + HRA ;
               
return 0;
 };
   void teacher::getdata(){
   	cout << "Specify your name. \n";
       cin >> name ;
      cout << "Specify your subject.\n";
       cin >> subject ;
      cout << "Specify your DA (Daily Allowence).\n";
       cin >> DA ;
      cout << "Specify your HRA (Home Rent Allowence).\n";
       cin >> HRA;
   };

    void teacher::display(){
      cout << " Name    : " << name    << endl ;
      cout << " Subject : " << subject << endl ;
      cout << " DA      : " << DA      << endl ;
      cout << " HRA     : " << HRA     << endl ;
      cout << " Salary  : " << salary  << endl ;
    };
int main(){
	teacher st;

   st.getdata();
   st.display();

   cin.get();
   cin.ignore();
   return 0;
}

here every thing working fine except salary. I want salary as a sum of DA and HRA as i coded. But its not working :(

But its not working

Not helpful. How is it not working? What did you expect versus what it currently does?

Not helpful. How is it not working? What did you expect versus what it currently does?

If i input the value of DA and RHA as 100 and 2000 respectively i need value of salary as the sum of DA and RHA which shoud be 2100 but it shows different value like " 1.36521e- 0 "

That's because you don't call "payrol" function anywhere.

I did

int teacher::payrol(){
salary = DA + HRA ;}

I did

int teacher::payrol(){
salary = DA + HRA ;}

That's the function definition. For the function to evaluate salary, given the specified values of DA and HRA, you need to have a function call in main.

same as:

#include <iostream>
using namespace std;
 
int add(int x, int y)//function definition;
{
return x+y;
}

int main(){
	int x=4;
        int y=5;
        int z=add(x,y); //function call
		cout<<z<<endl;
		cin.get();
}
class teacher{
private:
char name[20];
char subject[10];
float DA , HRA ;
float salary;
public:
void getdata();
void display();
int payrol();
 
};
 
int teacher::payrol(){
salary = DA + HRA ;
 
return (salary);
void teacher::getdata(){
cout << "Specify your name. \n";
cin >> name ;
cout << "Specify your subject.\n";
cin >> subject ;
cout << "Specify your DA (Daily Allowence).\n";
cin >> DA ;
cout << "Specify your HRA (Home Rent Allowence).\n";
cin >> HRA;
};
 
void teacher::display(){
cout << " Name : " << name << endl ;
cout << " Subject : " << subject << endl ;
cout << " DA : " << DA << endl ;
cout << " HRA : " << HRA << endl ;
cout << " Salary : " << salary << endl ;
};

still problem i cannot get the real addition of DA and HRA for salary. I know that the following code is not working

int teacher::payrol(){
salary = DA + HRA ;
 
return (salary);
};

is there any code i need to add to solve

I have added the function call.

C++ Syntax (Toggle Plain Text)
#include <iostream.h>
 
class teacher{
private:
      char name[20];
      char subject[10];
      float DA , HRA ;
      float salary;
public:
      void getdata();
      void display();
      int payrol();
 
     			};
 
   int teacher::payrol(){
      salary =  DA + HRA ;
 
return 0;
 };
   void teacher::getdata(){
   	cout << "Specify your name. \n";
       cin >> name ;
      cout << "Specify your subject.\n";
       cin >> subject ;
      cout << "Specify your DA (Daily Allowence).\n";
       cin >> DA ;
      cout << "Specify your HRA (Home Rent Allowence).\n";
       cin >> HRA;
payrol();//->here you call payrol function to evaluate salary
   };
 
    void teacher::display(){
      cout << " Name    : " << name    << endl ;
      cout << " Subject : " << subject << endl ;
      cout << " DA      : " << DA      << endl ;
      cout << " HRA     : " << HRA     << endl ;
      cout << " Salary  : " << salary  << endl ;
    };
int main(){
	teacher st;
 
   st.getdata();
   st.display();
 
   cin.get();
   cin.ignore();
   return 0;
}

ohh thank you so much to find out my mistake :)

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.