-1

I'm having an issue with this piece of code. When I enter the input(Ex. 150.79 - 789.4) the program does not do anything. It does not even stop running, it continues running. Does anybody know what I'm doing wrong?

    #include <stdio.h>
    int main(void)
    {
    float value1, value2;
    char operator, ch;
    printf("Type in your expression.\n");

    scanf_s("%f %c %f", &value1, &operator, &value2);

When I try to receive input this way, it works.

    //This version works...
    /*
    scanf_s("%f", &value1);
    ch = getchar();
    scanf_s("%c", &operator);
    scanf_s("%f", &value2);
    */

    switch (operator)
    {
    case '+':
    printf ("%.2f\n", value1 + value2);
    break;
    case '-':
        printf("%.2f\n", value1 - value2);
        break;
    case '*':
        printf("%.2f\n", value1 * value2);
        break;
    case '/':
        if (value2 == 0)
            printf("Division by zero.\n");
        else
            printf("%.2f\n", value1 / value2);
        break;
    default:
        printf("Unknown operator.\n");
        break;
    }

    system("pause");
    return 0;
    }
Frantz Paul
  • 127
  • 2
  • 11
  • 1
    You have two incomplete code fragments. What exactly ***does*** work, and what exactly ***doesn't***? In either case, you should know that [`scanf()` leaves input in the input buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer) – torstenvl Dec 12 '18 at 04:30
  • The commented out code where the user inputs separately works. The one line that uses scanf_s does not work. When I run scanf in Visual Studio, I get an error. That is why I used scanf_s. – Frantz Paul Dec 12 '18 at 05:05
  • What's the error you're getting with `scanf()`? Incidentally, my system compiler is not fully C11 compliant yet and lacks `scanf_s()` – torstenvl Dec 12 '18 at 05:12
  • 1
    I hate to ask a stupid question, but you are pressing Enter/Return after you type your input line, right? – torstenvl Dec 12 '18 at 05:13
  • The error I was getting was " Severity Code Description Project File Line Suppression State Error C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details." However, I have sense resolved the issue by placing "#pragma warning(disable:4996)" near the top of my code. – Frantz Paul Dec 13 '18 at 05:05

1 Answers1

0

I have found a solution to the original problem that I posted. Originally, I was not able to use scanf because I kept on receiving this error from VS(Visual Studio), " Severity Code Description Project File Line Suppression State Error C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details."

I did some more researching online and found out that I could bypass this error from VS by pasting the line "#pragma warning(disable:4996)" near the top of my code.

The code works fine now with scanf.

Frantz Paul
  • 127
  • 2
  • 11