Thread: getch(); problem!!?!?!

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    8

    getch(); problem!!?!?!

    hy, I have one problem with getch();

    look my program:

    Code:
    #define _WIN32_WINNT 0x0500
    #include <iostream>
    using namespace std;
    #include <conio.h>
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include <windows.h> 
    
    
    int main()
    
    {
     ShowWindow(GetConsoleWindow(), SW_HIDE);
             
       string g;
       
    do {
      
         g= getch();
      
     if(g=="A") {
    messagebox("A");
    } 
    
    
      
      
        } while(g!="K");    
    
    ShowWindow(GetConsoleWindow(), SW_SHOW);
    system("pause")
    
    return 0;
    
    }
    and getch() doesn't work!!!
    why?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Where exactly do you expect the input to come from? getch() reads from stdin [or a similar input - but lets make it stdin at the moment], when you don't have a window, there's no console to feed your keypresses through to stdin.

    If you want to do this, you'll POSSIBLY be able to do it by using the Windows API, but I'm 99.9% sure that you can't read from a console when the window of that console is hidden, becuase the window itself is responsible for transforming a generic keypress into a "stdin" input.

    Could of course be that I'm wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Define "doesn't work." getch returns an int and you are attempting to store it into a string. Do you get no warnings/errors at all? If this is a C++ program you should also be including <string>, and <cstdio> instead of <stdio.h> and <cstdlib> instead of <stdlib.h>.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hk_mp5kpdw View Post
    Define "doesn't work." getch returns an int and you are attempting to store it into a string. Do you get no warnings/errors at all? If this is a C++ program you should also be including <string>, and <cstdio> instead of <stdio.h> and <cstdlib> instead of <stdlib.h>.
    And probably shouldn't have "using namespace std" BEFORE the includes either!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by matsp View Post
    And probably shouldn't have "using namespace std" BEFORE the includes either!

    --
    Mats
    Actually, conio.h, stdio.h, and every other include after davidboja's using namespace std all use the global namespace. Right now, only iostream uses std::, and as the using directive is after that header file, everything's fine and dandy.

    Of course, if the code was changed to use <cstdio> instead of <stdio.h> etc, then those c* header files would have to go before the using directive. Presumably this is what you meant, but I just thought I'd clarify.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dwks View Post
    Actually, conio.h, stdio.h, and every other include after davidboja's using namespace std all use the global namespace. Right now, only iostream uses std::, and as the using directive is after that header file, everything's fine and dandy.

    Of course, if the code was changed to use <cstdio> instead of <stdio.h> etc, then those c* header files would have to go before the using directive. Presumably this is what you meant, but I just thought I'd clarify.
    Actually, I was more trying to say that "You should never put using namespace ..."[1] before a header-file, but yes, only on some headerfiles does it make a noticable difference.

    [1] "Never say never", but in my opinion, there is, AFAIK, no good reason to put a using namespace ... before any include file.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    but when i don't hide cmd it work!!!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by davidboja View Post
    but when i don't hide cmd it work!!!
    And I said:
    Where exactly do you expect the input to come from? getch() reads from stdin [or a similar input - but lets make it stdin at the moment], when you don't have a window, there's no console to feed your keypresses through to stdin.

    If you want to do this, you'll POSSIBLY be able to do it by using the Windows API, but I'm 99.9% sure that you can't read from a console when the window of that console is hidden, becuase the window itself is responsible for transforming a generic keypress into a "stdin" input.

    Could of course be that I'm wrong.
    That pretty much confirms what I say: You need to have a command window to receive data through the console device. Which is hardly surprising. [Well, it's not surprising to me anyways].

    What are you actually trying to do? [Big picture, not "read a character from the keyboard" type answer].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by davidboja View Post
    but when i don't hide cmd it work!!!
    And what do you want? Read a character from a console window that isn't there?

  10. #10
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    >>messagebox("A");

    Is that one of your functions? That's not a real function otherwise.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To clarify: Windows uses "messages" to indicate keypresses. The "console window" is used to receive these messages, and translate it into "standard input". Without the window there, Windows will just send the message about the keypress to the next window, which is perhaps your desktop, a web-browser, MS Word or whatever is left on the screen. Whether that application actually does anything with the message about the keypress is obviously up to that application.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by matsp View Post
    [1] "Never say never", but in my opinion, there is, AFAIK, no good reason to put a using namespace ... before any include file.
    --
    Mats
    Never, indeed. What if the contents of the header file require some namespace to be brought into the global namespace? . . . "Who is the idiot who designed this header file, anyway?"

    Note that you can detect keypresses sent to other applications. But you have to use special functions like GetAsyncKeyPress(), and it can be a bit of a pain.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. Problem with my file opener
    By kzar in forum C Programming
    Replies: 7
    Last Post: 04-20-2005, 04:20 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Problem with getch() and fgetc()
    By Krupux in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 11:38 PM