简体   繁体   中英

How to remove entered letters/strings or any inputs

I have made a hangman program in C. I have also started coding on C++. My game asks for a word (string), then enter and the hangman screen appears. But because I entered the word before, we can cheat by pressing the UP arrow key and see the previous inputs. 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;

Use SetConsoleHistoryInfo with a history buffer size of 0 on Windows Vista / Server 2008 or later. 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.

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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