Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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");
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading