avoid newline scanf

Discussion in 'C Programming' started by Rafael Oliveira, Mar 24, 2012.

  1. Hi, I'm trying to use scanf like this:

    scanf("%d%c%d", &a, &op, &b);
    if (op=='+')
    c=a+b;
    printf("=%d",c);

    My input is 2+2, and it prints "=4". Its fine.

    The problem is that I dont want a newline after scanf.

    There is a way of avoid this newline?

    There is a way of using "=" instead of the default end scanf char "enter"?

    ty.
     
    Rafael Oliveira, Mar 24, 2012
    #1
    1. Advertising

  2. Rafael Oliveira

    Gene Guest

    On Mar 23, 11:54 pm, Rafael Oliveira <> wrote:
    > Hi, I'm trying to use scanf like this:
    >
    > scanf("%d%c%d", &a, &op, &b);
    > if (op=='+')
    >  c=a+b;
    > printf("=%d",c);
    >
    > My input is 2+2, and it prints "=4". Its fine.
    >
    > The problem is that I dont want a newline after scanf.
    >
    > There is a way of avoid this newline?
    >
    > There is a way of using "=" instead of the default end scanf char "enter"?


    In a word, no. This isn't a C question. It's an OS question: How to
    implement unbuffered IO? In no system I'm aware of will scanf do what
    you want.
     
    Gene, Mar 24, 2012
    #2
    1. Advertising

  3. Rafael Oliveira

    Kaz Kylheku Guest

    On 2012-03-24, Rafael Oliveira <> wrote:
    > Hi, I'm trying to use scanf like this:
    >
    > scanf("%d%c%d", &a, &op, &b);
    > if (op=='+')
    > c=a+b;
    > printf("=%d",c);
    >
    > My input is 2+2, and it prints "=4". Its fine.
    >
    > The problem is that I dont want a newline after scanf.


    That is caused by your interactive input being buffered in your
    operating system, and not released into the C program until
    you hit Enter.

    I.e. it's not scanf that is doing this.

    > There is a way of using "=" instead of the default end scanf char "enter"?


    On some platforms (anything that looks like Unix) you can reprogram
    the tty driver for character-at-a-time input.

    I don't know about Windows. There are ways to get characters from the console,
    but there is the question of whether that can be done through scanf.
    Shrug. In any case, it won't be the same code as for Unix.
    You're looking at different source files or #ifdefs for different
    platforms.

    (I would not even consider using scanf for any sort of interactive
    input, whether character-at-a-time or line by line.)
     
    Kaz Kylheku, Mar 24, 2012
    #3
  4. Rafael Oliveira

    Don Y Guest

    Hi Rafael,

    On 3/23/2012 8:54 PM, Rafael Oliveira wrote:
    > Hi, I'm trying to use scanf like this:
    >
    > scanf("%d%c%d",&a,&op,&b);
    > if (op=='+')
    > c=a+b;
    > printf("=%d",c);
    >
    > My input is 2+2, and it prints "=4". Its fine.
    >
    > The problem is that I dont want a newline after scanf.


    You are getting the newline *before* (during?) scanf()'s
    execution.

    > There is a way of avoid this newline?
    >
    > There is a way of using "=" instead of the default end scanf char "enter"?


    Depending on the environment in which you are operating
    (hosted (and *which*!) vs. standalone), you can probably
    arrange for "raw" (uncooked) I/O.

    Typically (hosted), input is buffered by the OS ("canonical/cooked
    mode") and delivered to the application line-at-a-time. Lines
    are (typically) terminated with newlines. Certain other control
    characters are applied to allow for some interactive processing
    *in* the terminal handler. E.g., when you make a mistake and
    type a backspace to delete that character, your application
    doesn't see the backspace *or* the "bad character". The terminal
    handler "cooks" the input before it is delivered to your application.

    (In UN*X), you can put the terminal in raw mode and process
    individual keystrokes *in* your application. But, note that
    you will have to process *all* keystrokes. Including any
    editing keystrokes (like backspace). And, you will have to
    decide when the user has "finished" his data entry (perhaps
    with a '=' in your case). You might also have to deal with
    the case where NO KEYSTROKE has occurred (i.e., you can
    have the handler return "nothing" to you after a timeout
    if the user stops typing)

    You can also (again, UN*X) alter the set of control characters
    that the terminal handler uses for this input processing.
    And leave it in cooked mode (so you don't have to deal
    with those individual keystrokes).

    You can do this by tweeking the VEOL and VEOL2 control
    characters (see termios(4)).

    Doing any of this *portably* is another issue, entirely! :>
     
    Don Y, Mar 24, 2012
    #4
  5. On Fri, 23 Mar 2012 20:54:49 -0700 (PDT), Rafael Oliveira
    <> wrote:

    >Hi, I'm trying to use scanf like this:
    >
    >scanf("%d%c%d", &a, &op, &b);
    >if (op=='+')
    > c=a+b;
    >printf("=%d",c);
    >
    >My input is 2+2, and it prints "=4". Its fine.
    >
    >The problem is that I dont want a newline after scanf.
    >
    >There is a way of avoid this newline?


    Probably not but you can suppress it easily enough.

    When scanf is processing characters for computing the value to store
    in b, it will stop at the first character which cannot be part of an
    int. With an obedient typist, this will be the \n produce by the
    ENTER key but it could be any other non-digit (alpha, imbedded +,
    etc). The remove all these extraneous characters from the input
    buffer simply code
    while (getchar() != '\n')
    ;
    after the call to scanf. This will remove one character at a time
    from the input buffer until the character just removed is the one
    produce by ENTER.

    >There is a way of using "=" instead of the default end scanf char "enter"?


    Probably not.

    --
    Remove del for email
     
    Barry Schwarz, Mar 24, 2012
    #5
  6. A char different than a int will stop scanf but scanf will only write in the memory after <enter>.

    So it will not avoid the newline.

    I solved this printing everything in the screen again, after a "clear" or "cls".

    So I could write the value of c in the same line of my previous input.

    But it was not good, because its lazy.

    Another solutions will be welcome.
     
    Rafael Oliveira, Mar 24, 2012
    #6
  7. Rafael Oliveira

    Don Y Guest

    Hi Barry,

    On 3/24/2012 10:20 AM, Barry Schwarz wrote:
    > On Fri, 23 Mar 2012 20:54:49 -0700 (PDT), Rafael Oliveira
    > <> wrote:
    >
    >> Hi, I'm trying to use scanf like this:
    >>
    >> scanf("%d%c%d",&a,&op,&b);
    >> if (op=='+')
    >> c=a+b;
    >> printf("=%d",c);
    >>
    >> My input is 2+2, and it prints "=4". Its fine.
    >>
    >> The problem is that I dont want a newline after scanf.
    >>
    >> There is a way of avoid this newline?

    >
    > Probably not but you can suppress it easily enough.


    No. The newline is coming *before* scanf(3) even *sees*
    the characters typed by the user. The terminal handler
    is buffering the input -- "cooking" it -- and only
    passing it on to the application -- scanf() -- once the
    user has typed ENTER.

    > When scanf is processing characters for computing the value to store
    > in b, it will stop at the first character which cannot be part of an
    > int. With an obedient typist, this will be the \n produce by the
    > ENTER key but it could be any other non-digit (alpha, imbedded +,
    > etc). The remove all these extraneous characters from the input
    > buffer simply code
    > while (getchar() != '\n')
    > ;
    > after the call to scanf. This will remove one character at a time
    > from the input buffer until the character just removed is the one
    > produce by ENTER.


    But the buffer isn't made available to the application until
    the user has typed ENTER! The terminal handler is receiving
    keystrokes, echoing them and accumulating them in a buffer
    *before* passing the entire buffer to scanf. I.e., the
    characters typed have already been echoed (including ENTER)
    before scanf() even sees the first character in the buffer.

    >> There is a way of using "=" instead of the default end scanf char "enter"?

    >
    > Probably not.


    The OP needs to look at the termios(4)-related features of his
    hosted OS. In UN*X, you can exercise a lot of control over what
    the terminal handler does to keystrokes prior to passing them
    to the application (scanf(), gets(), or whatever *other* input
    hook might be waiting).

    To take the terminal handler out of the picture completely,
    you can tell it to pass each keystroke directly to the
    application without echoing, without checking for "control
    characters" (not the same as the "control characters" you
    think of when you think of ASCII), without even *waiting*
    for a keystroke! I.e., "nothing has been typed since the
    last time you called me".

    You can also control things like whether CR and NL are equivalenced.
    If input keystrokes are automatically echoed. etc.

    But, then the application has to do all of that processing
    itself! If you want to allow the user to delete the last
    character that he typed, then *you* have to check for the
    receipt of a backspace (or DEL?) character *and*, when detected,
    backup one keystroke and delete the keystroke that preceded
    it -- and inform the user that you are doing this (by some
    mechanism)!

    You even have to enforce your own processing of localization
    criteria: is "three and a half" typed as "3.5"? Or, 3,5"?

    In this way, you strip keys of any particular meaning and
    just use the terminal handler to get the keystrokes *to*
    you. This is usually an unnecessary burden for the majority
    of applications.

    If, for example, you want the user to be able to interactively
    navigate between "fields" in a visual "form" -- perhaps using
    TAB to advance to the next field -- you need to operate the
    interface in raw mode. Otherwise, the user would have to type
    "TAB ENTER", "TAB ENTER", "UPARROW ENTER", "LEFTARROW ENTER", etc.
     
    Don Y, Mar 24, 2012
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Till Crueger

    Get rid of trailing newline for scanf

    Till Crueger, Jan 16, 2005, in forum: C Programming
    Replies:
    4
    Views:
    1,819
    Jonathan Burd
    Jan 17, 2005
  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,401
    those who know me have no need of my name
    Apr 3, 2006
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    681
    Richard Bos
    May 2, 2006
  4. Florian Lindner

    Avoid newline at the end

    Florian Lindner, Nov 11, 2007, in forum: Python
    Replies:
    4
    Views:
    314
    Florian Lindner
    Nov 11, 2007
  5. Replies:
    4
    Views:
    284
    Ben Finney
    Jan 23, 2008
Loading...

Share This Page