I have written a console program. It should wait untill user presses a key. What function should I use to get a key from keyboard?
I have written a console program. It should wait untill user presses a key. What function should I use to get a key from keyboard?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
You can use fgetc(), or getch() if your compiler has conio.h
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
It works. Thanks. Last time I tried to use getc() from stdio.h but it didn't work as I wanted.
Now, how can I flush the buffer?
Last edited by siavoshkc; 01-18-2006 at 05:15 AM.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
When I use
It works fine.Code:cout<<"Press a key to exit..."<<endl; getch();
But when I write
It doesn't print last line to screen. But after I press a key it prints it and exits. Why?Code:cout<<"Press a key to exit..."; getch();
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
you are mixing c++-style io-functions ( cout << ... ) and c-style functions ( getchar() ). that is never a good idea.
if you would just use
you would not have that problem because cin and cout are synchronized.Code:char x; cout<<"Press a key to exit..."; cin >> x;
if you still want to use your way you could flush cout before waiting for a char.
KurtCode:cout<<"Press a key to exit..."; cout.flush(); getchar();
I'm pretty certain it has something to do with endl - IIRC it flushes the output buffer. Not sure if that's a problem, someone else will have to verify that.
Note that getchar() and getch() are two different functions, he's using the latter.
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
That is right outputting endl on cout will automatically flush the buffer. '\n' at the end of the string would not. But the OP didn't even have a '\n' at the end of his outputstring I assumed that he wants the curser to stay on the same line. In that case flush is the only way to make shure the string gets displayed.Originally Posted by ahluka
Ok I missread that. But that makes it even worse. Don't think that a nonstandard function will be synchronized with cout.Originally Posted by ahluka
Kurt
That is what I have always used.Code:cin.get();
OK. And what about flushing input stream?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
if you want them to press "any" key to exit, just do something like this...
Code:int main() { //whatever you want to go here system("pause");//waits till user hits any key }
What is it? Is it a system function? What should be included?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Two varients:
andCode:cout<<"Press a key to exit..."; cout.flush(); getch();
The second code is better because there is no need to flush the input buffer. My program works fine now, thanks a lot.Code:system("pause");//waits till user hits any key
But I still have two questions:
1) How can I flush input stream? (With cin.clear()?)
2) What is this magical function "system()"?
Last edited by siavoshkc; 01-19-2006 at 12:57 AM.
Except system() hands control over to the program it calls without regard to which program it calls, which could potentially be replaced by a malicous program - security risk. Not that it's much of an issue here, just something to keep in mind. The FAQ has an article on this.Originally Posted by siavoshkc
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
if you're using MS VC++6, system is defined in header <stdlib.h> and you're using some form of windows too (dumb point but better safe than sorry - imagine MS VC++ for Unix!!?)
what it does? lots!!!
go in to start menu -> run: cmd.exe (or command.exe - i'm not sure if its version specific). this will load the windows command line interface or shell, which is most commonlly thought of as MS-DOS. it should look just like your console program
try typing pause.
now go into your code (include the header) and have this in your code:
system ("pause");
run it - see any similarity?
the system () is a cheap and easy way to do lots of things that are not so often as easy in C/C++. it allows you to use shell commands in your own program, and there are a lot of them! the syntax is as above, just remember the quotation marks! a personal favorite of mine is system ("cls"); clears the console of all text - no need for a custom function. another good one is system ("color (hex_number)"); changes the *colour formatting of the console. type in color -ls into the shell to see how it works.
sounds cool!!? thought so. why haven't you been told!!? cos its not as cool as it looks...
system () is generally frowned upon by serious programmers because as a previous post said,
"system() hands control over to the program it calls "
an example of this is to use it to open a text file or some program. save your executable in a specific directory and create a text file with something in it, in the same directory.
in the middle of your code, call
system ("something.txt");
you should see notepad open up, but your program is stalled in its place - it waits for the shell to return control to the program. close the text file, and your program resumes! it is also important that the item your are opening is in the same directory as your executable, i don't know how to change the path that it looks for it in just yet, but i'm not particularly concerned.
here's a link to a microsoft site that has a load of shell commands for windows xp, most are common to earlier editions as well:
http://www.microsoft.com/resources/d...us/ntcmds.mspx
Have fun tinkering with the system from the inside!
* i am aware that i spelled colour here, that is how it is spelt in european (i.e. original) english. windows was written with american english or just "american," as they like to think of their dialect as a unique language! no offence meant to anyone from america, great place and all but you're all a bunch of jackasses if y'all think y'all can come up with a new language by changing a few spellings from an older one! i've had my rant, now i'll leave before i get a lynch mob after me!