创建结构candybar,要求用new来动态分配结构数组
2009-10-15 18:22
441 查看
#include <iostream>
struct CandyBar
{
char kind[20];
float weight;
int cal;
};
int main()
{
using namespace std;
int size;
cout<<"the size is: ";
cin>>size;//数组的元素数
cin.get();
CandyBar *ps=new CandyBar[size];//创建一个动态数组
//从键盘读取各项的值.....
for(int i=0;i<size;i++)
{
cout<<"Enter the kind: ";
cin.getline(ps[i].kind,20);
//如果不想从键盘读取kind的值,可使用strcpy(ps[i].kind,"kindname");直接对kind赋值.使用ps[i].kind="kindname";会报错!!
cout<<"Enter the weight: ";
cin>>ps[i].weight;
cin.get();
cout<<"Enter the cal: ";
cin>>ps[i].cal;
cin.get();
cout<<"ps["<<i<<"].kind:"<<ps[i].kind<<" "<<endl;
cout<<"ps["<<i<<"].weight:"<<ps[i].weight<<" "<<endl;
cout<<"ps["<<i<<"].cal:"<<ps[i].cal<<" "<<endl;
}
return 0;
}
struct CandyBar
{
char kind[20];
float weight;
int cal;
};
int main()
{
using namespace std;
int size;
cout<<"the size is: ";
cin>>size;//数组的元素数
cin.get();
CandyBar *ps=new CandyBar[size];//创建一个动态数组
//从键盘读取各项的值.....
for(int i=0;i<size;i++)
{
cout<<"Enter the kind: ";
cin.getline(ps[i].kind,20);
//如果不想从键盘读取kind的值,可使用strcpy(ps[i].kind,"kindname");直接对kind赋值.使用ps[i].kind="kindname";会报错!!
cout<<"Enter the weight: ";
cin>>ps[i].weight;
cin.get();
cout<<"Enter the cal: ";
cin>>ps[i].cal;
cin.get();
cout<<"ps["<<i<<"].kind:"<<ps[i].kind<<" "<<endl;
cout<<"ps["<<i<<"].weight:"<<ps[i].weight<<" "<<endl;
cout<<"ps["<<i<<"].cal:"<<ps[i].cal<<" "<<endl;
}
return 0;
}
相关文章推荐
- 使用new来为动态分配结构数组并赋值
- c++ primer plus 习题5.7(使用new为结构创建动态数组)
- 用vector和string代替new动态分配的数组
- 关于[]静态数组和new分配的动态数…(转)
- C++中关于函数内静态数组和new分配的动态数组的区别分析
- Android JNI编程(五)——C语言的静态内存分配、动态内存分配、动态创建数组
- new 动态分配数组空间
- C++笔记之使用new动态分配多维数组
- C++:多维数组的动态分配(new)和释放(delete)
- 1.5 使用new创建动态结构和自动, 静态, 动态三种存储方式
- new 动态分配数组空间 .xml
- C++中关于[]静态数组和new分配的动态数组的区别分析
- C++中关于[]静态数组和new分配的动态数组的区别分析
- new 动态分配数组空间
- C++:多维数组的动态分配(new)和释放(delete)
- 结构struct动态数组创建、操作、删除
- 结构struct动态数组创建与操作
- 哪位大哥知道怎么用new动态分配一个指针数组?
- C++中关于[]静态数组和new分配的动态数组的区别分析
- C++中关于[]静态数组和new分配的动态数组的区别分析