Answer the question
In order to leave comments, you need to log in
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);
Type what you want to calculate: 2+2
= 4
Type what you want to calculate: 2+2 = 4
Answer the question
In order to leave comments, you need to log in
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;
}
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.
Without a special console API of a particular OS - no way. Because the string is passed to the console when the user presses enter.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question