I have tried to write system("pause"); in DevCpp, but it doesn't work until I include a C++ library so DevCpp knows it is a C++ program. What do you use in C instead? getch()?
I have tried to write system("pause"); in DevCpp, but it doesn't work until I include a C++ library so DevCpp knows it is a C++ program. What do you use in C instead? getch()?
No its not standard. Use getchar insteadOriginally Posted by TriKri
system() itself should be standard (although the "pause" part is probably platform specific), but you'll need to include <stdlib.h> (I think) to use it.
In DevCpp the C++ headers include it for you, that's why you don't apparently need to include it when using <iostream> for example.
You need the <stdlib.h> header to use system (as mentioned); in Standard C++, this would be <cstdlib> (and you'd need a using statement or use std::system). It executes a system (DOS) command, and so is unportable, unsafe, slow, and generally undesireable and unrecommended.
Some compilers support getch(), which usually gets a keypress without the user having to hit enter; Dev-C++ has it in <conio.h>. It too is non-standard, though.
For the standard solution, use getchar() (for C). (You'll have to press <enter>, though.) Something like this:
This is covered in this FAQ, which tells you to read this one.Code:int c; /* must be int to hold EOF */ while((c = getchar()) != '\n' && c != EOF);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Ok, thank's for all the answers.