我的问题是输入的内容是什么,执行时总是会漏掉单词“输出:”的第一个字母“ o”。关键是该代码在Dev C ++上可以正常工作,但在Visual Studio中仍然缺少“ o”。我认为这可能与getche()函数有关,与键盘缓冲区有关,但是我该如何解决呢?
#include <iostream>
#include <conio.h>
#define convertDifference 7 // the difference value of converting one to another
#define input_max 100 //determine input length
#define stopKey 27 // 27 --> ansi decimal for "Esc"
int main(){
char input[input_max] = {0};
int i = 0;
// retrieve input----------------------------------
std::cout << "input: ";
input[i] = _getche();
while ((int)input[i] != stopKey) {
i++;
if (i > input_max-1) {
std::cout << "\ninput max exceed";
break;
}
else {
_getche(); // clears keyboard buffer (if unable to compile or run, change "_getche()" into "getche()")
//for compilers that ignore keyboard buffers, comment or delete the above line code
input[i] = _getche(); // if unable to compile or run, change "_getche()" into "getche()"
}
}
// convert & output--------------------------------
puts("");
i = 0;
std::cout << "output: ";
while (i <= input_max - 1 && ((int)input[i] != stopKey)) { //check if exceeded max of input length or stopKey appears
std::cout << (char)(input[i] - convertDifference);
i++;
}
puts("");
return 0;
}