#include <iostream>
#include <string>
struct CandyBar
{
std::string name;
double weight;
int energy;
};
CandyBar & stinput(CandyBar &, std::string n = "Millennium Munch", double w = 2.85, int e = 350);
void stshow(CandyBar &);
int main()
{
using namespace std;
int energy;
double weight;
string name;
CandyBar candy;
stshow(stinput(candy));
cout << "输入品牌的名称" << endl;
getline(cin, name);
cout << "输入重量" << endl;
cin >> weight;
cout << "输入热量" << endl;
cin >> energy;
stshow(stinput(candy, name, weight, energy));
return 0;
}
CandyBar & stinput(CandyBar & C, std::string n, double w, int e)
{
C.name = n;
C.weight = w;
C.energy = e;
return C;
}
void stshow(CandyBar & C)
{
std::cout << "candy的品牌名称是 " << C.name << std::endl;
std::cout << "candy的重量是 " << C.weight << std::endl;
std::cout << "candy的热量是 " << C.energy << std::endl;
}
把函数定义中的std::string 换为char * 就是书中的要求,不过我习惯用string类来存储字符串