C++primer plus的一道题,为什么运行时老跳过cin.getline?

qq_17791449 2014-07-17 12:56:00
设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存储在字符数组中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。接下来,程序提示用户输入每辆车的生产商(可能有多个单词组成)和年份的信息。请注意,这需要特别小心,因为它将交替读取数值和安符串。最后,程序将显示每个结构的内容。该程序运行情况如下(2,Hudson Hornet,1952,Kaiser,1951,是要用户输入的,最后2行是程序回显):
How many cars do you wish to catalog? 2
Car #1:
Please enter the maker: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the maker: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser


代码如下:
#include <iostream>
using namespace std;
struct car
{
char producer[20];
int year;
};
int main()
{
int n,i;
cout<<"How many cars do you wish to catalog?";
cin>>n;
car* p=new car [n];
for(i=0;i<n;i++)
{
cout<<"Car #"<<i+1<<endl<<"Please enter the make:";
cin.getline(p[i].producer,20);
cout<<endl<<"Please enter the year made:";
cin>>(p+i)->year;
};
cout<<endl<<"Here is your collection:";
for(i=0;i<n;i++)
cout<<endl<<p[i].year<<" "<<(p+i)->producer;
delete [] p;
return 0;
}

用的VC++6.0,每次运行的时候cin.getline(p[i].producer,20);都没有起作用,直接跳过。改成cin>>的话就不会这样。请教各位大神,问题究竟出在哪儿了??
...全文
492 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
CPPWorld 2017-03-10
  • 打赏
  • 举报
回复
在循环体最后加一句cin.ignor(100,'\n');/*清除缓冲区里面的\n*/
  • 打赏
  • 举报
回复
cout << "Car #1:" << endl; cout << "Please enter the make: "; cin.getline(ps->manufacturer, 20); cout << "Please enter the made: "; cin >> (*ps).year; cout << "Car #2:" << endl; cout << "Please enter the make:"; cin.get(); cin.getline((ps+1)->manufacturer, 20); cout << "Please enter the made: "; cin >> (*(ps+1)).year;
  • 打赏
  • 举报
回复
前一个为什么不用cin.get();
qq_17791449 2014-07-17
  • 打赏
  • 举报
回复
引用 2 楼 Cnwanglin 的回复:
char c[10]; cin.getline( &c[0], 5); cout << c << endl; 注意 回车键也占用一个字符
谢了!只需在for的开头加入cin.get()跳过回车就好了
赵4老师 2014-07-17
  • 打赏
  • 举报
回复
乍看起来c++的cin、cout在输入、输出上比c的scanf、printf简单,不用格式控制符! 但是不用格式控制符,输入输出恰好是你期望的格式的时候好说;等到输入输出不是你期望的格式的时候,你就会觉得还是用格式控制符更方便、更靠谱。 摒弃cin、cout! 使用scanf、printf。
Cnwanglin 2014-07-17
  • 打赏
  • 举报
回复
basic_istream::getline Gets a line from the input stream. basic_istream<Elem, Tr>& getline( char_type *_Str, streamsize _Count ); basic_istream<Elem, Tr>& getline( char_type *_Str, streamsize _Count, char_type _Delim ); basic_istream<Elem, Tr>& getline( char_type *_Str, streamsize _Count ); basic_istream<Elem, Tr>& getline( char_type *_Str, streamsize _Count, char_type _Delim ); Parameters_Count The number of characters to read from strbuf. _Delim The character that should terminate the read if it is encountered before _Count. _Str A string in which to write. Return ValueThe stream ( *this). RemarksThe first of these unformatted input functions returns getline(_ Str, _Count, widen(' \n')). The second function extracts up to _Count - 1 elements and stores them in the array beginning at _ Str. It always stores the string termination character after any extracted elements it stores. In order of testing, extraction stops: At end of file. After the function extracts an element that compares equal to _Delim, in which case the element is neither put back nor appended to the controlled sequence. After the function extracts _Count - 1 elements. If the function extracts no elements or _Count - 1 elements, it calls setstate( failbit). In any case, it returns *this.
Cnwanglin 2014-07-17
  • 打赏
  • 举报
回复
char c[10]; cin.getline( &c[0], 5); cout << c << endl; 注意 回车键也占用一个字符
Cnwanglin 2014-07-17
  • 打赏
  • 举报
回复
cin.getline(char ch[],size)是cin 的一个成员函数,定义在<iostream>中,用于输入行指定size的字符串,以enter结束。若输入长度超出size,则不再接受后续的输入。

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