473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Impliment "Press any key to continue" in console program?

I'm running a simple win32 console application and I want to impliment
a "Press any key to continue", so I print that prompt, and then
what's the easiest way to impliment reading any key? Do I use
'getchar', or 'gets', or some get function? or 'cin', or what's
an easy way?

What I want is the program to pause before exiting so the user
can read whatever messages there are before the console window
disappears.
Jul 4 '06 #1
9 64263
try getch, getche, system("PAUSE")

-- Murali Krishna

Jul 4 '06 #2
Susan Rice wrote:
What I want is the program to pause before exiting so the user
can read whatever messages there are before the console window
disappears.
Depending on who uses your program and what it is used for, you might
not want to *force* your users to do that. They could want to run it
non-interactively, e.g. in a batch process.

Consider using a wrapper script that calls your program, then the
"pause" command or point out the possibility (for Windows users) to
alter the "Properties Program Close on exit" setting.

Regards,

Wolf

Jul 4 '06 #3
"Susan Rice" <sr****@cox.netwrote in message
news:8upqg.1030$lv.796@fed1read12...
I'm running a simple win32 console application and I want to impliment
a "Press any key to continue", so I print that prompt, and then
what's the easiest way to impliment reading any key? Do I use
'getchar', or 'gets', or some get function? or 'cin', or what's
an easy way?

What I want is the program to pause before exiting so the user
can read whatever messages there are before the console window
disappears.
Unfortunately, there is no cross platform way to do it. You can use an OS
specific call ( while ( !kbhit(), getch(), system("pause") etc.. in
windows ) or use a 3rd party library with such a call ( I seem to recall
boost has one ). The 3rd party libraries, as I'm aware, just take the their
function and call the OS specific code anyway.

If you are not concerned with cross platform operability, then use an OS
specific call.

What I do is the following code which is cross platform:

std::string wait;
std::cin >wait;

but this requires 2 key strokes. Any key and enter. Enter alone won't do
it. Any other key alone won't do it.

If your problem is with VC++ closing when you run a program before you can
see the output, one solution is to press ctrl-f5 to turn it instead of F5,
then it waits for a keypress before it closes the console.
Jul 4 '06 #4
Susan Rice posted:
I'm running a simple win32 console application and I want to impliment
a "Press any key to continue", so I print that prompt, and then
what's the easiest way to impliment reading any key? Do I use
'getchar', or 'gets', or some get function? or 'cin', or what's
an easy way?

What I want is the program to pause before exiting so the user
can read whatever messages there are before the console window
disappears.

include <cstdio>

int main()
{
std::puts("Press any key to continue...");

std::getchar();
}
--

Frederick Gotham
Jul 4 '06 #5
"Frederick Gotham" writes:
include <cstdio>

int main()
{
std::puts("Press any key to continue...");

std::getchar();
}
That puts a new spin on "any' that even Microsoft hasn't stooped to use.
Jul 4 '06 #6
How about if I say Press RETURN to exit. Will this work OK?

void MyExit()
{
printf( "Press RETURN to finish:" );
char c = getchar();
exit(1);
}

What I'm finding is I can enter a bunch of characters before pressing
RETURN, and I'm wondering what happens to all those characters.
Does just the first character go into c, and the rest are discarded?
There's no buffer overflow happening is there?

Frederick Gotham wrote:
Susan Rice posted:

>>I'm running a simple win32 console application and I want to impliment
a "Press any key to continue", so I print that prompt, and then
what's the easiest way to impliment reading any key? Do I use
'getchar', or 'gets', or some get function? or 'cin', or what's
an easy way?

What I want is the program to pause before exiting so the user
can read whatever messages there are before the console window
disappears.

include <cstdio>

int main()
{
std::puts("Press any key to continue...");

std::getchar();
}

Jul 5 '06 #7
Susan Rice wrote:
How about if I say Press RETURN to exit. Will this work OK?

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the newsgroup FAQ:

<http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4>


Brian

Jul 5 '06 #8
Susan Rice <sr****@cox.netwrote:
How about if I say Press RETURN to exit. Will this work OK?

void MyExit()
{
printf( "Press RETURN to finish:" );
char c = getchar();
exit(1);
}

What I'm finding is I can enter a bunch of characters before pressing
RETURN, and I'm wondering what happens to all those characters.
Does just the first character go into c, and the rest are discarded?
There's no buffer overflow happening is there?
The rest will remain in the input buffer and read by the next
request for input. In your case, there is no "next request" so the
remaining input will be discarded.

hth
--
jb

(reply address in rot13, unscramble first)
Jul 5 '06 #9
Jim Langston <ta*******@rocketmail.comwrote:
"Susan Rice" <sr****@cox.netwrote in message
news:8upqg.1030$lv.796@fed1read12...
>What I want is the program to pause before exiting so the user
can read whatever messages there are before the console window
disappears.

What I do is the following code which is cross platform:

std::string wait;
std::cin >wait;

but this requires 2 key strokes. Any key and enter. Enter alone won't do
it. Any other key alone won't do it.
I usually use:

std::cout << "Press <Enterto continue...\n";
std::string trash;
std::getline(std::cin, trash);

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 11 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Harman Sahni | last post by:
As per this URL http://msdn.microsoft.com/library/en-us/vjref98/html/14_14.asp?frame=true conitnue works on for, while, do... I know it works for foreach as well as I'm using it somewhere. My...
2
by: Paul Johnston | last post by:
I'm using VB.Net. I've tried using Console.Read() but it requires a line-terminator before it finishes (doesn't that mean it's functionally equivalent to ReadLine() -- why have it then?). I've...
5
by: Matt | last post by:
I will be grateful if someone explians this part colfree = FALSE; upfree = FALSE; downfree = FALSE; of the code below. I don't understand how this marks the downward and upward diagonals....
0
by: Noam_h | last post by:
Hi all. Calling this web service: POST /WebService2/Service1.asmx HTTP/1.1 Host: WKSI105080A22 Content-Type: text/xml; charset=utf-8 Content-Length: 396 SOAPAction:...
8
by: Timur Ametov | last post by:
Hallo everybody. I'm using Visual Studio 7 and hier is my question. How can i see in Outputwindow after my Programm ended the message "Press any key to continue". Is it possible? Now this window...
1
hpbutterbeer
by: hpbutterbeer | last post by:
We have a Machine Project and my brain is currently in a clouded state. Sorry, I'm just a beginner in C Programming... Text twist is a windows game whose main objective is to form words out of the...
3
by: per9000 | last post by:
Hi, can I print some default text on the console when doing a ReadLine? A silly example below shows two small scenarios. In the first a user is told what he appears to be called, and asks for a...
2
by: Chris Thomasson | last post by:
I was wondering if the 'SLINK_*' and 'SLIST_*' macros, which implement a simple singly-linked list, will produce _any_ possible undefined behavior: ____________________________ #include...
0
by: nstout | last post by:
writing a program in java System.out.print("Continue? (y/n): "); choice = sc.next(); System.out.println(); Then it displays "press any key to continue..." How do you stop this from...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.