0

Everything complies alright but I'm just wondering why its not reading the switch statement and enter the operator it.

EX: Enter first number 5 then it automatically reads the default statement enter second number 2 and the result is 0.

float addNum(float, float);
float subtractNum(float, float);
float multiplyNum(float, float);
float divideNum(float, float);

#include <stdio.h>
#include <ctype.h>

int main()
{
    char opNum;
    float result;
    float firstNum;
    float secNum;

    printf("Enter first number: ");
    scanf("%f", &firstNum);
    printf("Enter the operator:\n");
    scanf("%c", &opNum);
    switch (opNum)
    {
        case '+':
            result = addNum(firstNum, secNum);
            break;

        case '-':
            result = subtractNum(firstNum, secNum);
            break;

        case '*':
            result = multiplyNum(firstNum, secNum);
            break;

        case '/':
            result = divideNum(firstNum, secNum);
            break;

        default:
            printf("The operator you entered is invalid.");
    }
    printf("Enter second number: ");
    scanf("%f", &secNum);
    printf("Your total number is: %f", result);
    return 0;
}

float addNum(float firstNum, float secNum)
{
    float result = 0.0;
    result = firstNum + secNum;
    return result;
}

float subtractNum(float firstNum, float secNum)
{
    float result = 0.0;
    result = firstNum - secNum;
    return result;
}

float multiplyNum(float firstNum, float secNum)
{
    float result = 0.0;
    result = firstNum * secNum;
    return result;
}

float divideNum(float firstNum, float secNum)
{
    float result = 0.0;
    result = firstNum / secNum;
    return result;
}
Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • `scanf("%c", &opNum);` skip newline --> `scanf(" %c", &opNum);` – BLUEPIXY Feb 08 '17 at 04:14
  • `printf("Enter second number: "); scanf("%f", &secNum);` move to after `scanf(" %c", &opNum);` (Before the functions are called) – BLUEPIXY Feb 08 '17 at 04:15
  • So, you want to use the second number before it got entered? – deamentiaemundi Feb 08 '17 at 04:15
  • Adding some `printf()`s to show the various values would nip this in the bud before it ever got posted as a question. Does no one bother to even do that level of debugging anymore? – John3136 Feb 08 '17 at 04:18

0 Answers0