C++ Learning Community Forum
August 01, 2010, 03:18:36 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: 1 [2]
  Print  
Author Topic: Properly get a single character.  (Read 2773 times)
mleveill
1337 L0LCoder
C++ Freak
***
Posts: 430


IM IN UR PROGRAMZ WRITIN UR CODEZ!


View Profile
« Reply #15 on: September 19, 2008, 02:59:06 AM »

hush your mouth , C has it's place even today.


seconded
Logged

Quote from: kohlrak
Common interests aren't always the best thing. Especially programming wise.
I could just picture a couple fighting over weather to "xor eax by eax" or "and eax by 0"...
Then kids'll come home and argue with their parents that readabilty is better than size and that "mov eax, 0" would be best because that's "more readable." (Even if it really isn't...)

LOLCode - http://lolcode.com/home
oldneuro
N00b!!1
*
Posts: 17


View Profile
« Reply #16 on: December 25, 2008, 11:36:44 AM »

To get just a single character from stdin, there is no way in the standard C library.

To do it, you need to use the non-standard function "getch".  It is part of ncurses on Linux, and part of io.h on Windows.

#ifdef _WIN32
#include <io.h>
#else
#include <curses.h>
#endif

int main() {
  char c = '\0';
  printf("press 'q' to quit\n");
  while (c != 'q') {
    c = getch();
  }
  return 0;
}

This means that the program will have the added dependency of the ncurses library on Linux.
Logged
myork
Global Moderator
C++ guru
*****
Posts: 1147


View Profile
« Reply #17 on: December 28, 2008, 07:01:29 AM »

To get just a single character from stdin, there is no way in the standard C library.

What about getc().

Quote
     #include <stdio.h>

     int     fgetc(FILE *stream);
     int     getc(FILE *stream);
     int     getc_unlocked(FILE *stream);

     int     getchar(void);
     int     getchar_unlocked(void);

     int     getw(FILE *stream);
Logged
oldneuro
N00b!!1
*
Posts: 17


View Profile
« Reply #18 on: January 08, 2009, 05:22:15 AM »

getc (actually, fgetc(stdin)) only RETURNS a single character off the input stream, however, it does block until a newline or EOF is pressed.  In otherwords, getc() and fgetc(stdin) is exactly the same as getchar().

Hope that clears things up.  I encourage you to try out my suggestion for yourself to see what I mean.
Logged
oldneuro
N00b!!1
*
Posts: 17


View Profile
« Reply #19 on: January 08, 2009, 05:32:04 AM »

// to compile, use the following:
// on win32: cl getc.c
// on gcc: gcc -o getc getc.c -lcurses

#include <stdlib.h>
#include <stdio.h>
#ifdef _WIN32
#include <conio.h>
#define printtxt        printf
#else
#include <curses.h>
#define printtxt        printw
#endif

int main()
{
        char c = '\0';

#ifndef _WIN32
        initscr();
        cbreak();
        noecho();
        nonl();
        intrflush(stdscr, FALSE);
        keypad(stdscr, TRUE);
#endif
        printtxt("press 'q' to quit\n");
        while (c != 'q')
                c = getch();
#ifndef _WIN32
        endwin();
#endif
        return 0;
}
« Last Edit: January 08, 2009, 05:34:52 AM by oldneuro » Logged
Pages: 1 [2]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!