By default, other than input operations, the console was never told to wait. Before return in main( ), type: std::cin.ignore( std::numeric_limits< std::streamsize >::max( ), '\n' ); Optionally, if you're on Windows (or even on unix), execute the program from command prompt/terminal.
I guess you could in corporate system( ), but don't lean on it, and don't use it within the release build of a project; major security issues, an' all.
@framework @learn2adv
actually Im just a student who took a subject regarding c++ just easy one, so my knowledge is limited to what the teacher teaches and as it's a small part of assignment I try to not use beyond that but it works after I put system("pause"); .
@ctest: For learning purposes, system( )'s fine, but don't too attached to it.
learn2adv wrote:
Not sure why it is needed these days other than the fact it stops the execution.
It's not that system( ) doesn't do the job, it's the fact that it brings security risks, performance penalties, and OS dependency to the table. Like I said, for learning, it's fine. But in production code, it's a risky move.
that makes me wonder, when he teaches in the class, he didn't use it, and everything is fine with <<endl; . I am using the same version 2005 of microsoft express C++ as he did but I upgraded something as it have compatibility error with window 7. what worries me is what will happen if he use the codes in his compiler , afraid will give error, because I need to submit my assignment day after tomorrow
Based on your code which you posted, there should be no compatibility issues. The only issue that may occur is if your tutor has a different OS. system( ) was used in MinGW's compiler suite that came with Dev-C++ many years ago.
No I mean, I try visual c++ express editor 2005, in window 7, the program has compatibility error, then I upgrade package something to make it working in window 7. and he never use system ( ) or others,
just like when he type
#include<iostream>
using namespace std;
int main ()
{
cout<<"Hello world"<<endl;
return 0 ;
}
and debug, the program 'waits' but mine not. he didnt use system ( ) that's why I am asking if that will give other effects something
It's not that system() doesn't do the job. It's that there is no job to be done. (but if it were, it will be terrible doing it, xP)
When a program ends I want for it to end.
It's just a configuration in your IDE. It should launch a wrapper of the program, and is the wrapper the one that expects a key. system or cin.ignore will not "work" if your program ended prematurely (like by a fatal error)
If you IDE is too stupid, your best option is to open a terminal and execute the program from there.
Things that you could learn if you don't use system("PAUSE");
_ Look, a terminal.
_ input/output redirection
_ pipes
_ command line arguments
_ working directory
what will happen if he use the codes in his compiler , afraid will give error
You should include #include<cstdlib> where system is declared.
Ask your teacher about the issue.
Interesting. I'm assuming you are working on a PC. My professor told us if we want to see the program on a pc we should include system("PAUSE"). He uses that in all the programs we do in class. But I work on a mac at home and he specifically said not to include it if coding in xcode.