C++中对象数组的问题
#include <iostream>
#include <string>
using namespace std;
const int size=20;
class plorg
{
private:
char name[size];
int CI;
public:
void setCI(int c);
void get();
void showplorg() const;
plorg();
plorg(char *na,int c=20);
};
plorg::plorg()
{
strcpy_s(name,"Plorg");
name[size]='\0';
CI=0;
}
plorg::plorg(char *na,int c)
{
strncpy_s(name,na,size);
name[size]='\0';
CI=c;
}
void plorg::setCI(int c)
{
CI=c;
}
void plorg::showplorg() const
{
cout<<"plorg's name: "<<name<<" and CI is "<<CI<<endl;
}
int main()
{
plorg *plorgs[3];
char temp[size];
int c;
for(int i=0;i<3;i++)
{
cout<<"Enter plorg's name: ";
cin.getline(temp,size);
cout<<"Ener plorg's CI: ";
cin>>c;
plorgs[i] = new plorg(temp,c);
plorgs[i]->showplorg();
}
}
而在运行时,第二次输入却不能输入名字,为什么呢?