public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Joachim <nuklear@singnet.com.sg>
To: "gcc@gcc.gnu.org" <gcc@gcc.gnu.org>
Subject: Newbie:getch??
Date: Sun, 20 Feb 2000 07:52:00 -0000	[thread overview]
Message-ID: <38B00E12.2C8022B5@singnet.com.sg> (raw)

Hi there!
I'm very new with gcc so please forgive me my stupid question!

I was wondering if there would be a function available that reads only
one character from stdin.
To be more specific I need something that works the same way as:
kbhit() or...
getch() or...
getche() alias Borlands <conio.h>

I need it to write an input function to handle my own keyboard
definitions. 
Unfortunatly getchar() is unsuitablee for what I want to do. 
Any glues on what to use?

Thank's  

	Joachim!
/*
19. Feb. 2000
Joachim Bauernberger
This is a list of features to be implemented for the Gas Tracker project.

Here are a couple of ideas for Gas Tracker that are floating around in my head
and scream to be put to paper 8-)

1)	Rewrite the input function in the following way:

-->	check for keys pressed and accept the following:
    a) digits
    b) . use with float
    c) Y = YES
    d) N = NO
    e) H = HELP
    f) S = SAVE
    g) Q = QUIT
    h) C = Calculate
    i) arrow keys

Note:
* In case of a yes or no answer we have to be able to accept toupper(y/n)
* The H key enables us to give guidance for lost souls 8-)
* The S key saves everything to a file.
* The Q key quits the program.

* The arrow keys help us in navigating through the different locations of the
  program. This way we can change some of the values that we don't like but
  have already entered earlier.
  We have to make sure that all calculation will start after the C butten is
  pusehed.

* This will make extensive use of pointers YIKES!



*/

#include <stdio.h>
#include <ctype.h>

#define CR 0x0d		           //carriage return (enter key)
#define ESC 0x1b               //Escape key
#define TAB 0x09               //Tab key
#define LF 0x0a                //Line feed
#define BACKSPACE 0x08         //Backspace
#define SAVE 0x13              //Ctrl-S for Save
#define HELP 0x08              //Ctrl-H for Help
#define QUIT 0x11              //Ctrl-Q for Quit
#define NULL 0		           //Empty character
#define TRUE 1
#define FALSE 0
#define LENGTH 10	           //size of the string

int input(char *string, int length);

int main()
{

	char string[LENGTH];

        printf("Enter the text:\n");
        if (!input(string,LENGTH))
        printf("You entered: %s!\n",string);
        else
        printf("\n***** CANCELED. *****");
        return 0;
}

int input(char *string, int length)
{
    	int done = FALSE;
        int cancel = FALSE;
        int index = 0;
        char ch;

        	string[0] = NULL;	//initialize the buffer

                do {
                	ch = getc(stdin);      /*we need to change this to something that accepts only one char!*/

                        /*check to see whether the buffer is full*/
                        if (index == length) {
                        	switch (ch) {
                                	case CR:
                                        	break;
                                        default:
                                        	putchar('\a');
                                        	ch = NULL;
                                                break;
                                        } //end switch
                        }  //end if

                /*process the keyboard input*/

                switch(ch) {
                	case CR:  //enter key
                        	putchar(ch);
                                putchar(LF);
                        	string[index] = NULL;
                                done = TRUE; //set variable to exit while after break
                                break; //break out of switch

                        case NULL:
                        	break;

                        case BACKSPACE:
                            if (index == 0) { //bounds checking if the index is 0

                            	break;
                            }
                            else
                        	putchar(ch);
                                putchar(' ');
                                putchar(ch);
                                index--;

                                break;

                        case ESC:
                                done = TRUE;
                                cancel = TRUE;
                                break;

                        default:
                        	putchar(ch);
                                string[index] = ch;
                                index++;

                                break;
                        }  //end switch
                } while (!done);   //end do-while
                return(cancel);
} //end

             reply	other threads:[~2000-02-20  7:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-02-20  7:52 Joachim [this message]
2000-02-20 10:07 ` Newbie:getch?? Erik Mouw
2000-03-16  7:45 Newbie:getch?? Jose Camargo
2000-03-16 10:17 ` Newbie:getch?? Mo McKinlay

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=38B00E12.2C8022B5@singnet.com.sg \
    --to=nuklear@singnet.com.sg \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).