25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more...
Home   Projects   Up   Sitemap   Search   Blog   Forum+Chat   About Us   Privacy   Terms of Use   Feedback   FAQ   Images   Services   Payments   Humor   Music

Console user input functions in DeSmet C

On this page are functions that used to be commonly needed in console applications:

  • Ask the user if they want to continue. 
  • Pause the display after one screenful, saying "Press any key to continue..."
  • Ask which disk drive should be used for file reads/writes. 
  • Get a numeric value from the user (integer, double) with a default value they can press Enter to accept. 
  • Input data from a file using a function equivalent to the BASICA statement INPUT #1, A$
/* ===========================================================*/
/* INPUT FUNCTIONS */
/* ===========================================================*/
/* -------------------------------------------------------------- */
/*	contin()	3-2-94  	*/
/*	Copyright (C)1994 Steven Whitney. Published under the */
/*	GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY. */
/*	Initially published by http://25yearsofprogramming.com. */
/*	returns TRUE if user wants to continue, FALSE if not */
/* -------------------------------------------------------------- */
#define TRUE	(1)
#define FALSE	(0)
int contin()
{
char ch, tolower();

do
{
	puts("\nContinue? (y/n): ");
	ch = tolower((char)getchar());
	puts("\n");
}
while((ch != 'y') && (ch != 'n'));
if(ch == 'y')
	return(TRUE);
return(FALSE);
}


/* ----------------------------------------------------------------------- */
/*	PRESSKEY()  3-3-94 */
/*	Copyright (C)1994 Steven Whitney. Published under the */
/*	GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY. */
/*	Initially published by http://25yearsofprogramming.com. */
/*	says press any key to continue, and waits for a key */
/*	erases its message, but moves to new line anyway so that if echoed */
/*	to printer with ^P, subsequent text won't overwrite it.*/
/* ----------------------------------------------------------------------- */
int presskey()
{
	puts("Press any key to continue...");
	ci();
	puts("\r                            \n");
	return(1);
}

/* ----------------------------------------------------------------------- */
/*	 GETDRV()   06-08-87  5-1-93 Function obtains drive name to use.   */
/*	Copyright (C)1987, 1993 Steven Whitney. Published under the */
/*	GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY. */
/*	Initially published by http://25yearsofprogramming.com. */
/*   Function returns one character, which is a drive name. */
/*  But the returned character is returned in integer form. */
/* ----------------------------------------------------------------------- */
int getdrv()  
{
	char dr, legal[6], *strcpy(), toupper(), *index();
	void printf();
	strcpy(legal,"ABCDI");    /* legal drive names on system */
	do
	{
		printf("\nWhich disk drive should be used <%s>:  ",legal);
		dr = toupper((char)(getchar()));
	}
	while((index(legal,dr)) == 0);      
	putchar('\n');
	return((int)dr);
}



/* ----------------------------------------------------------------------- */
/* GETINT() 6-4-87 10-12-93 */ 
/* Copyright (C)1987, 1993 Steven Whitney. Published under the */
/* GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY. */
/* Initially published by http://25yearsofprogramming.com. */
/* generalized variable input routine for INTEGERS only */
/* ----------------------------------------------------------------------- */
int getint(varaddr,desc)
int *varaddr;		/* current value used as default if user presses <cr> */
char desc[];
{
	void printf();
	char temp[81], *gets();
	printf("\nEnter %s or <cr> for %d :  ",desc,*varaddr);
	gets(temp);
	if(!strlen(temp))
		return 1;
	else
	{
		sscanf(temp,"%d",varaddr);
		return 1;
	}
}


