Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 20th, 2006, 12:02 AM   #1
bconnor
Newbie
 
Join Date: Mar 2006
Posts: 9
Rep Power: 0 bconnor is on a distinguished road
How would i

(a)Modify play_again3.c to ignore keyboard signals and only respond to yes or no input.

(b)Modify play_again3.c so; upon receiving a keyboard signal, it resets the terminal attributes and exits with a value of 2.

/* play_again3.c
 *	purpose: ask if user wants another transaction
 *	 method: set tty into chr-by-chr, no-echo mode
 *		 set tty into no-delay mode
 *		 read char, return result
 *	returns: 0=>yes, 1=>no, 2=>timeout
 *	 better: reset terminal mode on Interrupt
 */
#include	<stdio.h>
#include	<termios.h>
#include	<fcntl.h>
#include	<string.h>

#define	ASK		"Do you want another transaction"
#define	TRIES	   3	                                   /* max tries */
#define SLEEPTIME  2                                       /* time per try */
#define	BEEP       putchar('\a')                           /* alert user */

main()
{
	int	response;

	tty_mode(0);				/* save current mode	*/
	set_cr_noecho_mode();			/* set -icanon, -echo	*/
	set_nodelay_mode();			/* noinput => EOF	*/
	response = get_response(ASK, TRIES);	/* get some answer	*/
	tty_mode(1);				/* restore orig mode	*/
	return response;
}
get_response( char *question , int maxtries)
/*
 * purpose: ask a question and wait for a y/n answer or maxtries
 *  method: use getchar and complain about non-y/n input
 * returns: 0=>yes, 1=>no, 2=>timeout
 */
{
	int	input;

	printf("%s (y/n)?", question);			/* ask		*/
	fflush(stdout);					/* force output	*/
	while ( 1 ){
		sleep(SLEEPTIME);			/* wait a bit	*/
		input = tolower(get_ok_char());        	/* get next chr */
		if ( input == 'y' )
			return 0;
		if ( input == 'n' )
			return 1;
		if ( maxtries-- == 0 )			/* outatime?	*/
			return 2;			/* sayso	*/
		BEEP;
	}
}
/*
 *  skip over non-legal chars and return y,Y,n,N or EOF
 */
get_ok_char()
{
	int c;
	while( ( c = getchar() ) != EOF && strchr("yYnN",c) == NULL )
		;
	return c;
}

set_cr_noecho_mode()
/* 
 * purpose: put file descriptor 0 into chr-by-chr mode and noecho mode
 *  method: use bits in termios
 */
{
	struct	termios	ttystate;

	tcgetattr( 0, &ttystate);		/* read curr. setting	*/
	ttystate.c_lflag    	&= ~ICANON;	/* no buffering		*/
	ttystate.c_lflag    	&= ~ECHO;	/* no echo either	*/
	ttystate.c_cc[VMIN]  	=  1;		/* get 1 char at a time	*/
	tcsetattr( 0 , TCSANOW, &ttystate);	/* install settings	*/
}

set_nodelay_mode()
/*
 * purpose: put file descriptor 0 into no-delay mode
 *  method: use fcntl to set bits
 *   notes: tcsetattr() will do something similar, but it is complicated
 */
{
	int	termflags;

	termflags = fcntl(0, F_GETFL);		/* read curr. settings	*/
	termflags |= O_NDELAY;			/* flip on nodelay bit	*/
	fcntl(0, F_SETFL, termflags);		/* and install 'em	*/
}

/* how == 0 => save current mode,  how == 1 => restore mode */
/* this version handles termios and fcntl flags             */

tty_mode(int how)
{
	static struct termios original_mode;
	static int            original_flags;
	if ( how == 0 ){
		tcgetattr(0, &original_mode);
		original_flags = fcntl(0, F_GETFL);
	}
	else {
		tcsetattr(0, TCSANOW, &original_mode); 
		fcntl( 0, F_SETFL, original_flags);	
	}
}
bconnor is offline   Reply With Quote
Old Apr 20th, 2006, 12:07 AM   #2
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
a) You could try something to this extent:
if (input != 'y' && input != 'n')
{
     printf("bad input, sorry!!!");
     /* do what ever it is you do with bad input */
}
Not sure about (b) though.
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old Apr 20th, 2006, 2:08 AM   #3
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
How Many Points Do You Get On Each Question??
hbe02 is offline   Reply With Quote
Old Apr 20th, 2006, 3:28 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
What keyboard signal do you want to receive?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 20th, 2006, 7:37 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Handling of input is platform and OS specific. Best to learn that now, for several reasons, including what information you need to put in your posts. The standard libraries will cover a lot of what one normally needs, but not all.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC