You are not logged in.

#1 2013-04-11 06:40:00

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

[C] getchar() not working?

Well, I'm making a big game. I use getchar(); to wait for ENTER from user.
Like this:

getchar();

It looks pretty valid, though.

But, this really annoys me, when, getchar() doesn't pause the program and wait for user input. It just, passes...

Maybe, all the input (fgets and scanf stuff) before the getchar(); must have interfered with the getchar();, i don't really know.

Please help...
Also, if you have a better code (other than getchar()), feel free to post it.


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#2 2013-04-11 18:53:45

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,019

Re: [C] getchar() not working?

#include <stdio.h>

int main() {
    puts("Testing getchar()");
    if(getchar() == 'y') puts("WIN");
    else puts("FAIL");
    return(0);
}

getchar() works fine here. You put so little info about what you are doing in your post there was nothing else I or anybody else could do to help you out...


You're just jealous because the voices only talk to me.

Offline

#3 2013-04-11 20:28:37

Ozymandias_117
Member
From: USA
Registered: 2010-08-30
Posts: 15

Re: [C] getchar() not working?

The one thing I would wonder is if your buffer isn't always empty, so there is something for getchar() to grab before the user inputs something else.

Offline

#4 2013-04-12 00:22:13

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 3,475
Website

Re: [C] getchar() not working?

I have two answers to your question:

  1. Use "fflush(stdin);" before you call "getchar"

  2. Don't use "fflush(stdin)" before you call "getchar"

milo64 wrote:

Also, if you have a better code (other than getchar()), feel free to post it.

There are many different ways to get character input. Use whatever language and library works for you. smile

Is there a special reason you're using C?

Last edited by drcouzelis (2013-04-12 00:23:45)

Offline

#5 2013-04-12 01:11:46

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 987

Re: [C] getchar() not working?

I will bet you ONE HUNDRED DOLLARS you only see this problem when the call to getchar() is preceded by a scanf().

Don't use scanf for interactive programs. There are two main reasons for this:

1) scanf can't recover from malformed input. You have to get the format string right, every time, or else it just throws away whatever input it couldn't match and returns a value indicating failure. This might be fine if you're parsing a fixed-format file when poor formatting is unrecoverable anyway, but it's the exact opposite of what you want to do with user input. Use fgets() and sscanf(), fgets() and strtok(), or write your own user input routines using getchar() and putchar().

1.5) Even properly used, scanf inevitably discards input (whitespace) that can sometimes be important.

2) scanf has a nasty habit of leaving newlines in the input stream. This is fine if you never use anything but scanf, since scanf will usually skip over any whitespace characters in its eagerness to find whatever it's expecting next. But if you mix scanf with fgets/getchar, it quickly becomes a total mess trying to figure out what might or might not be left hanging out in the input stream. Especially if you do any looping -- it's quite common for the input stream to be different on the first iteration, which results in a potentially weird bug and even weirder attempts to fix it.

tl;dr -- scanf is for formatted input. User input is not formatted. //

Last edited by Trent (2013-04-12 01:12:19)

Offline

#6 2013-04-12 06:47:25

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,019

Re: [C] getchar() not working?

Milo64, I was wrong. There was enough info for other people to help you out...
smile


You're just jealous because the voices only talk to me.

Offline

#7 2013-04-12 11:41:45

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

Re: [C] getchar() not working?

drcouzelis wrote:

I have two answers to your question:

  1. Use "fflush(stdin);" before you call "getchar"

  2. Don't use "fflush(stdin)" before you call "getchar"

milo64 wrote:

Also, if you have a better code (other than getchar()), feel free to post it.

There are many different ways to get character input. Use whatever language and library works for you. smile

Is there a special reason you're using C?

I use C because it's my main programming language.


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#8 2013-04-12 11:43:20

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

Re: [C] getchar() not working?

Trent wrote:

I will bet you ONE HUNDRED DOLLARS you only see this problem when the call to getchar() is preceded by a scanf().

Don't use scanf for interactive programs. There are two main reasons for this:

1) scanf can't recover from malformed input. You have to get the format string right, every time, or else it just throws away whatever input it couldn't match and returns a value indicating failure. This might be fine if you're parsing a fixed-format file when poor formatting is unrecoverable anyway, but it's the exact opposite of what you want to do with user input. Use fgets() and sscanf(), fgets() and strtok(), or write your own user input routines using getchar() and putchar().

1.5) Even properly used, scanf inevitably discards input (whitespace) that can sometimes be important.

2) scanf has a nasty habit of leaving newlines in the input stream. This is fine if you never use anything but scanf, since scanf will usually skip over any whitespace characters in its eagerness to find whatever it's expecting next. But if you mix scanf with fgets/getchar, it quickly becomes a total mess trying to figure out what might or might not be left hanging out in the input stream. Especially if you do any looping -- it's quite common for the input stream to be different on the first iteration, which results in a potentially weird bug and even weirder attempts to fix it.

tl;dr -- scanf is for formatted input. User input is not formatted. //

I forgotted to say that, I use scanf to get an integer.


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#9 2013-04-12 11:47:10

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,812
Website

Re: [C] getchar() not working?

at the simplest, fgets+sscanf should avoid this problem.  There are also good options with (n)curses, depending how much interaction you expect.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#10 2013-04-13 05:28:27

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

Re: [C] getchar() not working?

Anyway, i use getchar() 2 times, it works, this now solved. thanks.


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#11 2013-04-16 11:08:04

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 987

Re: [C] getchar() not working?

Okay, but when it breaks again because you decided to shotgun-debug instead of doing things right the first time, don't say I didn't warn you...

Offline

Board footer

Powered by FluxBB