0

Here is my code:

#include <stdio.h>

int main() {
    char op;
    int num1, num2;
    printf("Enter the operation(+,-,/,*,!,^) :  ");
    scanf(" %c", &op);
    switch (op) {
      case '+':
        printf("Enter the first operand : ");
        scanf(" %d", num1);
        printf("Enter the second operand : ");
        scanf(" %d", num2);  
        printf("%d", num1 + num2);
        break;
    }
}

when I run this. code stops after me entering first operand. I added space before scanf but it didn't solve the problem below.

output of code

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    Don't post pictures of text. Post text as text. Output is text. – Jabberwocky Mar 28 '23 at 14:43
  • You'll need to discard the trailing line feed character from stdin. But that's not the only problem, you have some basic syntax errors `scanf(" %d",num1);` which the compiler should already have told you about: [What compiler options are recommended for beginners learning C?](https://software.codidact.com/posts/282565) – Lundin Mar 28 '23 at 14:44
  • @Lundin are you sure the dupe is correct? – Jabberwocky Mar 28 '23 at 14:45
  • Somewhat unrelated: your approach is somewhat wrong. Write the code for the `-` operator and you'll understand what I mean. – Jabberwocky Mar 28 '23 at 14:47
  • @Jabberwocky There's several problems here. Though I don't know a dupe for "basic scanf syntax errors made by every beginner". Maybe this one is better: https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq – Lundin Mar 28 '23 at 14:48
  • @Lundin I'mean with the space in front of the scanf conversion specifiers (which the OP has put in their code) it should work provided the `&` is added. The real problem is the missing `&` which the dupe does not address. – Jabberwocky Mar 28 '23 at 14:52
  • 2
    Note that you can take input before the switch statement. That way, you won't have to write the same code for every operator. – Harith Mar 28 '23 at 15:03
  • You only need the space before the conversion specifications which don't automatically skip white space — and there are only three of those: `%c`, `%[…]` (scan sets) and `%n`. All the other conversion specifications skip white space automatically, whether you want them to or not. And white space includes newlines, of course. The `scanf()` function basically doesn't care about newlines — essentially, it can't be used reliably where you need line-based input. Use `fgets()` or POSIX `getline()` to read a line, and then use `sscanf()` or other functions to parse the line that was read. – Jonathan Leffler Mar 28 '23 at 22:06

2 Answers2

2

You forgot to add the address operator (&) in front of the num1 and num2 variables in the calls to scanf.

Modify those two lines like this:

scanf(" %d", &num1);
scanf(" %d", &num2);
//           ^add the &
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
-1

I usually add an & after the comma of every scanf I mean really when your advancing and that mistake is not corrected it can be annoying:

scanf("%d", &var);
user16217248
  • 3,119
  • 19
  • 19
  • 37
mr. code
  • 67
  • 5
  • *"I usually add an & after the comma of every scanf"* Do you understand what function `&` even has? – gre_gor Apr 29 '23 at 18:42