美文网首页
C++ primer plus 第八章编程练习2答案

C++ primer plus 第八章编程练习2答案

作者: ticks | 来源:发表于2018-06-06 18:28 被阅读0次
    #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类来存储字符串

    相关文章

      网友评论

          本文标题:C++ primer plus 第八章编程练习2答案

          本文链接:https://www.haomeiwen.com/subject/phlisftx.html