Press any key continue automatically

Oct 21, 2020 at 3:13pm
Hello everyone,
I will be very happy if you can help me for a problem I face with. I have written a C++ code to read a multiple data files in Visual Studio interface. The code works fine but after reading each file , I get the prompt screen ans say "press any key to continue…" I press enter and the code reads the Following file and go on. However, I want to do this automatically without pressing any key to continue reading. How can I do that ? Is there any code to press any key to continue that I can add to my code ?
Thank you so much

Oct 21, 2020 at 3:16pm
If you have access to the source code, just remove the part of the code that forces the user to wait.

Alternatively, you need to pipe newlines from either a file or from the output of another program into your program.

For example, make file called file.txt, and in that file just press Enter a bunch of times to create new lines, and then save the file.
Then, on the command line, run:
type file.txt | my_program
where my_program is the name of your C++ program.
Last edited on Oct 21, 2020 at 3:20pm
Oct 21, 2020 at 3:19pm
Thank you @Ganado. Yes I have access to the source code, there is a line system("pause"). When I remove it, it reads the files successively as I want, however, I can not see the results on the prompt screen since it disappears quickly as soon as the debugging finishes. I need to see the results as well.
Oct 21, 2020 at 3:22pm
Then put a single system("pause") [or equivalent, see the sticky thread] at the very end of main, outside of any loop.

Even better, since you said you're debugging, just put a breakpoint at the end of main and run your debugger.
There's a sticky thread dedicated to this sort of issue: http://www.cplusplus.com/forum/beginner/1988/

(I also edited my previous post while you were typing, with an alternative)
Last edited on Oct 21, 2020 at 3:24pm
Oct 21, 2020 at 3:56pm
Hello learner999,

At times I have found this useful to put a pause into a program.

 
std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread". 

The only 2 parts to be concerned about are that you change "seconds" to "milliseconds" and the number in (). 3 means 3 whole seconds and 1000ms is 1 second. using "milliseconds" can give you more control over the delay time.

Andy
Oct 21, 2020 at 9:21pm
Thank you so much @Handy Andy and @Ganado . The problem has been solved . Thank you so much
Topic archived. No new replies allowed.