Click here to Skip to main content
11,641,971 members (66,532 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++
I am having problem with kbhit() and getch() function.

//word = "Hello";
//Funtion is working fine but when I press a key it doesnot enter if(kbhit()) loop.
//Moreover, I tried _getch() next to "if" loop statement but _getch() is also not working :(
void Game::Print_LetterBox(int x,int y,char* word)
    int size = strlen(word);
    char key;
        key = _getch();
    for(int i=0;i<size;i++)
        Letter_Box( word[i],x + i*34,y,"purple");
        for(int j=0;j&lt;size;j++)
            if(word[j] == key)
                Letter_Box( word[i],x+i*34,y,"red");
    Animated_Character( x + size*34, 540 , 5);

Any help!!
Posted 18-Jan-13 7:39am
Edited 18-Jan-13 7:50am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I think your problem is that if is not a loop - it's a one time conditional test. If the test passes, then the code in the statement (or compound statement) below it is executed. If it isn't then the condition is skipped completely, and execution continues immediately with the next statement. I suspect that you want to use while rather than if which does loop.
saad_lah at 18-Jan-13 14:20pm
I google and find that it _getch() or kbhit() doesnot work in directx c++,
OriginalGriff at 18-Jan-13 14:59pm
Perhaps next time it would help if you tagged your question as DirectX, or said so in the question itself?
Remember that we can't see your screen, access your HDD, or read your mind :laugh:
saad_lah at 18-Jan-13 15:00pm
:/ okay
OriginalGriff at 18-Jan-13 15:06pm
It's not a problem - it just helps us to answer you better! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 233
1 Mika Wendelius 150
2 Sergey Alexandrovich Kryukov 134
3 OriginalGriff 110
4 Afzaal Ahmad Zeeshan 89
0 Mika Wendelius 460
1 OriginalGriff 265
2 DamithSL 258
3 CPallini 169
4 Afzaal Ahmad Zeeshan 149

Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100