eg.
#include <iostream>
using namespace std;
void main()
{
int a,b,c;
cout<<"input a:";
cin>>a;
cin.ignore(1024, '\n');
cout<<"input b:";
cin>>b;
cin.ignore(1024, '\n');
cout<<"input c:";
cin>>c;
cout<<a<<"\t"<<b<<"\t"<<c<<endl;
}
如果没有cin.ignore(),可以一次输入3个数,用空格隔开就好了。。可是非常不美观。。这样才是我们想要的。
如果cin.ignore()不给参数,则默认参数为cin.ignore(1,EOF),即把EOF前的1个字符清掉,没有遇到EOF就清掉一个字符然后结束,会导致不正确的结果,因为EOF是文件结束标识呵。
#include<iostream><wbr style="font-family:simsun; font-size:14px; line-height:21px"><span style="font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="font-family:simsun; font-size:14px; line-height:21px"><br style="font-family:simsun; font-size:14px; line-height:21px"><span style="font-family:simsun; font-size:14px; line-height:21px">using</span><wbr style="font-family:simsun; font-size:14px; line-height:21px"><span style="font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="font-family:simsun; font-size:14px; line-height:21px"><span style="font-family:simsun; font-size:14px; line-height:21px">namespace</span><wbr style="font-family:simsun; font-size:14px; line-height:21px"><span style="font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="font-family:simsun; font-size:14px; line-height:21px"><span style="font-family:simsun; font-size:14px; line-height:21px">std;</span><wbr style="font-family:simsun; font-size:14px; line-height:21px"><span style="font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="font-family:simsun; font-size:14px; line-height:21px"><span style="font-family:simsun; font-size:14px; line-height:21px"></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
void main()<wbr><wbr><br> {<wbr><wbr><br> char<wbr><wbr>str1[30],str2[30],str3[30];<wbr><wbr><br> cout<wbr><wbr><<<wbr><wbr>"请输入你的姓名:";<wbr><wbr><br> cin>>str1;<wbr><wbr><br> cout<<"请输入你的住址:";<wbr><wbr><br> cin.ignore();<wbr><wbr><br> cin.getline(str2,30,'a');<wbr><wbr><br> cout<wbr><wbr><<<wbr><wbr>"请输入你的籍贯:";<wbr><wbr><br> cin.ignore();<wbr><wbr><br> cin.getline(str3,30);<wbr><wbr><br> cout<<str3;<wbr><wbr><br> }<wbr><wbr><br><br> 如果在地址那里输入bcdabcd那么此时流里面剩的是bcd\n,此时cin.ignore();吃掉的就是b了,这是流里还剩下cd\n直接交给cin.getline(str3,30);应为有个\n所以这里getline就直接返回 .</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>