Im making a typing game for fun. It will show a random word on screen, and you have to type it out. Im using getch() in a loop. Each iteration it checks the letter entered and as soon as you make a mistake it will deduct points, play a sound effect sound effect and move on to next word.
I'm trying to make it ignore inputs entered during the time the sound effect is playing. Naturally, it takes some time to realise I made a mistake, so if I dont add this, I end up making multiple mistakes as the next few words flash by while im still typing the old word. (I think other people would have the same problem, or maybe its just me? :p )
Ive tried placing cin.ignore(std::numeric_limits::max(), '\n') before the input loop but they screw something up and getch() stops work. you can type normally, as its no longer snatched up by getch(). I also have to press enter twice to move on. I need to clear only whats in the buffer and nothing further Ive tried other stuff, but they all give similar results.
Is there any way to do what Im planning?
while (1){
randword = "youtube";
cout << endl << endl << "The word is " << randword;
cout << endl << "<< ";
//some code here to clear ONLY whats IN the buffer (so it doesnt eat the next few inputs and etc)
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
int poscounter = 0; // counts how many letters typed so far, and if all typed then of course gives total number of letter in word
for (std::string::iterator it = randword.begin(); it != randword.end(); ++it){ // allows input loop to cycle thru letters of the word until the end. but doesnt count them, so have to manually do that with poscounter
poscounter = poscounter + 1;
randwordcurrent = *it; // get letter of word at current iterator position
playertempanswer = _getch(); // get player answer. no need for enter! however _getch() does not display the input, so must output manually.
cout << playertempanswer; // manual output since getch() does not display user input
if (playertempanswer == randwordcurrent){ // correct answer
PlaySound(TEXT("rightfx.wav"), NULL, SND_FILENAME | SND_ASYNC);
playeranswerstate = 1;
}
else if ((playertempanswer != randwordcurrent) && (playertempanswer != "|")){ // incorrect answer, but not exit game trigger either
playeranswerstate = 0;
break; // break out of for loop early
}
} // end of randword input loop
if (playeranswerstate == 1){ // actions for right answer
std::this_thread::sleep_for(milliseconds(160)); // give time for rightfx to finish before playing the wordrightfx
PlaySound(TEXT("wordrightfx.wav"), NULL, SND_FILENAME | SND_ASYNC);
}
else if (playeranswerstate == 0){ // actions for wrong answer
PlaySound(TEXT("wrongfx.wav"), NULL, SND_FILENAME | SND_ASYNC);
std::this_thread::sleep_for(milliseconds(500)); // give time for wrongfx to finish ()
}
} //end of infinite while loop
stripped down version of the project showing only the necessary parts: https://onedrive.live.com/redir?resid=EE70CC84F483296F!1864&authkey=!ABLuRHvr_SNlaKs&ithint=folder%2c