• 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

    • I always turn encryption off 1st boot, crazy its on by default on new computers, it should ASK you ON or OFF on 1st boot,, So many people dont even know its on , then forget their windows login and microsoft account,, RETarDED Microsoft is now, , i also find having it on slows things down too
    • Adding AI is just an excuse to hike prices. I don't want any AI features in our Slack workspace and yet will have to pay for it.
    • Hello Could you be kind too help me fix this 3.1gig and not 256gig it says 3.1gig only .someone told me to fix it with this https://www.disk-partition.com/articles/64gb-flash-drive-only-showing-32gb-5740i.html yes my sandisk ultra usb 3.0 256gb wont show up in full .  but i don't know want options i have to use on there can someone  please guide me with step by step instructions please  guide me with step by step instructions on how to repair this .sorry for asking but I've tried but i don't know want i have to do on that programme please help me i am on windows 11 home thank you nick
    • AI is as big a scam as FTX was...run by the same 'group' of people as well.
    • Slack Business+ plan jumps to $15/month as AI features launch by Paul Hill Salesforce has announced a price increase for one of its plans, which will add a few dollars per user each month to organizations' bills. The Slack Business+ plan will see prices rise from $12.50 to $15 per user per month if you pay for a whole year up front; if you pay monthly, the bill will increase from $15 to $18. Salesforce has kept the prices of its Pro plan the same and introduced a new Enterprise+ plan with custom prices for larger organizations. The new price adjustments will take effect on August 17 if you’re planning to renew, and immediately for any new customers. So, if you are renewing before August 17, you will be able to delay this price rise for around a year. The company isn’t just pulling a fast one on customers with these price rises. It’s adding AI to all paid plans, so if you use these features, the increase somewhat justifies them. We saw a similar tactic from Google Workspace back in January. New AI features and deeper Salesforce integration Slack has outlined all the new AI features coming to its Pro, Business+, and Enterprise+ plans starting from July 17. The Pro Plan will have access to conversation and thread summaries, plus huddle notes, while Business+ will have all of those plus: AI Search, Recaps, Translations, File summaries, Workflow generation, and Summarize channels workflow step. The Enterprise+ subscription has all those AI features plus Enterprise search. Additionally, it provides enhanced security, admin controls, governance, and compliance. The company also announced that all plans, including the free plan, will get access to Salesforce Channels, which lets you use Slack and Salesforce together directly within their Customer 360 Apps. Who benefits? By embedding AI right within Slack, organizations should be able to boost efficiency more easily in their daily operations. To ensure that it keeps up with the competition, the company is investing significant resources into its research and development to deliver new innovations. Affected organizations will need to try to find effective ways to use these AI tools to make the price increase worth it, whether this is speeding up tasks to free workers up for other work or something else. The move definitely doesn’t hurt Slack because it’ll make more money whether you use these AI tools or not.
  • Recent Achievements

    • Week One Done
      vivetool earned a badge
      Week One Done
    • Reacting Well
      pnajbar earned a badge
      Reacting Well
    • Week One Done
      TBithoney earned a badge
      Week One Done
    • First Post
      xuxlix earned a badge
      First Post
    • First Post
      Tomek Święcicki earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      673
    2. 2
      ATLien_0
      288
    3. 3
      Michael Scrip
      223
    4. 4
      +FloatingFatMan
      195
    5. 5
      Steven P.
      144
  • Tell a friend

    Love Neowin? Tell a friend!