0

having to face some issue on this program to develop a calculator, i have tried to do the calculation for maths operation before the switch statement. The print result only prompt invalid entry on my default statement after entering the value and maths operation

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
       // declare and initialise working storage
       char function;
       int num1, num2, sum, sub, multi, div, remainder;
    
    
       // prompt user to enter 2 number
       printf("Enter numbers and function: ");
       scanf("%d%c%d", &num1, &function, &num2);

       // calculation
       sum = num1 + num2;
       sub = num1 - num2;
       multi = num1 * num2;
       div = num1 / num2;
       remainder = num1 % num2;
    
       // print the result
       switch(function)
       {
            case '+':
                printf("The sum is %d\n", sum);
                break;
            case '-':
                printf("The subtraction is %d\n", sub);
                break;
            case '*':
                printf("The multiplication is %d\n", multi);
                break;
            case '/':
                if(num2 ==0)
                {
                     printf("Cannot divide by 0!\n");
                }
                else
                {
                    printf("The div is %d\n", div);
                }
                break;
            case '%':
                 if(num2 ==0)
                {
                     printf("Cannot divide by 0!\n");
                }
                else
                {
                    printf("The remainder is %d\n", remainder);
                }
                break;
    
            default:
                printf("Invalid Entry!!!!!\n");
                break;
    
       }
    
    
    
       return 0;
}
Sheng Yao
  • 5
  • 5
  • Please give the exact input, expected result and actual result. Note that `%c` will match all characters including spaces. If you are entering something like "1 + 2" then `function` will be a space character. – kaylum Apr 15 '22 at 11:21
  • OT: It is a waste to do the calculations for all possibilities. Instead you should do just a single calculation in each of the `switch` cases. Also you need to check whether `num2` is 0 otherwise you will get an exception for that case. – kaylum Apr 15 '22 at 11:27
  • @kaylum: FYI, if you optimize with GCC, there is no waste; it moves the arithmetic operations to the individual switch cases. – Eric Postpischil Apr 15 '22 at 11:45
  • 2
    What does your input look like? `"%d%c%d"` will work for `5+5` and `5+ 5`, but not `5 +5` or `5 + 5`. See [here](https://stackoverflow.com/a/5240807/2505965). – Oka Apr 15 '22 at 11:53
  • So you need `"%d %c%d"` because `%c` (and `%[]`) does not automatically filter whitespace from the input- you need to *tell* it to. The `%d` (and `%f` and `%s`) does though. You might have picked up on this but you skimped the *essential* check on the user input: `if(scanf("%d %c%d", &num1, &function, &num2) != 3) { /* handle error */ }`. **Always** validate user input if you want to write robust software. – Weather Vane Apr 15 '22 at 12:01
  • The divide by zero check is too late. Your program would already have crashed before then. – Bill Morgan Apr 15 '22 at 12:23
  • @Oka my initial input on the program was by enter first number: 5 enter function: + enter 2nd number : 5 and the result print is Invalid Entry! – Sheng Yao Apr 17 '22 at 05:55
  • @WeatherVane thanks for the advice! will take a look and try it out! – Sheng Yao Apr 17 '22 at 05:58
  • @kaylum alright so you mean can put the calculation on the print statement like `printf("%d %c %d = %d", num1, function, num2, num1 + num2);` ? – Sheng Yao Apr 17 '22 at 06:15
  • @EricPostpischil thanks for this important advice! btw what is GCC? – Sheng Yao Apr 17 '22 at 06:37
  • @ShengYao: GCC is [the GNU Compiler Collection](https://gcc.gnu.org), although many people think of it as “the GNU compiler” rather than a collection, and [the GNU C library](https://www.gnu.org/software/libc/) is closely associated with it although they are separate products and other libraries can be used with GCC. On any system where a `gcc` command is installed, it is likely to be GCC, with the notable exception that on a Mac, it is likely to be Clang instead (given the alias `gcc` so that old scripts to compile sources would continue to work). – Eric Postpischil Apr 17 '22 at 13:41

1 Answers1

0

The %c reads a character. Spaces and newlines are characters too. %d waits only for an int input. That's why every time you want to provide different numbers you have to press return or space to have the program read the next int input. So, if you choose to give your inputs like this:

1 + 3

the programm understands:

&num1 = 1 , &num2 = 3, &function = (space)

If you choose to enter your characters like this:

1+3(return)

it will run proper. But that's not a friendly calculator.

What you can do is:

printf("Enter numbers : ");
scanf("%d%d", &num1, &num2);// give 5(space)2 or 5(return)2 
scanf(%c,&enter); // reads the newline
printf("Enter function: ");
scanf("%c",&function);
Ria.Np
  • 16
  • 4