N
N
Neo_hacker2018-10-04 13:51:17
C++ / C#
Neo_hacker, 2018-10-04 13:51:17

How not to wrap to the next line after scanf?

I made a simple calculator, but the point is that after receiving the line it is transferred to the next one in order to print the answer. Here, for example, only for the sum (I will not post all the code, it will be a flood):

printf("Type what you want to calculate: "); 
scanf("%d%c%d", &first, operation, &second);
if (*operation == '+')
        printf("= %d\n", first+second);

After executing scanf, it does not continue the line, but goes to the next

Type what you want to calculate: 2+2
= 4

Is there a way for it to continue the line?
Thus:

Type what you want to calculate: 2+2 = 4

Thanks

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Roman, 2018-10-04
@Neo_hacker

windows

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

int main()
{
  char buf[80];
  int i = 0;
  int a;
  char op;
  int b;
  while((buf[i] = _getch(stdin)) != 13)
  {
    putchar(buf[i]); ++i;
  }
  sscanf(&buf, "%i%c%i", &a, &op, &b);
  if(op == '*')
    printf(" = %i", a * b);
  return 0;
}

R
res2001, 2018-10-04
@res2001

If you don’t dig deep into the “API of a particular OS”, then you can read character by character, not by scanf, but by getchar, but then you will have to manually exercise all input control, convert numbers, etc. etc.
But this can be done within the standard library.

M
Mercury13, 2018-10-04
@Mercury13

Without a special console API of a particular OS - no way. Because the string is passed to the console when the user presses enter.

N
ns5d, 2018-10-04
@ns5d

https://stackoverflow.com/questions/1798511/how-to...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question