首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++中对象数组的有关问题

2012-10-08 
C++中对象数组的问题#include iostream#include stringusing namespace stdconst int size20class

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

而在运行时,第二次输入却不能输入名字,为什么呢?

[解决办法]
fflush(stdin);
cin.getline(temp,size);

热点排行