C++ Primer Plus第五版 第四章编程习题 第9题 写的程序错误 新手求解~~
#include <iostream>struct CandyBar {
char brand[40];
double weight;
int calories;
};
int main()
{
using namespace std;
CandyBar *snack= new CandyBar[3];
snack[0] = { "Mocha Munch", 2.3, 350 };
snack[1] = { "shijin", 3.3, 450 };
snack[2] = { "xu fuji", 4.3, 550 };
cout << snack[0].brand <<" "<< snack[0].weight <<" "<< snack[0].calories << endl;
cout << snack[1].brand <<" "<< snack[1].weight <<" "<< snack[1].calories << endl;
cout << snack[2].brand <<" "<< snack[2].weight <<" "<< snack[2].calories << endl;
delete [] snack;
return 0;
}
要求用new来动态分配数组