/* ----------------------------------------------------------------------- */
/* GETDBL() 6-4-87 10-12-93 */
/* Copyright (C)1987, 1993 Steven Whitney. Published under the */
/* GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY. */
/* Initially published by http://25yearsofprogramming.com. */
/* generalized variable input routine for doubles only */
/* ----------------------------------------------------------------------- */
int getdbl(varaddr,desc)
double *varaddr;
char desc[];
{
	void printf();
	char temp[81], *gets();
	printf("\nEnter %s or <cr> for %f :  ",desc,*varaddr);
	gets(temp);
	if(!strlen(temp))
		return 1;
	else
	{
		sscanf(temp,"%lf",varaddr);
		return 1;
	}
}


/* ----------------------------------------------------------------------- */
/*  FGETBSTR()  6-4-87  10-12-93  */
/*	Copyright (C)1987, 1993 Steven Whitney. Published under the */
/*	GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY. */
/*	Initially published by http://25yearsofprogramming.com. */
/*	Gets a string from a Basic-formatted sequential file (strings enclosed */
/*	in quotes and separated by commas. Emulates Basic function INPUT #1, A$. */
/*	(maxlen) should be one less than the space allocation for the string  */
/*	in the main program; this will leave room for the terminating null. */
/*	When this number is matched, no further characters are added to the   */
/*	string, but the function keeps searching for the real end of string  */
/*	so that the next call will begin with a legal starting point or an     */
/*	end of file condition which will signal something wrong with the file.   */
/*	This version requires that ALL file data be enclosed in quotes, and	 */
/*	it therefore allows commas to be imbedded in the strings.  */
/* */
/*  RETURNS:  0 if ok, ERR if end of file or something else is wrong. */
/*	*/
/*	3-2-94 For most purposes, this function is obsolete.  Instead: */
/*	fscanf(infile,"\"%[^\"]\",\"%[^\"]\",%d\n",bufa,bufb,&integer); */
/*		this will read this file:   "string","string",10 */
/*	9-25-94 FGETBSTR() may still be useful:  DeSmet C seems to have a */
/*	255(? don't recall)-character limit on the size of a ...scanf() field */
/*	being scanned.  Don't know if Borland has the same limitation, but */
/*	fgetbstr() can scan any length string from the input stream. */
/* ----------------------------------------------------------------------- */
#define	FALSE	0
#define	TRUE	1
#define	ERR		(-1)
#define	EOF		(-1)
typedef int FILE;
int fgetbstr(fileptr,strptr,maxlen) 
FILE *fileptr;
int maxlen;
char *strptr;
{
	char *orgptr;
	int c, t;
	int foundstart = FALSE;
	orgptr = strptr;   /* SAVE ORIGINAL POINTER VALUE FOR LENGTH CHECK */
	*(strptr) = '\0';     /* START WITH NULL STRING */
	while((c = fgetc(fileptr)) != EOF  && ((c & 127) != 26)) /* EOF CHECKS (-1,^Z) */     
	{
		if((foundstart == FALSE) && (c == '"' || c == ',' || c == 10 || c == 13))
		{		                     	/* LOOP PAST DELIMITERS & LINE ENDS */
			if((t = fgetc(fileptr)) == EOF) /* IF EOF COMES BEFORE START */
				return(ERR);     			/* OF STRING, RETURN EOF */
			if(t == '"' && c == '"')		/* TEST FOR "" POSSIBILITY */
				return(0);					/* ...WHICH IS OK */
			else							/* OTHERWISE, PUT THE CHAR BACK */
				ungetc(t,fileptr);	  /* BECAUSE IT WILL BE PART OF STRING */
			continue;             
		}						                            
		foundstart = TRUE;
		if(c != '"') /* end of string; other values could be comma,\r,\n */
		{                                       
			if(strlen(orgptr) < maxlen)  /* or exceeds max length */
			{
				*(strptr++) = c;        /* add input to string  */
				*(strptr) = '\0';       /* add \0 so test is valid  */
			}
			continue;      /* but continue searching in any event  */
		}
		return(0);
	}
	return(ERR);
}

 

 

Valid HTML 4.01 Transitional Valid CSS
Yahoo! Search
Search the web Search this site
View content labeling at ICRA.