-
why does the getch() run first no matter what? How can i make the getch run after i say like "enter a key"
Code:
#include <conio.h>
#include <iostream.h>
int get;
int keypress(){
get = getch();
return get;
}
void main(){
cout<<"Press a key\n";
keypress();
cout<<get<<"\n";
}
-
try this:
Code:
#include <conio.h>
#include <iostream.h>
int get;
int keypress(){
get = getch();
return get;
}
void main(){
cout<<"Press a key\n";
flush(cout);
keypress();
cout<<get<<"\n";
flush(cout);
}
alternatively, you can do this:
Code:
#include <conio.h>
#include <iostream.h>
int get;
int keypress(){
get = getch();
return get;
}
void main(){
cout<<"Press a key" << endl;
keypress();
cout<<get<<endl;
}
endl sends a new line, and flush's the output buffer, ensuring everything displays correctly.
-
Because when you send something to any output device, it is nearly always buffered. So, you tell it to send the string, and it then waits for the key. When your program finishes, it flushes the buffers and prints it out.
So to fix this, use "endl", which is the same as a newline and a buffer flush:
Code:
#include <iostream.h>
#include <conio.h>
void main() {
cout << "Press a key" << endl;
cout << getch() << endl;
}
-
Oh...hehe...
Beaten by 2 minutes... :)