help about reading from console screen in c++

Discussion in 'Programmer's Corner' started by moslem, Jul 12, 2010.

  1. moslem

    Thread Starter New Member

    Dec 16, 2009
    Hello ever one,
    i've aquestion about reading from console screeen i've a search engine project and i want when i start to enter the required word to search the console begins to display suggestions like google in the attached photo my problem now how can i make the main sense the letter before pressing enter to display the suggestions before the user press enter to search.
    my mean in a nother way:
    in main when i identify for ex
    string i;
    the variable i will set to the value after entering it from console screen and press enter i want to sense every letter before pressing enter to be able to display suggestions to the user.
    thanks alot.
    Last edited: Jul 12, 2010
  2. BMorse

    AAC Fanatic!

    Sep 26, 2009
    well since you did not mention what OS, I will assume windows....

    Try including the windows.h file and use the GetKeyState() function

    Like this: (This example scans through all keys and returns which one is pressed, but it also eats up a lot of cycles and does not work well with cin, but this is just one example of available functions from the win32 API)

    Code ( (Unknown Language)):
    1. int getKey(){
    2.     int buttonpressed = 0;
    3.     while (!buttonpressed){
    4.         for (int i = 0; i < 255; i++){
    5.             if (GetKeyState(i) < 0){buttonpressed = i;}
    6.         }
    7.     }
    8.     while (GetKeyState(buttonpressed) < 0){;}
    9.     return buttonpressed;
    10. }
    B. Morse
    moslem likes this.
  3. moslem

    Thread Starter New Member

    Dec 16, 2009
    @B. Morse
    i heard about method or command called console.readkey() method or function is that way you explained above if not what's this method.
  4. cheezewizz

    Active Member

    Apr 16, 2009
    I think console.readkey() is actually C# ...
    moslem likes this.
  5. moslem

    Thread Starter New Member

    Dec 16, 2009
    @B. Morse
    can you please explain this function to me i can't understand it clearly ana explain how can i use it.
    thanks in advanced.