C++ Learning Community Forum
August 01, 2010, 03:18:36 AM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
News
: Hello.
Home
Help
Search
Login
Register
C++ Learning Community Forum
>
General C++ Programming
>
C++ Help
>
C Programming
>
Properly get a single character.
Pages:
1
[
2
]
« previous
next »
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!
Re: Properly get a single character.
«
Reply #15 on:
September 19, 2008, 02:59:06 AM »
Quote from: C-Man on September 17, 2008, 10:12:34 PM
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
Re: Properly get a single character.
«
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
Re: Properly get a single character.
«
Reply #17 on:
December 28, 2008, 07:01:29 AM »
Quote from: oldneuro 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.
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
Re: Properly get a single character.
«
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
Re: Properly get a single character.
«
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
« previous
next »
Jump to:
Please select a destination:
-----------------------------
General C++ Programming
-----------------------------
=> C++ Help
===> C Programming
=> C++ Tips
=> C++ in General
=> 3rd Party Libraries Programming
-----------------------------
Platform Specific Programming
-----------------------------
=> Windows
=> Linux
=> DirectX
=> OpenGL
-----------------------------
C++ Creations
-----------------------------
=> Finished Products
=> Work in Progress
-----------------------------
Resources
-----------------------------
=> Recommended Resources
=> Bookworm's Corner
===> Accelerated C++: Practical Programming by Example
===> Code Complete
-----------------------------
Other than C++....
-----------------------------
=> Off Topic
=> Other Programing Languages
===> ASM Programming
=> Computer Lab
=> The Community
Loading...