C++Primer Plus(第六版) 第八章 第二题

本文展示了一个使用C++编写的简单程序,该程序定义了一个名为CandyBar的结构体,并通过两个函数setCandyBar和showCandyBar来设置和显示结构体的数据成员。在main函数中,通过调用这两个函数演示了如何初始化结构体变量并输出其内容。
如题
#include<iostream>
#include<cstring>

struct CandyBar
{
	char name[40];
	double weight;
	int heat;
};

void setCandyBar(CandyBar &candybar, const char *name = "Millennium Munch", const double weight = 2.85, const int heat = 350);
void showCandyBar(const CandyBar &candybar);

int main()
{
	using namespace std;
	CandyBar c1, c2;
	setCandyBar(c1);
	showCandyBar(c1);
	setCandyBar(c2, "Abc", 8.45, 8700);
	showCandyBar(c2);
	cin.get();
	return 0;
}

void setCandyBar(CandyBar & candybar, const char * name, const double weight, const int heat)
{
	strcpy_s(candybar.name, name);
	candybar.weight = weight;
	candybar.heat = heat;
}

void showCandyBar(const CandyBar & candybar)
{
	std::cout << "name:" << candybar.name << ",weight:" << candybar.weight << ",heat:" << candybar.heat << std::endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值