1. 程式人生 > >使用new為結構分配記憶體而不是宣告一個結構變數

使用new為結構分配記憶體而不是宣告一個結構變數

// practice4-8.cpp : 定義控制檯應用程式的入口點。
//

#include "stdafx.h"
#include <iostream>
const int len = 70;
struct pizza 
{
	char brand[len];
	double diameter;
	double weight;
};

int _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;
	pizza * ps = new pizza;
	cout<<"Enter pizza brand:";
	cin.getline(ps->brand,len);
	cout<<"Enter its diameter;";
	cin>>(*ps).diameter;
	cout<<"Enter its weight:";
	cin>>ps->weight;
	cout<<"****************************************"<<endl;
	cout<<ps->brand<<"\n"<<ps->diameter<<"\n"<<ps->weight<<endl;
	delete ps;
	system("pause");
	return 0;
}