牛骨文教育服务平台(让学习变的简单)
博文笔记

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

创建时间:2016-10-14 投稿人: 浏览次数:144
// 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创建的动态结构数组赋值方法还是有问题!应该还有更简单的
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。