avoid newline in C

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

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


My input is 2+2. So it prints "=4". It's fine.

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

There is some code I can use for this?

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

ty.
As far I know there is no way to avoid a newline character in scanf statement. However you can use an alternative way instead. use getche() It will take input but not wait for user to hit the enter key.

Code goes like this:

1
2
3
a=getche()-'0';//convert char to integer value,subtracting ascii value of 0 from it
op=getche();//read a char as an operator
b=getche()-'0';//convert char to integer value,subtracting ascii value of 0 from it 

Last edited on
One more thing I would like to include. if you are trying to write a calculator program the use switch statement instead of if-else.
Ty shysan21, but I cant use getche because it just read 1 character. If I want read the number 12 for example it wont work.

I was looking for a way of coming back 1 line, but didnt find any.

So I used "system("clear")" or "cls" and write all the values again on the screen.

If someone find another solution, please post it.

ty.
Topic archived. No new replies allowed.