0

I have a course in c from the university and we are required to use the scanf_s function. I've started writing a code for my first assignment and the scanf_s doesn't seem to work. It seems the program just ignores that line where I wrote scanf_s("%c", &mathOparation, 1);

I would be happy if anyone can explain why.

My code:

#include <stdlib.h>
#include<math.h>

int main()
{
    int choise; // the selection the user chooses from the list below

    printf("please choose from the selection below the secrtion you want:\n ");
    printf("1.standard calculator\n");
    printf("2.Bitwise shoft calculator\n");
    printf("3.pascal's triangle\n");
    printf("0.to exit\n");
    printf("enter a number between 1-3\n");
    scanf_s("%d", &choise);
    switch (choise)
    {
    case 1://the basic calculator
    {
        double num1=0, num2=0;// the 2 numbers which we will use the oparation on
        char mathOparation;// the oparation the user needs to choose
        printf("what would you like to do '+' ,'-','*','/' \n");
        scanf_s("%c", &mathOparation, 1);  
        printf("please enter the 2 numbers you want to operate on:\n");
        scanf_s("%lf%lf", &num1, &num2);
        switch (mathOparation)
        {
        case '+':
            printf("%lf", num1 + num2);
            break;
        case '-':
            printf("%lf", num1 - num2);
            break;
        case '*':
            printf("%lf", num1 * num2);
            break;
        case '/':
            printf("%f", num1 / num2);
            break;
        default:
            printf("error");
            break;
        } 
    } 
    default:
        printf("error please try again");
        break;
    }

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
lights
  • 9
  • 2
  • It should print "error" based on your code – stark Mar 20 '22 at 14:15
  • It's very unlikely that the program ignores the `scanf_s` line, but the scan can fail or nor do what you want. Have you analyzed the problem? What is the return value of `scanf_s`? (Should be 1.) What is the value of `mathOperation`? (Instead of just printing "error", you could be more explicit, e.g. "Unknown operator '$'.". – M Oehm Mar 20 '22 at 14:18
  • 1
    Never describe a problem merely as “dosnt seem to work”. Always include instructions on how to reproduce the problem, including an exact copy of input that reproduces the problem, show the output or other observed behavior, and show an example of the desired output or behavior. – Eric Postpischil Mar 20 '22 at 14:18
  • Review [this question](https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer) and its answers to see if that is your problem. – Eric Postpischil Mar 20 '22 at 14:18
  • In this respect `scanf_s` behaves like `scanf`: most of the format specifiers for `scanf` automatically filter leading whitespace, but `%c` and `%[]` and `%n` do not. Adding a space in front of the `%` instructs `scanf` to filter leading whitespace here too. So here, you need `scanf_s(" %c", &mathOparation, 1);` with a space added. – Weather Vane Mar 20 '22 at 14:19
  • From the frequency such questions are asked, I am amazed that textbooks seem to omit the crucial information about how `scanf` and friends handle whitespace, both in the input stream and in the format string. – Weather Vane Mar 20 '22 at 14:22
  • Aside: you need to `#include ` too. – Weather Vane Mar 20 '22 at 14:24
  • thank you Weather Vane it works now. i still don't understand why the space helped though... – lights Mar 20 '22 at 15:34
  • It's *by definition* of how the function works. I attempted to explain that in the [comment](https://stackoverflow.com/questions/71547500/scanf-sc-mathoparation-1-doesnt-work#comment126453484_71547500), and the duplicate also discusses the matter. Whitespace in the format string matches whitespace in the input. – Weather Vane Mar 20 '22 at 17:09

0 Answers0