Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hey,
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;
 
    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 18-Jan-13 7:39am
saad_lah1.1K
Edited 18-Jan-13 7:50am
ProgramFOX118.8K
v3

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
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 Sergey Alexandrovich Kryukov 483
1 OriginalGriff 333
2 Maciej Los 305
3 CHill60 220
4 manoj kumar choubey 184
0 OriginalGriff 6,367
1 Sergey Alexandrovich Kryukov 5,296
2 Maciej Los 3,384
3 Peter Leow 3,259
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2014
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