如题
#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;
}
本文展示了一个使用C++编写的简单程序,该程序定义了一个名为CandyBar的结构体,并通过两个函数setCandyBar和showCandyBar来设置和显示结构体的数据成员。在main函数中,通过调用这两个函数演示了如何初始化结构体变量并输出其内容。
 第八章 第二题&spm=1001.2101.3001.5002&articleId=74057027&d=1&t=3&u=bd33a2a68f4643269a6f012171ae7b1c)
815

被折叠的 条评论
为什么被折叠?



