您的位置:首页 > 其它

使用new来为动态分配结构数组并赋值

2016-10-14 11:48 351 查看
// practice4-9.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int calorie;
};
int _tmain(int argc, _TCHAR* argv[])
{
int i;
CandyBar * ps =new CandyBar[3];
//为动态结构数组赋值
ps[0].brand="pizza hut";
ps[1].brand="pizza down";
cout<<"Enter ps[2] brand:";
getline(cin,ps[2].brand);
cout<<"Enter weight:";
cin>>ps[2].weight;
cout<<"Enter calorie:";
cin>>ps[2].calorie;
for (i=0;i<3;++i)
{
cout<<ps[i].brand<<endl;
}
delete [] ps;
system("pause");
return 0;
}
为new创建的动态结构数组赋值方法还是有问题!应该还有更简单的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: