我试图在程序中读取一行字符串,并且当字符串为"q"
时,程序应该会中断,但是我的主要功能有些奇怪的行为。您能为我找到吗?
非常感谢!
#include <iostream>
#include <string>
using namespace std;
int main()
{
int distance;
int points[1000][2] = {0};
string input;
cout << "Please enter the distance: ";
cin >> distance;
cin.clear();
while (true) {
cout << "Please enter the coordinates, ";
cout << "enter \"q\" to exit: ";
getline(cin, input);
cin.clear();
// Finish input
if (input == "q")
break;
}
return 0;
}
并且终端中的输出是:
Please enter the distance: 5
Please enter the coordinates, enter "q" to exit: Please enter the coordinates, enter "q" to exit:
cout
循环中的while
似乎已进行了两次。