chapter17_13_get_gun.cpp
#include <iostream>
const int Limit = 255;
int main(){
using std::cout;
using std::cin;
using std::endl;
char input[Limit];
cout << "Enter a string for getline() processing:\n";
cin.getline(input, Limit, '#'); //从输入流获取数据,第三个参数规定直到遇到分界字符#停止输入,并丢弃输入中的分界字符#
cout << "Here is your input:\n";
cout << input << "\nDone with phase 1\n"; //输出分界字符#之前的input内容
char ch;
cin.get(ch); //从#分界字符的后面继续读取输入流,这里只读取了一个char字符
cout << "The nexr input character is " << ch << endl;
if(ch != '\n') //如果ch没有读取到换行符,执行该循环
cin.ignore(Limit, '\n'); //丢弃接下来的255个字符,直到到达第一个换行符,然后丢弃换行符
cout << "Enter a string for get() processing:\n";
cin.get(input, Limit, '#'); //从输入流获取数据,第三个参数规定直到遇到分界字符#停止输入,并保留输入中的分界字符#
cout << "Here is your input:\n";
cout << input << "\nDone with phase 2\n";
cin.get(ch); //这里获得的是#的char字符,因为getline抽取了第三个参数并丢弃,然而get抽取第三个参数并保留在输入流中
cout << "The next input character is " << ch << endl;
return 0;
}
/**
Enter a string for getline() processing:
Please pass
me a #3 melon!
Here is your input:
Please pass
me a
Done with phase 1
The nexr input character is 3
Enter a string for get() processing:
I still
want my #3 melon!
Here is your input:
I still
want my
Done with phase 2
The next input character is #
**/