方法1
#include <iostream>
#include "conio.h"
int main()
{
while (true)
{
// 在此处填入需要循环的代码
if (_kbhit()) // 如果有按键被按下
{
if (_getch() == 'q') //如果按下了q键则跳出循环
{
break;
}
}
}
return 0;
}
方法2
#include <iostream>
#include <string>
int main() {
bool done = false;
std::string input;
while (!done) {
std::cout << "请输入命令: ";
std::getline(std::cin, input);
if (input == "q") {
done = true;
}
else if (input == "help") {
std::cout
<< "\nCommand List:\n"
<< "help: Display this help text\n"
<< "quit: Exit the program\n"
<< std::endl;
}
else {
std::cout << "Unrecognized Command" << std::endl;
}
}
getchar();
return 0;
}
如果您觉得这篇博文有用,请访问我的个人站:http://www.stubbornhuang.com/,更多博文干货等着您。