我有一個C++程序,它從終端的輸入,以及用於某種原因,這是生產的無限循環:C++無限循環與輸入
double getSideLength()
{
cout << "Enter a side: "
double side;
cin >> side; cin.ignore(80, '\n');
while (side <= 0){
cout << "Please enter a valid side. Try again: ";
cin >> side; cin.ignore(80, '\n');
}
return side;
這將產生輸出:
Enter a side: invalid
Please enter a valid side. Try again:
Please enter a valid side. Try again:
Please enter a valid side. Try again:
.... and so on. "invalid" is the only input the user made
你使用ignore子句是什麼? – 2012-02-15 07:19:10