I am just learning the C++ programming language. What is the code for telling the DOSbox to stay open until I press a certain key like 'enter'. When I run the executable file, the command prompt pops up and displays the "Hello World" and then closes instantly.
The code for what I am trying to do is:
#include %26lt;iostream%26gt;
using namespace std;
int main ()
{
cout%26lt;%26lt;"Hello World";
return 0; // What is the code and where do I put it?
}
What is the code for keeping the DOSbox open in C++?
Your question was somewhat misleading until i fully read it, the reason, DOSBox is a 486 emulator designed to run old MS-DOS games.
Your question is regarding the terminal, command prompt.
There are two ways you can accomplish this. Run the program from a previously opened terminal (command prompt), or just have add cin statement (cheap way).
The last one would rewrite your code as:
#include %26lt;iostream%26gt;
using std::cout;
using std::cin; //don't recall if it is : or :: what you write there.
int main(){
cout %26lt;%26lt;"Hello World";
char a;
cin %26gt;%26gt; a;
return 0;
}
The cin statement will keep the program running until you press enter. At this point, the first of whatever cin got from the keyboard buffer it will allocate on a, the program will terminate, deallocating a and all memory used by the program.
Notice that since you are only using cout and cin, you don't need to include the entire namespace, like you did, but only include those two statements you are using. You can omit my
using std::cout;
using std::cin;
and replace it with your
using namespace std;
but this generates less optimal code.
sweet pea
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment