简体   繁体   English

如何删除输入的字母/字符串或任何输入

[英]How to remove entered letters/strings or any inputs

I have made a hangman program in C. I have also started coding on C++. 我已经用C编写了一个hangman程序。我也已经开始使用C ++进行编码。 My game asks for a word (string), then enter and the hangman screen appears. 我的游戏要求输入一个单词(字符串),然后输入,然后出现hangman屏幕。 But because I entered the word before, we can cheat by pressing the UP arrow key and see the previous inputs. 但是因为我之前输入过单词,所以我们可以通过按UP箭头键进行作弊并查看以前的输入。 Is there any way to delete the previous input words from record? 有什么办法可以从记录中删除先前输入的单词? (on windows) Please also tell me any specific terms if any for these types of things, Thank you; (在Windows上)也请告诉我有关这些类型的事物的特定术语,谢谢;

Use SetConsoleHistoryInfo with a history buffer size of 0 on Windows Vista / Server 2008 or later. 在Windows Vista / Server 2008或更高版本上,将SetConsoleHistoryInfo的历史记录缓冲区大小设置为0。 Not sure about other platforms. 不确定其他平台。

Another way (besides using SetConsoleHistoryInfo ) is to implement an input of your program with getch() - there will be no history at all. 另一种方法(使用SetConsoleHistoryInfo )是使用getch()实现程序的输入-根本没有历史记录。

Example: 例:

const int bufferSize = 2048;
wchar_t buffer[bufferSize];
memset( buffer, 0, sizeof(buffer) );
printf( "\nEnter string:\n" );

int charsEntered = 0;
while(1) {
    wchar_t ch = _getch();
    if( ch == L'\r' ) {
        break;
    } else if( ch == 8 ) {
        if( charsEntered == 0 ) {
            continue;
        }
        _putch( ch );
        _putch( L' ' );
        _putch( ch );
        charsEntered--;
    } else {
        buffer[charsEntered++] = ch;
        _putch( ch );
    }
}

wprintf( L"\nString: %s", buffer );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM