c

Its not giving me the desired output


#include <stdio.h>

void main ()
{
    printf ("PRN-2301106083");
    int a, b, r;
    char s;
    printf ("\n\nEnter the number 'A':");
    scanf ("%d", &a);
    printf ("\nEnter the number 'B'");
    scanf ("%d",&b);
    printf ("\nEnter the operator '+,-,*,/,%':");
    scanf ("%c", &s);
    
    switch (s)
    {
        case '+':
        printf ("\nYou have entered addition!");
        r=a+b;
        printf ("\nThe output is: %d", r);
        break;
        
        case '-':
        printf ("\nYou have entered substraction!");
        r=a-b;
        printf ("\nThe output is: %d", r);
        break;
        
        case '*':
        printf ("\nYou have entered multiplication!");
        r=a*b;
        printf ("\nThe output is: %d", r);
        break;
        
        case '/':
        printf ("\nYou have entered division!");
        r=a/b;
        printf ("\nThe output is: %d", r);
        break;
        
        case '%':
        printf ("\nYou have entered modulus!");
        r=a%b;
        printf ("\nThe output is: %d", r);
        break;
        
        default:
        printf ("\nInvalid Choice")
    }
}

It will straight away print the last printf's Invalid Choice even when I have not enter the operator from the previous commmand.If anyone can pls send the corrected code or point out what's wrong then pls do.


Solution

  • The %c conversion specifier won't skip any leading whitespace automatically. One solution to this problem is to put a space before the conversion specifier in the format string, which "tells" scanf to skip leading whitespace:

    scanf(" %c", &s);
    

    Finally, with this fix, your code should look like this:

    #include <stdio.h>
    
    void main ()
    {
        printf ("PRN-2301106083");
        int a, b, r;
        char s;
        printf ("\n\nEnter the number 'A':");
        scanf ("%d", &a);
        printf ("\nEnter the number 'B'");
        scanf ("%d",&b);
        printf ("\nEnter the operator '+,-,*,/,%':");
        scanf (" %c", &s);
        
        switch (s)
        {
            case '+':
            printf ("\nYou have entered addition!");
            r=a+b;
            printf ("\nThe output is: %d", r);
            break;
            
            case '-':
            printf ("\nYou have entered substraction!");
            r=a-b;
            printf ("\nThe output is: %d", r);
            break;
            
            case '*':
            printf ("\nYou have entered multiplication!");
            r=a*b;
            printf ("\nThe output is: %d", r);
            break;
            
            case '/':
            printf ("\nYou have entered division!");
            r=a/b;
            printf ("\nThe output is: %d", r);
            break;
            
            case '%':
            printf ("\nYou have entered modulus!");
            r=a%b;
            printf ("\nThe output is: %d", r);
            break;
            
            default:
            printf ("\nInvalid Choice");
        }
    }