ToniaHe
2015-12-19 14:07
采纳率: 100%
浏览 2.0k
已采纳

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 14: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;

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • 念七 2015-12-19 14:51

    图片说明

    评论
    解决 无用
    打赏 举报
  • ToniaHe 2015-12-19 14:55

    #include
    #include
    using namespace std;
    int main()
    {
    struct CandyBar
    {
    char *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;
    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;
    

    }

    评论
    解决 无用
    打赏 举报
  • 0x5A52 2015-12-19 14:58

    P(x|c)=\frac{P(c|x)\cdot P(x)}{P(x)}

    
    
    评论
    解决 无用
    打赏 举报
  • 0x5A52 2015-12-19 15:02

    用memcpy给char数组赋值

    评论
    解决 无用
    打赏 举报
  • 一行白露 2015-12-20 02:05

    不懂耶,不过有时候字符串后面不加/0就可能出现乱码。

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题