avoid newline scanf

R

Rafael Oliveira

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.
 
G

Gene

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.
 
K

Kaz Kylheku

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.)
 
D

Don Y

Hi Rafael,

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! :>
 
B

Barry Schwarz

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.
 
R

Rafael Oliveira

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.
 
D

Don Y

Hi Barry,

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.
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top