编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习9
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
//string kind;
char kind[20];
float weight;
double calory;
};
void Display(CandyBar ca,int n)
{
cout<<"The kind of ca["<<n<<"] is: "<<ca.kind<<endl;
cout<<"The weight of ca["<<n<<"] is: "<<ca.weight<<endl;
cout<<"The calory of ca["<<n<<"] is: "<<ca.calory<<endl;
}
int main()
{
int size;
cout<<"Enter the size of the array: ";
cin>>size;
cin.get();
CandyBar *ca=new CandyBar[size];
//getline(cin,ca[0].kind);
cin.get(ca[0].kind,20).get();
ca[0].weight=200;
ca[0].calory=100;//getline(cin,ca[1].kind);//
cin.get(ca[1].kind,20).get();
ca[1].weight=205;
ca[1].calory=105;//getline(cin,ca[2].kind);//
cin.get(ca[2].kind,20).get();
ca[2].weight=210;
ca[2].calory=110;
Display(ca[0],0);
Display(ca[1],1);
Display(ca[2],2);
system("pause");
return 0;
}