-6

I am trying to make a program that if the user gives a whole arithmetic operation from the keyboard ex 5*4 or 5/7 it will interpret the operation ex *, /, +, or - and print the result. How can I read the whole operation without the user pressing enter everytime he puts a number or a symbol ex * or / and put them in 3 variables? I tried using:

printf("give an operation")
scanf("%d%c%d",&num_1,&c,&num_2)

but I want to do with:

c=getchar()
Mike
  • 47,263
  • 29
  • 113
  • 177
user1809300
  • 703
  • 4
  • 9
  • 17

2 Answers2

2

If I understand your question and comments correct, you want to read a line of input from the user, where the user enters an expression and uses the Enter key to end the expression?

Then using scanf should work fine. You could also use fgets to get the line, and the use sscanf for the parsing.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
-1
scanf("%d %[*+-/] %d", &numA, op, &numB);

op should be defined as char array: char op[2];

Explanation

%[+-*/] : this means that you expect for %op the character + or - or * or /

MOHAMED
  • 41,599
  • 58
  • 163
  • 268