我使用 cin.peek() 方法得到了这段代码。我注意到奇怪的行为,当输入到程序看起来qwertyu$[Enter]一切正常时,但当它看起来qwerty[Enter]$只有当我输入双美元符号时才有效qwerty[Enter]$$。另一方面,当我使用cin.get(char)一切正常时。
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char ch;
int count = 0;
while ( cin.peek() != '$' )
{
cin >> ch; //cin.get(ch);
count++;
}
cout << count << " liter(a/y)\n";
system("pause");
return 0;
}
//Input:
// qwerty$<Enter> It's ok
//////////////////////////
//qwerty<Enter>
//$ Doesn't work
/////////////////////////////
//qwerty<Enter>
//$$ works(?)