Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,
I am having problem with kbhit() and getch() function.


C++
//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;

    if(_kbhit())
    {
        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
Updated 18-Jan-13 7:50am
v3

1 solution

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.
 
Share this answer
 
Comments
saad_lah 18-Jan-13 14:20pm    
I google and find that it _getch() or kbhit() doesnot work in directx c++,
OriginalGriff 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 18-Jan-13 15:00pm    
:/ okay
OriginalGriff 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900