People..this must be quite old by now..but I need to know, is there any other way to stop the program from terminating after execution,besides cin.get(); or system("PAUSE");?
system commands are hackable,so I'd prefer a C++ way..
Printable View
People..this must be quite old by now..but I need to know, is there any other way to stop the program from terminating after execution,besides cin.get(); or system("PAUSE");?
system commands are hackable,so I'd prefer a C++ way..
what's wrong with cin.get();? That's not a system comand!
Look in the faq - faq.cprogramming.com
Dunno it wasn't working..ok I'll check it.
It was probably getting a newline left in the input buffer.
Assuming Windows you don't need to do anything to keep the program open. Just run your programs from the Command Prompt window which stays open so you can rerun the program or type another command.
I have a few shortcuts to it, all configured to open in directories where my programs are, so I wouldn't have to cd a lot to get to these directories.
have you tried:
Code:std::cout << "Press [Enter] to continue...";
cin.ignore(256, '\n');
There's also getch.
>Noobs *rolleyes*
Um, I fail to see what the problem is that warrants this kind of response.
>cin.ignore(256, '\n');
>There's also getch.
Let's not keep it so simple that we produce bad code, mmkay? Here are the issues:
1) The program is running interactively, so the user needs to see any output.
Obviously, if the program terminates and closes the window before the user has time to read any output, that's an issue.
2) The program is running in a separate process with its own console window.
If the program owns the window and the process terminates, the window will be destroyed, thus losing any of the output before the user can read it.
3) The program is performing unclean input that might leave junk in the stream.
This happens when you mix formatted and unformatted input, cin's >> operator and cin.get, for example.
4) The program uses a blocking single character read to stop execution.
If there are any characters in the stream, this read will succeed immediately and there won't be any blocking.
The solution is to discard leftover characters in the stream if they exist so that the blocking read blocks, but only if the program is running interactively in a separate process. Here's a largely portable solution that covers all of the bases, but it's kind of voodooish:
The problem is that while you can portably control the stream contents (sort of), you can't portably tell with code how the program is being run. So you can use parameters to tell that. When you have problems keeping the output window open, just add -i and -o to the run string:Code:#include <cstring>
#include <iostream>
#include <ios>
#include <istream>
#include <limits>
namespace jsw {
template <typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& ignore (
std::basic_istream<CharT, Traits>& in, CharT delim )
{
if ( in.rdbuf()->sungetc() != Traits::eof() && in.get() != delim )
in.ignore ( std::numeric_limits<std::streamsize>::max(), delim );
return in;
}
}
int main ( int argc, char *argv[] )
{
bool interactive;
bool owns_console;
for ( int i = 1; i < argc; i++ ) {
if ( std::strlen ( argv[i] ) == 2 ) {
if ( argv[i][0] == '-' ) {
if ( argv[i][1] == 'i' )
interactive = true;
else if ( argv[i][1] == 'o' )
owns_console = true;
}
}
}
// Your program goes here
if ( interactive && owns_console ) {
std::cin.clear();
jsw::ignore ( std::cin, '\n' );
std::cout<<"Press Enter to continue . . .";
std::cin.get();
}
}
As a side note, anything besides the following two options is probably a poor choice when it comes to clearing an unfinished line from the input stream:Code:myprog -i -o
Code:std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
That means getch is a poor choice (Yarin), and random magic numbers for the ignore amount are a poor choice (sh3rpa), and system("PAUSE") is an extremely poor choice for various reasons (SVXX). All in all, this is a deceptively simple operation, but it's very easy to step into the realm of non-portable behavior.Code:char ch;
while ( std::cin.get ( ch ) && ch != '\n' )
;
Hmm..so can I make this a header file and include it in my main file?
>Hmm..so can I make this a header file and include it in my main file?
No, you're not allowed to customize anything in C++. Any code you see is written in stone and can't be changed. :rolleyes:
I'm joking, by the way. In fact, if you know that your program won't be used as a filter (ie. cin and cout won't be redirected) to non-interactive devices, you can omit the switch stuff in your main program:
Code:#ifndef JSW_STREAM
#define JSW_STREAM
#include <ios>
#include <istream>
#include <limits>
namespace jsw {
template <typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& ignore (
std::basic_istream<CharT, Traits>& in, CharT delim )
{
if ( in.rdbuf()->sungetc() != Traits::eof() && in.get() != delim )
in.ignore ( std::numeric_limits<std::streamsize>::max(), delim );
return in;
}
}
#endif
Code:#include <iostream>
#include "jswstream.h"
int main()
{
// Your program goes here
std::cin.clear();
jsw::ignore ( std::cin, '\n' );
std::cout<<"Press Enter to continue . . .";
std::cin.get();
}
O.O nice Ima implement this...thanks!