// 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创建的动态结构数组赋值方法还是有问题!应该还有更简单的
使用new来为动态分配结构数组并赋值
最新推荐文章于 2024-05-29 22:35:05 发布