LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2007, 02:09 AM   #16
geo_gt
LQ Newbie
 
Registered: Nov 2004
Distribution: OpenSUSE 11.3 64bit Intel Core2 Quad Core Q9300, 4GBRAM
Posts: 9

Rep: Reputation: 0

Hi all,
a little late post but it might helps.

You said that nothing works. Depends what you mean. If you use getch() but the applications doesn't wait for that key, then that's probably because this getch() takes the last '\n' from a previous input.

Try to use 2 or even three times getch() and decide what's best for you.
For example

//Wait for a key pressed
getch();
getch();
getch();


Regards,
geo
 
Old 05-14-2014, 05:31 PM   #17
darkscrap
LQ Newbie
 
Registered: May 2014
Posts: 1

Rep: Reputation: Disabled
Thumbs up Late, but hopefully helpful

Hey all. Been reading through this and I'm really surprised that nobody mentioned clearing the input buffer. It's pretty darn likely that "getchar()" or anything else besides a "cin" won't actually wait for a user's input because there is garbage sitting in the input buffer.

You can very easily use getchar() by first clearing the input buffer. DON'T use loops or anything. Seems silly... Just clear the buffer. You should be clearing the buffer before and after you use any "getline" statement anyway, for safety reasons.
Code:
#include <iostream>
using std::cout;   // Specify only the functions you're going to use
using std::cin;    // to cut down on file sizes

int main()
{
//Stuff 'n things

cout << "Press <ENTER> to quit...\n";
cin.ignore( cin.rdbuf()->in_avail() );
getchar();
cin.clear();
cin.ignore( cin.rdbuf()->in_avail() );

return 0;
}
Granted, this doesn't answer the original question of accepting ANY character, because the input stream automatically recognizes a carriage return (or ENTER key) as the terminating character of the string. So you have to hit ENTER at some point. But at least this method is simple and works efficiently.

Hope that helps a bit. Cheers! :-)
 
Old 05-14-2014, 10:22 PM   #18
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,881
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Just for the record: it's 'termios' on linux.
Code:
    struct termios oldtio, newtio;
    int rc;

    rc= tcgetattr (0, &oldtio);
    if (rc) {
        perror ("tcgetattr (0, &oldtio)");
	exit (4);
    }
    memcpy (&newtio, &oldtio, sizeof (newtio));
    newtio.c_lflag &= ~(ISIG | ICANON | ECHO);
    newtio.c_iflag &= ~(INLCR | ICRNL | IXON | IXOFF);
    newtio.c_cflag &= ~(PARENB);
    newtio.c_cflag = (newtio.c_cflag & ~CSIZE) | CS8;
    newtio.c_cc[VMIN] = 1;
    rc= tcsetattr (0, TCSAFLUSH, &newtio);
    if (rc) {
	perror ("tcsetattr (0, TCSAFLUSH, &newtio)");
	exit (8);
    }
    ...
    tcsetattr (0, TCSAFLUSH, &oldtio); /* reset */

Last edited by NevemTeve; 05-14-2014 at 10:24 PM.
 
Old 05-19-2014, 11:09 AM   #19
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Rep: Reputation: 26
Seems like this thread has been solved.. I would cal it solved up above on the page
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Key press Majestros Programming 2 02-14-2007 03:42 AM
Double key press on Keyboard Zettai Linux - Newbie 2 03-09-2005 10:59 PM
Keyboard Problem - I have to press a key twice for certain keys daedius Linux - Hardware 4 01-13-2005 06:32 AM
Insert Boot-Disk and press any key computerlady911 Linux - Laptop and Netbook 7 10-04-2004 12:56 PM
What key to press in order to type symbols? windz Linux - General 0 02-10-2004 06:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration