0
我有以下代碼:C++中的cin.get()問題?
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int x = 0;
cout << "Enter x: " ;
cin >> x;
if (cin.get() != '\n') // **line 1**
{
cin.ignore(1000,'\n');
cout << "Enter number: ";
cin >> x;
}
double y = 0;
cout << "Enter y: ";
cin >> y;
if (cin.get() != '\n'); // **Line 2**
{
cin.ignore(1000,'\n');
cout << "Enter y again: ";
cin >> y;
}
cout << x << ", " << y;
_getch();
return 0;
}
在執行時,我可以輸入x值和它忽略行1如我的預期。但是,當程序要求y值時,我輸入了一個值,但程序沒有忽略,而在第2行?我不明白,第1行和第2行有什麼區別?我該如何讓它按預期工作?