• 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

    • RIP Hotlips..... IMHO, her best scenes were the few where she dared to let her REAL feminine side show through from underneath all that crappy, worn-torn soldier facade that she had to keep up with...she instantly lit up every room or situation where she was featured !
    • Helium Converter 3.3.69.0 by Razvan Serea Helium Converter is a free Windows utility for converting audio files between formats such as MP3, FLAC, AAC, WMA, OGG, and WAV. It supports batch conversion, preserves or updates tag information, and offers features like volume normalization. With a simple interface, it's ideal for users who need to convert large music libraries quickly and efficiently while retaining metadata. Helium Converter key features: Supports file formats: MP3, MP4, FLAC, AAC, M4A, WMA, WAV, OGG, OPUS, APE.... Batch conversion for large music libraries Preserves and edits metadata (ID3, Vorbis Comments, etc.) Volume normalization to equalize loudness Album art extraction and embedding Drag-and-drop interface for quick file selection Adjustable encoding parameters (bitrate, sample rate, channels) Uses internal codecs for consistent performance Supports CUE sheets for split track conversion File renaming based on tags during export Unicode support for international file and tag names Logging of conversion processes for troubleshooting Multi-core CPU support for faster conversions Download: Helium Converter 3.3.69.0 | 25.1 MB (Freeware) Links: Helium Converter Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Since it's been quite a while since the last episode aired, would it be fair (or cruel) to refer to Peggy as a MILF, or even a SMILF ?  HAHAHAHAHAHA
    • Their computers are not gonna stop working in October
    • Crowdstrike the same service provider that caused millions of in damages? I hate disliking a company for a singular failure but they really screwed up.
  • Recent Achievements

    • One Year In
      WaynesWorld earned a badge
      One Year In
    • First Post
      chriskinney317 earned a badge
      First Post
    • Week One Done
      Nullun earned a badge
      Week One Done
    • First Post
      sultangris earned a badge
      First Post
    • Reacting Well
      sultangris earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      172
    2. 2
      snowy owl
      122
    3. 3
      ATLien_0
      122
    4. 4
      Xenon
      116
    5. 5
      +Edouard
      93
  • Tell a friend

    Love Neowin? Tell a friend!