【发布时间】:2020-01-21 19:11:15
【问题描述】:
我需要添加点击退格时删除字符的代码:
double inputDoubleNumber() {
double result = 0;
int char_code;
std::string buffer = "";
bool dot = false;
do {
char_code = _getch();
if (char_code > 47 && char_code < 58 || char_code == 46 ) {
if (char_code == 46 && dot == true) continue;
if (char_code == 46) dot = true;
buffer += (char)char_code;
std::wcout << (char)char_code;
}
} while (char_code != 13);
std::wcout << std::endl;
result = atof(buffer.c_str());
return result;
}
我该怎么做?
【问题讨论】:
-
这不是 C 函数,因为它使用 C++ 类型和对象,例如
std::string和std::wcout。 -
您想在用户按退格键时从
buffer中删除最后输入的字符吗? -
你为什么用
getch而不是std::cin? -
是的,我想从缓冲区中删除最后一个字符
-
请尽量避开magic numbers。