• 0

[C++] Console function to wait for input


Question

This has annoyed me for hours if not days. :(

I'm writing a simple console application with a menu that has some choices. Every time you select an option it should:

ask for a number,

output the result,

pause the app until a key is pressed so the user can read the data printet out,

clear the screen and display the menu again

system("pause") doesn't work because it's launched in a seperate thread and is not in sync with the application (sometimes its way behind the app and sometimes ahead of the execution(!) O_o )

SleepEx() doesn't seem to be able to do what I want.

WaitForInputIdle() doesn't work since console applications doesn't have a message queue.

What can I do to make this work? I hate those system calls to be so out of sync with the app!

And no, I don't want to use an infinite while loop untill a char is pressed as it would take 100% CPU.

Anyone? :(

Edited by Andos
Link to comment
https://www.neowin.net/forum/topic/462358-c-console-function-to-wait-for-input/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

It just seems that EVERYTHING is completely out of sync when doing those console apps.

I was just told to use the getchar(); function to wait for an enter press.

I have this code:

------

cout << "Press enter to continue...";

getchar();

------

For whatever reason, it waits for the enter key and THEN it prints out "Press enter to continue..." :s

What is wrong here???

  • 0

If you're on Windows some functions from conio.h may work for you

http://www.digitalmars.com/rtl/conio.html

(This is non-standard and will make your code less portable)

If things are "out of sync" try to flush your output after you print.

printf("Press enter to continue...");
fflush(stdout);
getchar();

Are you sure you don't have another input routine before your "Press enter..." that is waiting for input?

Edited by spydoor
  • 0

Thanks but that didn't work.

But I'm using cout << and not printf()

How come such a simple app can have so many problems with this? And why has so few gotten theese issues before?

:-/

*edit*

Yes i have a cin >> somevalue; input a bit earlier in the code.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Well to finalise the thread, I bought the addin card as recommended but had teething problems trying to connect it to my eGPU. After much research, I found that rated cabling is a thing and after connecting a rated thunderbolt 4 cable to the new USB addin card I started troubleshooting. A few BIOS tweaks and updates here and there and everything is now connected and working. New eGPU is being picked up by Thunderbolt Control Center Thank you to anyone that stopped and read the thread especially to the tech resources that stopped to post and help. It was of massive help being able to reach out to the community as always. I just hope I haven't wasted too much of the community time trying to diagnose the problem. Thanks for your help again. 😊 Kind regards
    • they keep making "changes" to stuff that isn't broke if I was them I'd just stop making changes to classic and focus on fixing the other mess
    • Every Xbox is a slimmed-down and modified Windows. Xbox 1 (2002) was a Windows NT Xbox 360 Windows 8 Xbox One Windows 10 Xbox SX Windows 11 all with versions with specific services just for games. The Windows that will come on laptops will be a slimmed-down Windows 11 Home focused on games.
    • The problem was that people who have played Extraction Shooters said there were too many missing features. Like Proximity Chat ...because of "toxicity". Lack of any story development even actually outlined. "Too watered down" for regular Extraction Shooter Enjoyers. Stupid amount of Aim Assistance on MnK. Questionable outdoor map design (how can something so "colorful" be so effing dull). Not to mention people still ###### they seemed to use the Marathon name and added in the alien/npcs after the fact, stappled on some other game concept they were working on. Like Arkane Studios was forced to use Prey as a title for their own original IP, by Bethesda to sucker in people for name recognition and ownership over the Prey IP. Which backfired with lower sales, due to the people realizing not a sequel or reboot, and the people who didn't like the original Prey.
  • Recent Achievements

    • Explorer
      Jdoe25 went up a rank
      Explorer
    • First Post
      Ian_ earned a badge
      First Post
    • Explorer
      JaviAl went up a rank
      Explorer
    • Reacting Well
      Cole Multipass earned a badge
      Reacting Well
    • Reacting Well
      JLP earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      665
    2. 2
      ATLien_0
      283
    3. 3
      Michael Scrip
      224
    4. 4
      +FloatingFatMan
      190
    5. 5
      Steven P.
      146
  • Tell a friend

    Love Neowin? Tell a friend!