C++中关于getline()输入char数组问题~(已解决)
//创建一个汽车结构,并且输出输入的汽车名和制造年代 #include<iostream>
#include<string>
using namespace std;
struct car
{char name[20];
int year;
};
int main()
{int i;
cout<<"How many cars do you wish to catalog? ";
cin>>i;
car * pc=new car[i];
cout<<"Car #1: "<<endl;
cout<<"Please enter the make: ";
cin.getline(pc->name,20); //1处
cout<<"please enter the year made: ";
cin>>pc->year;//无法输入,直接跳到末尾。
cout<<"Car #2: "<<endl;
cout<<"please enter the make: ";
cin.getline((pc+1)->name,20);
cout<<"please enter the year made: ";
cin>>(pc+1)->year;
cout<<"Here is your collection; "<<endl;
cout<<pc->year<<" "<<pc->name<<endl;
cout<<(pc+1)->year<<" "<<(pc+1)->name<<endl;
delete [] pc;
return 0;
}
为什么我在1处输入了一行名字之后就什么都不能输入了,程序直接运行到结束?请各位前辈帮我改正正确~谢谢
加入cin.get()也无用,请前辈们帮我测试下~并且改正下,非常感谢~!
[[it] 本帖最后由 沿途有鬼 于 2008-7-22 18:31 编辑 [/it]]