Hello,
I am capturing a video in my program using OpenCV. If the user presses "Enter" key, I take a picture and ask the user if the picture is okay. If he says "No", I keep capturing the video and wait for another "Enter" input.
Note: This pressing the "Enter" key happens in a window that OpenCV creates, not in the terminal.
So if the user says "No", and while capturing video again, if the user types something in the terminal (by accident), then whatever he types gets written to getline()'s buffer and when I call this getline() function for the second time, it reads that input from its buffer.
How can I prevent this?
Code:
string choice;
int choiceIsOkay = 0;
while (1)
{
// Capture video
if (waitKey(30) == 13) // Enter
{
while (choiceIsOkay == 0)
{
cout << "Is the picture okay? (Y/N): ";
getline(cin, choice);
if ((choice == "Y") || (choice == "y"))
{
choiceIsOkay = 2;
}
else if ((choice == "N") || (choice == "n"))
{
choiceIsOkay = 1;
}
else
{
cout << "\nInvalid input\n";
choiceIsOkay = 0; // Go back and wait for a valid input
}
}
if (choiceIsOkay == 2)
{
runAlgorithm = 1;
break;
}
else choiceIsOkay = 0; // Reset the flag
}