0

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 :(

Edited by brainfo: n/a

3
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by brainfo
0

But its not working

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

0

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 "

0

I did

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

Edited by brainfo: n/a

0

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();
}

Edited by Neon87: n/a

0
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

Edited by brainfo: n/a

0

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;
}

Edited by Neon87: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.