| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦   
共有 380 人关注过本帖
标题:C++中关于getline()输入char数组问题~(已解决)
收藏  订阅  推荐  打印
沿途有鬼
Rank: 2
等级:注册会员
帖子:68
积分:882
注册:2008-7-20
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()也无用,请前辈们帮我测试下~并且改正下,非常感谢~!

[ 本帖最后由 沿途有鬼 于 2008-7-22 18:31 编辑 ]
2008-7-20 21:32
踏魔狼
Rank: 12Rank: 12Rank: 12
等级:贵宾
帖子:1289
积分:13340
威望:22
注册:2005-9-22

在cin.getline(pc->name, 20);
之前加上
cin.clear();
cin.ignore(1024, '\n');

=×&D o I p R e E n C g T l X&×=
2008-7-21 01:50
沿途有鬼
Rank: 2
等级:注册会员
帖子:68
积分:882
注册:2008-7-20

引用:
踏魔狼 在 2008-7-21 01:50 的发言:

在cin.getline(pc->name, 20);
之前加上
cin.clear();
cin.ignore(1024, '\n');
#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.clear();
cin.ignore(1024, '\n');
cin.getline(pc->name,20);
cout<<"please enter the year made: ";
cin>>pc->year;
cout<<"Car #2: "<<endl;
cout<<"please enter the make: ";
cin.clear();
cin.ignore(1024, '\n');

cin.getline((pc+1)->name,20);


cout<<"please enter the year made: ";
cin>>(pc+1)->year;
cout<<"Here is your collection; "<<endl;
cin.ignore();
cout<<pc->year<<" "<<pc->name<<endl;
cout<<(pc+1)->year<<" "<<(pc+1)->name<<endl;
delete [] pc;
return 0;
}
非常感谢你的回答,由于我C++学习的还是很前面,请问你能帮我解释下为什么要在1之前加上2和3吗?由其是cin.ignore(1024, '\n')中的1024参数不懂~
cin.clear();//2
cin.ignore(1024, '\n');//3
cin.getline(pc->name,20);//1
2008-7-21 13:03
踏魔狼
Rank: 12Rank: 12Rank: 12
等级:贵宾
帖子:1289
积分:13340
威望:22
注册:2005-9-22

cin.clear()是清除输入流的标记,不论之前的输入标记是错误标记还是正常标记。
cin.ignore(1024, '\n')是丢弃输入流的字符。1024是最大丢弃数为1024个字符,'\n'是另一个条件,也就是当这个字符是'\n'时丢弃停止。
这个1024还不是很完美,最好的做法是得到输入流的最大字符数,这样就能保证所有的字符都能被丢弃。如下:
cin.ignore(numeric_limits<streamsize>::max(), '\n')
使用上面的numeric_limits之前还要包含#include <limits>

=×&D o I p R e E n C g T l X&×=
2008-7-21 14:50
沿途有鬼
Rank: 2
等级:注册会员
帖子:68
积分:882
注册:2008-7-20

引用:
踏魔狼 在 2008-7-21 14:50 的发言:

cin.clear()是清除输入流的标记,不论之前的输入标记是错误标记还是正常标记。
cin.ignore(1024, '\n')是丢弃输入流的字符。1024是最大丢弃数为1024个字符,'\n'是另一个条件,也就是当这个字符是'\n'时丢弃停止。
...
非常感谢你的回答,谢谢对新手的帮助~!
2008-7-21 20:20
共有 379 人关注过本帖
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.048099 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved