-1

I'm a beginner at C programming and I just finished making a calculator using if-else statements in my code. And now I tried of doing the same using switch-statement, but it is always executing the default. Please look into my code and suggest me what is going wrong. I am currently writing the code in CodeBlock.

This is the message i'm getting

int main()
{
    printf("\nWhat operation do you want to do:\n\tA)Addition\n\tB)Subtraction\n\tC)Multiplication\n\tD)Division\n");
    float num1;
    printf("Please enter the first number: ");
    scanf("%f", &num1);
    float num2;
    printf("Please enter the second number: ");
    scanf("%f", &num2);
    char myChar;
    scanf("%c", &myChar);
    switch (myChar)
    {
        case 'A':
            printf("The addition of %.2f and %.2f is %.2f", num1, num2, num1 + num2);
            break;
        case 'B':
            printf("The subtraction of %.2f and %.2f is %.2f", num1, num2, num1 - num2);
            break;
        case 'C':
            printf("The multiplication of %.2f and %.2f is %.2f", num1, num2, num1 * num2);
            break;
        case 'D':
            printf("The quotient of %.2f and %.2f is %.2f", num1, num2, num1 / num2);
            break;
        default :
            printf("You enterned incorrect input");
            break;
    }
    return 0;
}

Any help will be appreciated

  • `char myChar; scanf("%c", &myChar);` move to before `float num1;` – BLUEPIXY Jan 22 '17 at 05:39
  • When you `scanf("%c", &myChar)` you're reading the newline left after reading your second number. Your `switch()` doesn't have a case for that, so it runs the default. – Dmitri Jan 22 '17 at 05:42
  • If you want to read a line, use code that reads a line, not code that reads a character or a number. For your code to work as shown, you'd have to enter "32 43+" since your code reads two numbers and a character. – David Schwartz Jan 22 '17 at 05:43
  • You caused the error by pushing `` after the second number even though you have no code to read that. Instead, enter `4+` for the second number. That way, when you code reads a number and then a character, it will get `4` then `+` instead of `4` then ``. – David Schwartz Jan 22 '17 at 05:46
  • 1
    Don't use `scanf` for user input; use `fgets`. – melpomene Jan 22 '17 at 05:46
  • 1
    *it fails to execute properly* is an absolutely useless error description unless you explain what *fails to execute properly* means. What **specific problem** are you having with the code you posted? – Ken White Jan 22 '17 at 05:46
  • 1
    Possible duplicate of [C scanf() issues?](http://stackoverflow.com/questions/32236684/c-scanf-issues) – melpomene Jan 22 '17 at 05:51

1 Answers1

0

Your problem is mostly related to scanf that the remaining \n from the previous input was interpreted as the input for the one.

Also your prompt for operation does not match with its corresponding input.

Suggest a fix:

float num1;
printf("Please enter the first number: ");
scanf("%f", &num1);

float num2;
printf("Please enter the second number: ");
scanf("%f", &num2);

printf("\nWhat operation do you want to do:\n\tA)Addition\n\tB)Subtraction\n\tC)Multiplication\n\tD)Division\n");
char myChar;
scanf(" %c", &myChar);

You can add printf as debugging purpose - that would help, too. Example for 1st input:

float num1;
printf("Please enter the first number: ");
scanf(" %f", &num1);
printf("num1 = %f\n", num1 );
artm
  • 17,291
  • 6
  • 38
  • 54