How to delete console history?

I wrote a brief code to show what I want:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    unsigned long number;
    std::string input;

    do
    {
        while(true)
        {
            std::cout << "Enter a number (0 to exit): ";
            getline(std::cin, input);
            std::stringstream myStream(input);
            if (myStream >> number)
            break;
            std::cin.clear();
        }

        std::cout << "You entered " << number << "\n\n";

    } while (number != 0);

    std::cout << "\n" << "Bye bye!" << "\n";
    std::cin.clear();
    std::cin.ignore(10000, '\n');

    return 0;
}


Here this code can handle even if you input text, instead of going crazy, but basically it's one of the beginner tutorials.
When you run this program, after you've typed something in, if you press up arrow the last input appears, if you press right arrow the input appears each character one by one, I know this is normal input behaviour, but is it possible to do something that this doesn't happen?
I mean if you press any arrow, I want nothing to be returned.
Last edited on
You should look into WinAPI and console handling functions.
Am I looking in the right place?
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073(v=vs.85).aspx

Maybe you know which function should I use (I suppose it's not even there and you have to figure out a way to do it yourself using similar methods).
So I found the syntax for the function I need (I think), it's like this:
1
2
3
BOOL WINAPI SetConsoleHistoryInfo(
  _In_  PCONSOLE_HISTORY_INFO lpConsoleHistoryInfo
);

I've used the WinAPI just a little bit (to clear the screen and to move the cursor to a different position) and I really have no idea what to do now. Something like this, I suppose:
 
BOOL WINAPI SetConsoleHistoryInfo( PCONSOLE_HISTORY_INFO NULL );
(Like that, the compiler tells me that PCONSOLE_HISTORY_INFO was not declared in the scope)

Can someone, please tell me what the function should look like and when should I call it. At the beginning of the main?
Last edited on
In Linux bash you can use
history -c
I want the equivalent for the console (preferably something that is compatible with Windows XP and Windows 7)
Are you trying to do this in bash? I don't understand your question. Are you trying to implement the equivalent of the history command in your code or not? Also when I press the up arrow in bash as I am running the program, I do not get the previous things I have typed before (this is not important).
Are you trying to do this in bash?
No, I'm trying to do this in a console program (the code I posted above)

Are you trying to implement the equivalent of the history command in your code or not?
I posted the bash example (it's completely unrelated to the problem), because it has the exact functionality I need, only for C++.

Also when I press the up arrow in bash as I am running the program, I do not get the previous things I have typed before
No, bash is completely unrelated, you get your previous results if you press the up arrow (↑) or the right arrow in the program (same code as in the first post)

Apparently the SetConsoleHistoryInfo is only for Windows Vista and above (I'm on Linux and I use a Windows XP VM to build Windows specific programs), so I'm looking for
something that is compatible with Windows XP and Windows 7
(and, if possible, Linux, (I do realise Windows an Linux are so different it's probably impossible).

The thing is that in a larger console game, once the player types his/her name it starts appearing in various locations if the player presses the up arrow.
Maybe I could use GetAsyncKeyState to create an exception, that if the up arrow is pressed the program ignores it?
Last edited on
Topic archived. No new replies allowed.