Sign in to follow this  
RaPfA

a small bight of help

Recommended Posts

how do i change this to a nother key for example the shift key cout << "Press the enter key to exit"; cin.ignore(std::cin.rdbuf()->in_avail() + 1); please answer

Share this post


Link to post
Share on other sites
That method detects any input, but you won't see anything until the enter key is pressed because the console is line-buffered - your program doesn't actually see any input data until the return, so there's nothing simple you can do.

Why do you want to do this? It may be that what you're trying to do is inappropriate for a console app, or that you're trying to learn things "out of order" by worrying about small things like this before you've really got the fundamentals down - or both.

Share this post


Link to post
Share on other sites
If I understand you correctly, this is one way to do it in Win32:


int main(void)
{
std::cout << "Press the shift key to exit" << std::endl;

while(!GetAsyncKeyState(VK_SHIFT))
Sleep(100);

return int();
}







P.S.
You're going to have to include the Windows header.

Edit: Also, if you're looking to change the target-key to some character, then this would work too:


//Code

while(_getch() != '\r') //In this instance, this will break when enter is pressed
Sleep(100);

//Some more code




[Edited by - raz0r on March 7, 2006 12:07:25 PM]

Share this post


Link to post
Share on other sites
it is just that when i run this program

// My own thing

#include <iostream>
using namespace std;

int main()
{
string leader;
string felling;

cout << "Type your name here: ";
cin >> leader;

cout << "\nHello " << leader << " how are you?: ";
cin >> felling;

cout << "\nWell that is " << felling << ".\n";

cout << "Press the enter key to exit";
cin.ignore(std::cin.rdbuf()->in_avail() + 1);

return 0;
}


it dosent show the whole program
this ocors in other instances aswell

can you help

Share this post


Link to post
Share on other sites
Quote:
Original post by RaPfA
it is just that when i run this program

// My own thing

#include <iostream>
using namespace std;

int main()
{
string leader;
string felling;

cout << "Type your name here: ";
cin >> leader;

cout << "\nHello " << leader << " how are you?: ";
cin >> felling;

cout << "\nWell that is " << felling << ".\n";

cout << "Press the enter key to exit";
cin.ignore(std::cin.rdbuf()->in_avail() + 1);

return 0;
}


it dosent show the whole program
this ocors in other instances aswell

can you help


What exactly do you mean? The code that you pasted looks okay to me...

The output should be something like this, atleast from what I can see:

Type your name here: raz0r
Hello raz0r how are you?: good
Well that is good.
Press the enter key to exit.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this