#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;
}
-
1Please describe the specific problem you facing – fukanchik Feb 20 '17 at 21:04
-
1`cin>>name[10];` => `cin>>name;` Ditto with `des` – Ed Heal Feb 20 '17 at 21:07
-
2The names of the arrays are "name" and "des". I recommend a visit to [the book list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Feb 20 '17 at 21:07
-
I wrote it on CodeBlocks IDE and it shows "Red" mark on left margin which means error.( Tbh I am not an experienced programmer pls help) – Tp25 Feb 20 '17 at 21:08
-
What happens if I enter 15 letters for my name? – Thomas Matthews Feb 20 '17 at 21:24
-
Answer: Use `std::string`. – Thomas Matthews Feb 20 '17 at 21:25
-
where to put the std::string @ThomasMatthews – Tp25 Feb 20 '17 at 21:35
2 Answers
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::string
s. 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.

- 476,176
- 80
- 629
- 1,111