在CPP中使用cout重复输出
Repetitive output using cout in CPP
我试图将一行字符串读入程序,当字符串为 "q"
时,程序应该会中断,但我的 main 函数出现了一些奇怪的行为。你能帮我找出来吗?
非常感谢!
#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:
while
循环中的cout
好像执行了两次
我试图将一行字符串读入程序,当字符串为 "q"
时,程序应该会中断,但我的 main 函数出现了一些奇怪的行为。你能帮我找出来吗?
非常感谢!
#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:
while
循环中的cout
好像执行了两次