ToniaHe 2015-12-19 06:07 采纳率: 100%
浏览 2153
已采纳

C++动态数组输出乱码的问题

为何输出成员brand是乱码“屯”?
#include
#include
using namespace std;
int main()
{
struct CandyBar
{
char brand[20];
float weight;
int calorie;
};

CandyBar *test=new CandyBar[3];
test[0].brand =="The First";
test[0].weight=5;
test[0].calorie=136;
test[1].brand =="The Second";
test[1].weight=10;
test[1].calorie=350;
test[2].brand=="The Third";
test[2].weight=15;
test[2].calorie=333;
cout<<"The Brand: "<<test[0].brand;
cout<<", "<<test[1].brand;
cout<<", "<<test[2].brand<<endl;
cout<<endl;
cout<<"The Weight: "<<test[0].weight;
cout<<", "<<test[1].weight;
cout<<", "<<test[2].weight<<endl;
cout<<endl;
cout<<"The Calorie: "<<test[0].calorie;
cout<<", "<<test[1].calorie;
cout<<", "<<test[2].calorie;
cin.get();
cin.get();
return 0;
}

展开全部

  • 写回答

6条回答 默认 最新

  • 念七 2015-12-19 06:12
    关注

    struct CandyBar
    {
    string brand;
    float weight;
    int calorie;
    };

    CandyBar *test=new CandyBar[3];
    test[0].brand ="The First";
    test[0].weight=5;
    test[0].calorie=136;
    test[1].brand ="The Second";
    test[1].weight=10;
    test[1].calorie=350;
    test[2].brand="The Third";
    test[2].weight=15;
    test[2].calorie=333;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部