编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习6

#include <iostream>
#include <string>
using namespace std;
struct car
{
 string pro;
 int  year;
};
int main()
{
 int num;
 cout<<"How many cars do you wish to catalog? ";
 (cin>>num).get();//注意将输入后的回车键转换成的换行符读取并丢弃
 car *newcar=new car[num];
 for(int i=0;i<num;i++)
 {
  cout<<"Car #"<<i+1<<":"<<endl;
  cout<<"Please enter the make: ";
  getline(cin,newcar[i].pro);
  cout<<"Please enter the year made: ";
  (cin>>newcar[i].year).get();//注意将输入后的回车键转换成的换行符读取并丢弃 
 cout<<"Here is your collection:"<<endl;
 for(int j=0;j<num;j++)
  cout<<newcar[j].year<<" "<<newcar[j].pro<<endl;
 delete [] newcar;//删除开辟的动态空间
 system("pause");
 return 0;
}

 

posted @ 2013-11-29 21:49  编程的爬行者  阅读(107)  评论(0编辑  收藏  举报