为了账号安全,请及时绑定邮箱和手机立即绑定

C++输出字符数组乱码

C++输出字符数组乱码

C++
梦里花落0921 2018-08-06 11:04:38
```#include <iostream>using namespace std;struct CandyBar{char name[50];double weight;int hot;};void Set(CandyBar &candy, const char *c = "Milennium Munch", const double &w = 2.85, const int &h = 350);void Show(const CandyBar &candy);int main(){CandyBar candyOne, candyTwo;Set(candyOne);Set(candyTwo, "CandyTwo", 3.50, 285);Show(candyOne);Show(candyTwo);return 0;}void Set(CandyBar & candy, const char * c, const double & w, const int & h){int n = strlen(c);int i;for (i = 0; i < n; i++){candy.name[i] = c[i];}candy.name[i + 1] = '\0';candy.weight = w;candy.hot = h;}void Show(const CandyBar &candy){cout << candy.name << " " << candy.weight << " " << candy.hot << endl;}```最后打印出来的是Milennium Munch?只有这么一行,后面的东西也全都不见了,当我把Show()函数改成```void Show(const CandyBar &candy){cout << candy.name << endl << candy.weight << endl << candy.hot << endl;}```打印:Milennium Munch2.85350CandyTwo3.5285打印的便正常了这是怎么回事
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 1109 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信