C++Primer.plus第四章4.11编程练习第9题

编程练习6链接:

https://blog.csdn.net/qq_34847850/article/details/119509313?spm=1001.2014.3001.5501 

根据上面内容做题:

//4.11.9.cpp -- 使用new和数组、结构体
# include <iostream>
# include <string>

using namespace std;

struct CandyBar
{
	string kind;
	double weight;
	int calorie;
};

int main()
{
	CandyBar * snack = new CandyBar[3];
	snack[0].kind="Mocha Munch1";
	snack[0].weight=2.3;
	snack[0].calorie=350;
	snack[1].kind="Mocha Munch2";
	snack[1].weight=2.6;
	snack[1].calorie=300;
	snack[2].kind="Mocha Munch3";
	snack[2].weight=2.7;
	snack[2].calorie=450;
	cout<<"糖块的品牌1: "<<snack[0].kind<<"\n";
	cout<<"糖块的重量1: "<<snack[0].weight<<"\n";
	cout<<"糖块的卡路里1: "<<snack[0].calorie<<"\n";
	cout<<"糖块的品牌2: "<<snack[1].kind<<"\n";
	cout<<"糖块的重量2: "<<snack[1].weight<<"\n";
	cout<<"糖块的卡路里2: "<<snack[1].calorie<<"\n";
	cout<<"糖块的品牌3: "<<snack[2].kind<<"\n";
	cout<<"糖块的重量3: "<<snack[2].weight<<"\n";
	cout<<"糖块的卡路里3: "<<snack[2].calorie<<"\n";

	delete [] snack;

	return 0;
}
/*
输出结果:
糖块的品牌1: Mocha Munch1
糖块的重量1: 2.3
糖块的卡路里1: 350
糖块的品牌2: Mocha Munch2
糖块的重量2: 2.6
糖块的卡路里2: 300
糖块的品牌3: Mocha Munch3
糖块的重量3: 2.7
糖块的卡路里3: 450
Press any key to continue
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值