i am working using visual c++ on windows 7. and getting the error error: Run-Time Check Failure #2 - Stack around the variable 'manager' was corrupted. what could be the solution ?
#include <iostream>
using namespace std;
class employee
{
char name[30];
float age;
public:
void getData(void);
void putData(void);
};
void employee ::getData(void)
{
cout<<"entr name";
cin>> name;
cout<<"entr age";
cin>>age;
}
void employee ::putData(void)
{
cout<<"name:"<< name<<endl;
cout<<"age"<<age<<endl;
}
const int size=3;
int main()
{
employee manager[size];
for(int i=0; i<size; i++)
{
cout <<endl<<"details of manager "<<i++<<endl;
manager[i].getData();
}
cout<<endl;
for(int i=0;i<size;i++)
{
cout<< endl << "manager "<< i++ << endl;
manager[i].putData();
}
getchar();
return 0;
}
employee manager[size];
for(int i=0; i<size; i++)
{
cout <<endl<<"details of manager "<<i++<<endl;
// PROBLEM IS HERE ----------->> ^^^^^
manager[i].getData();
}
When you increment i on the last iteration it becomes equal to the size and manager[i] is out of bounds. I think you meant not to increment i inside the loop, because you not only get out of bounds but also skip managers at odd positions. So here's what I suppose you intended:
for(int i=0; i<size; i++)
{
cout <<endl<<"details of manager "<<i<<endl;
manager[i].getData();
}
Same refers to the other loop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With