C++中对象数组的问题

LA_Lai LA_Lai 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#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();
}
}

而在运行时,第二次输入却不能输入名字,为什么呢?
...全文
评论 1
收藏 已收藏
分享
返回顶部
LA_Lai
C++ 语言 65,212 成员 250,513 内容