我一直在寻找一种方法来暂停控制台的显示。 我在互联网上找到了一些建议,但它不是我想要的工作方式。
我的问题是 我怎样才能改进这个pause()函数,使它能识别任何键(包括回车键)?
我先告诉你发生了什么,然后给你看一段代码。
这是代码。
#ifndef MYFUNCTIONS
#define MYFUNCTIONS
#include <cstdio>
#include <cstdlib>
using std::cout;
using std::cin;
void pause() {
string holdit;
//std::cout.flush();
//std::cin.clear();
std::cout << std::endl << "Press any key to continue...";
std::cin >> holdit;
holdit = std::cin.get();
std::cout << "The first character in holdit is <" << holdit[0] << "> x'" << setfill('0') << setw(2) << std::hex << (int)holdit[0] << "'" << endl;
if (holdit[0] == 'x') throw 99;
}
#endif
先谢谢你的帮助
经过多次尝试创建一个暂停()函数,我有了它。 它能像识别其他键一样识别ENTER键。 这个函数包含了一个cout语句来显示第1个十六进制字符(我在调试时使用了这个语句)。 如果你有任何意见,请告诉我。 我是一个C++初学者。 所以,请多多指教 :) 谢谢。
#ifndef MYFUNCTIONS
#define MYFUNCTIONS
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
void pause()
{
char holdit;
cout << "Press any key to continue..." << endl;
holdit = _getch();
cout << "holdit = < x'" << setfill('0') << setw(2) << hex << (int)holdit << "'>" << dec << endl;
if (holdit == 'x') throw 99;
}
#endif