-5
#include<iostream>
using namespace std;
class emp
{
    public:
                int en;
                char name[10],des[10];
void get()
{
    cout<<"enter emp no.";
    cin>>en;
    cout<<"enter emp name";
    cin>>name;
    cout<<"enter designation";
    cin>>des;
}
};
class salary : public emp
{
public:
    float bp,hra,da,pf,np;
    void get1()
    {
    cout<<"enter basic pay";
    cin>>bp;
    cout<<"enter domestic allowance";
    cin>>da;
    cout<<"enter profit fund";
    cin>>pf;
    cout<<"enter human resource admittance";
    cin>>hra;
    }
    void calculate()
    {
        np=bp+da+hra-pf;
    }

    void display()
    {
        cout<<en<<"\t"<<name<<"\t"<<des<<"\t"<<da<<"\t"<<pf<<"\t"<<np<<"\n";

    }
    };

    int main()
    {
    salary s[10];
    int i,n;
    char ch;
    cout<<"enter the no. of employees";
    cin>>n;
    for(i=0;i<=n;i++)
    {
     s[i].get();
     s[i].get1();
     s[i].calculate();
    }
    cout<<"\n eno. \t ename \t des \t bp \t hra \t da \t pf \t np \n";
    for(i=0;i<=n;i++)
    {
     s[i].display();
    }
    return 0;
    } 
halfer
  • 19,824
  • 17
  • 99
  • 186
Tp25
  • 9
  • 2

2 Answers2

2

cin>>des[10]; reads one (single) character for standard input, and attempts to write it to des[10]. Unfortunately, you've defined des as having 10 characters, so only des[0] through des[9] are valid, so when it attempts to write to des[10], you get undefined behavior.

My guess is that you probably wanted something more like:

cin.getline(des, 10);

This attempts to read a maximum of 10 characters from cin, and write them to des (and assures that it's NUL terminated).

The same, of course, applies to name.

Once you're done with that, you probably want to forget all of the above, define both name and des as std::strings. Then you can use std::getline(std::cin, name);. With this you don't have to specify a maximum size; the string will expand to hold as much as the user enters.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

instead of name[10] and des[10], use cin >> name and cin >> des

user3683297
  • 156
  • 1
  • 8