int main() {
std::cout << "Hello World..." << std::endl;
//Text Below Displays when you push a key
[//Tried using 2 system("pause"); but made it say "push any key" on 2 separate lines. ]
std::cout << "Wonder if this works..." << std::endl;
system("pause");
return 0;
#include <iostream>
#include <conio.h> //this makes getch() work.
usingnamespace std;
int main () {
cout << "hello world";
getch();
return 0;
}
i give you this code just to get you started and i'm friendly to n00bs like me LOL, but please being 13 years old is not an excuse not to search before you post.. it's probably been discussed by other threads before..
Too many people are referring me to a recent Topic about Console Closing. That discussions isn't very direct, offers many different possibilities that don't seem to work.
So what do you say is the final verdict for a pausing phrase?
#include <iostream>
#include <limits.h> /* For std::numeric_limits<std::streamsize>::max() */
externvoid exit(int); /* Save us including the whole cstdlib header */
class keepRunning {
public:
~keepRunning() {
std::cout << "Press ENTER to continue (terminate program).\n" << std::endl;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
exit(1); /* Error occurred */
}
};
int main() {
try {
keepRunning kr;
/* Put your main function here */
} catch (...) {
std::cout << "Exception occurred.\n";
/* Handle the exception */
}
return 0;
}
The try...catch block may be unnecessary, because the kr object's destroyer (~keepRunning()) has to be called when the program terminates, regardless of how. It allows you to exit gracefully, or at least, more gracefully.
Too many people are referring me to a recent Topic about Console Closing. That discussions isn't very direct, offers many different possibilities that don't seem to work.
So what do you say is the final verdict for a pausing phrase?
we all have our own style..
i suggest you forget about this closing console thing and use getch() or system("pause") in the mean time. instead go ahead and learn c++ and then comeback to this thread when you have learned enough to understand what they the links they have posted mean.. that's the time you make a choice..
you have posted in another thread that you don't even know how to compile a hello world program right?
well that's just my opinion..
NOTE:
i do not intend to offend, and i do not claim to be an expert.. i am also a n00b
Until you actually plan on getting serious you can get by on system("pause"), but I don't think you gave that thread a really good read. All of Duoas's solutions work well. The cin.ignore is standard, although it takes an enter and not the any key. If you are interested in going api style you can move up to the more advanced stuff he later posted for windows/linux api. But try to avoid getch() and system if you can. And it's more than doable. You don't need to understand that code to use it but it's reasonably well-explained and commented.
If you're confused about what ropez was typing, it's fairly simple and it's explained in that thread, and it is quite clever. Basically, he creates a class whose destructor calls a press any key to continue sort of function. Anything goes. He declares one object of that class on the stack, so when the program ends by any means, it must call the destructor, forcing that any-key-press code to be called.
EDIT: nice disclaimer, blackcoder.
Actually cin.ignore does accept sequence delimiting character codes. Personally I just use cin.ignore(100,'/n'). Of course I just use that in my own personal programming I would not suggest using that function for rather large inputs, but then again what program actually has large inputs like that from the console?
I guess for now I will just use system("pause");. I did post a thread about the Hello World thing.... the problem was because I wasn't using a Win32 Console app.
you could just use a getline() which does the same thing as press the enter key...
for the time being at least, personally I don't think it's a good idea to practise poor habits, at least getline is non-destructive. (you only need to be aware of cin >> and getline())
... yeah, all good ideas; but you could, you know, do it properly and use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); in a class destructor like I showed you above (which ropez came up with).
Actually cin.ignore does accept sequence delimiting character codes. Personally I just use cin.ignore(100,'/n'). Of course I just use that in my own personal programming I would not suggest using that function for rather large inputs, but then again what program actually has large inputs like that from the console?
Duoas showed why that's a bad idea. I'll show you again. This line reaches 100 characters| at the pipe symbol ('|'). Why not use std::numeric_limits<std::streamsize>::max()? Then cin.ignore() will keep ignoring input until either the most characters it could possibly ignore are input, or it finds a newline/linefeed ('\n').
cin.ignore(numeric_limits<streamsize>::max(), '\n')
That is the right platform-portable way to do it. (I removed the std::). If you use get() or getline() and there's a \n left in the buffer your program still executes beyond your control. There is no platform-portable way to do it better than that way.
That means no getch(), no system(), and (the second-best solution generally) get() or getline().