Hey, I'm a high school student who is fairly programming and I was curious as to how to input a single character without entered an End of Data marker (e.g. newline character/Enter)

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

int main ()
{
    char ch;
    switch (toupper(getchar()))
    {
           case 'A':
                puts("You entered the letter A");
                break;
           case 'B':
                puts("You entered the letter B");
                break;
           default :
                   puts("You entered niether A nor B");
                   break;
    }
    system("pause");
}

The problem I found was that
fgetc(),scanf(),fscanf(),getchar() all require you to press the Enter button in order for you to retrieve you input. I would like function or solution which allows me to input the character without pressing enter.

A practical example as to what I mean is in the command prompt the command pause does not force you to press enter, it waits for you to input a character and discards it.

#include <stdlib.h>

int main()
{
    system("pause");
}

I would like a solution to do exactly this except I do not want to discard the character entered.
(sorry if I was repetitive or redundant)

Recommended Answers

All 14 Replies

conio.h (if available to you) has the function _getch() which is unbuffered and does not echo the character read.

conio.h (if available to you) has the function _getch() which is unbuffered and does not echo the character read.

Do you know any site where i can get information on other functions in this header, I like to learn as much as I can about header files I use. Also thank you for your help, it worked perfectly.

conio.h (if available to you) has the function _getch() which is unbuffered and does not echo the character read.

i wish i could give you half a cookie, for your halfassed solution.

sure you can write a program with conio.h, but it will be broken on most every non-microsoft systems.

conio.h is a header file used in old MS-DOS compilers to create text user interfaces, however, it is not part of the C programming language, the C standard library, ISO C or required by POSIX. ... Most C compilers that target UNIX and Linux do not have this header and do not supply the concomitant library functions.

-- wikipedia

well jephthah, do you have a better suggestion?

>sure you can write a program with conio.h, but it will be broken on most every
>non-microsoft systems.
And you can write a termios.h solution that will be broken on every non-POSIX system. What's your point? You can't portably change the shell's input mode without the use of a third party library.

The full-cookie solution would be to use conditional compilation to implement code for each platform. Here is an MSDN page describing the functions in conio.h. Remember that you can simply open these headers up and look at them to see what's in them.

commented: yeah, that's it. +5

Hey, I'm a high school student who is fairly programming and I was curious as to how to input a single character without entered an End of Data marker (e.g. newline character/Enter)

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

int main ()
{
    char ch;
    switch (toupper(getchar()))
    {
           case 'A':
                puts("You entered the letter A");
                break;
           case 'B':
                puts("You entered the letter B");
                break;
           default :
                   puts("You entered niether A nor B");
                   break;
    }
    system("pause");
}

The problem I found was that
fgetc(),scanf(),fscanf(),getchar() all require you to press the Enter button in order for you to retrieve you input. I would like function or solution which allows me to input the character without pressing enter.

A practical example as to what I mean is in the command prompt the command pause does not force you to press enter, it waits for you to input a character and discards it.

#include <stdlib.h>

int main()
{
    system("pause");
}

I would like a solution to do exactly this except I do not want to discard the character entered.
(sorry if I was repetitive or redundant)

use getche() function from conio.h

And you can write a termios.h solution that will be broken on every non-POSIX system. What's your point?

that my indignant self-righteousness only flows in one direction: from GNU/GPL and against MSFT.

it ain't right, but i can still sleep most nights.

commented: Hahaha! +14

You're pathetic.

You're pathetic.

did i really piss in your cheerios, mate? is it so bad you're reduced to foaming about the mouth uttering unqualified personal attacks?

John A. rightly pointed out that a termios or ncurses solution would not be any better than your conio solution, just from the other direction.... and perhaps you missed it, but my attempt at self deprecating humor conceded the point. And your followup post about conditional compilation was right on the money, so it's obvious you "get it"

come on now, let's be friends, nucleon, you and me. we can go bowling or something :P


.

You're pathetic.

come on now, let's be friends, nucleon, you and me. we can go bowling or something :P

Some people don't get your twisted way of thinking/humour and mistakenly think that your making fun of them. That's what you get with a forum full of geeks.

Off-topic: bowling sucks, big time. :)

ok, all of this matters not. for now I am ok with _getch and getche, if the problem arises again I and need to get more indept then just expect another thread. Thanks for the help ^_^

don't go yet.... we still need to argue about winsock vs. sockets!

don't go yet.... we still need to argue about winsock vs. sockets!

oh wow, um. I really appreciate the enthusiasm but I am a high school student who is a self taught "Programmer". In english this means I am not familiar with half of this stuff so if you decided to have an argument you totally need to explain what all of this stuff is all about. for example winsock verses sockets and conditional compilation to implement code for each platform? Part of being effective help is being able to relate to the level of education and understand of the person your helping. Just thought I would give you guys some constructive criticism, but the help is appreciated and mostly understood. So, now that we have that out of the way. Do you still want to try explaining winsocks and sockets? (I hope winsocks keep my feet warm on windy days, sigh* so much technology and they still can't keep your feet warm in unpleasant whether, they should stop worrying about visual studio 2010 and come up with something that matters... lol)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.