Using cin in a program with ReadConsoleInput() ... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Using cin in a program with ReadConsoleInput() ...

I am trying to develop an interactive menu service, which will be able to read text and mouse clicks on buttons etc and keys together. My best bet was to use ReadConsoleInput() and handle each event in switch cases, which I am able to implement perfectly. But I now wished to perform a cin operation after my reading is complete. So I tried to implement the following 'escape' code: https://code.sololearn.com/cTVbrMO8S01G/#cpp But the problem I face is that cin reads nothing. I am able to type the characters, but they are never assigned to the string. It remains empty. So, how do I restore the reading ability to cin? I tried to make another approach, by marking some flag variable true, when I wish to exit, and appending each new character I type on screen to the string and print it at the position I am supposed to type it. I also implemented a backspace function like this by handling VK_BACK. But that becomes way too lengthy. I wished to have something easier like cin. Please help me reuse cin. Also, Is there another way I can exit from the infinite loop without using goto (a deprecated approach) ? I tried using break, but that just brings me out of the switch case.

9th Nov 2017, 10:30 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
1 Answer
+ 1
@kurwius My default console setting did not allow for mouse events and resizing events. Thats why I changed the console mode. But as you have done in your code, I must keep a backup. Edit - Thank You! It works as expected now! I just had another doubt. Is there a way I can find which all flags were enabled initially? The GetConsoleMode just returns a number, And there can be a lot of combinations for the same.
9th Nov 2017, 2:54 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar