Thread: Need explanation with switch case program!

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    2

    Question Need explanation with switch case program!

    A program to create a simple calculator using switch case:

    Code:
    #include <stdio.h>
    #include <conio.h>
    main()
    {
    clrscr();
    char o;
    int a,b;
    printf("Enter two operators: ");
    scanf("%d %d",&a,&b);
    printf("Enter the operand(+,-,*,/): ");
    scanf("%c",&o);
    switch(o)
    {
    case '+':
    printf("The addition of %d + %d = %d",a,b,a+b);
    break;
    case '-':
    printf("The subtracation of %d - %d = %d",a,b,a-b);
    break;
    case '-':
    printf("The multiplication of %d * %d = %d",a,b,a*b);
    break;
    case '-':
    printf("The division of %d / %d = %d",a,b,a/b);
    break;
    default:
    printf("Please enter a correct operator");
    break;
    }
    getch();
    }
    In this way the output is somewhat like this:
    Code:
    Enter two operators : 8 3
    Select an operator(*,-,*,/) Please enter a correct operator.
    While in the program if i ask for to enter the operand first, then the output is working properly!

    Code:
    #include <stdio.h>
    #include <conio.h>
    main()
    {
    clrscr();
    char o;
    int a,b;
    printf("Enter the operand(+,-,*,/): ");
    scanf("%c",&o);
    printf("Enter two operators: ");
    scanf("%d %d",&a,&b);
    
    switch(o)
    {
    case '+':
    printf("The addition of %d + %d = %d",a,b,a+b);
    break;
    case '-':
    printf("The subtracation of %d - %d = %d",a,b,a-b);
    break;
    case '-':
    printf("The multiplication of %d * %d = %d",a,b,a*b);
    break;
    case '-':
    printf("The division of %d / %d = %d",a,b,a/b);
    break;
    default:
    printf("Please enter a correct operator");
    break;
    }
    getch();
    }
    Output:
    Code:
    Select an operator(+,-,*,/) +
    Enter two operands 4
    5
    The addition of 4 + 5 = 9
    Just confused as logically according to my believe i think both are same right? i can ask to enter the operators first followed by the operand or the opposite! But in the first program the output is messed up as it is directly jumping outside showing that an invalid operand has been used.! :/
    Last edited by lovecoder; 08-07-2017 at 01:42 AM. Reason: Spelling mistake x

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    %c is fairly unique in that it supresses the usual behaviour of skipping leading white space (spaces, tabs and newlines).

    To counter this, put a space in your conversion string.
    Code:
    scanf(" %c",&o);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    2
    Quote Originally Posted by Salem View Post
    %c is fairly unique in that it supresses the usual behaviour of skipping leading white space (spaces, tabs and newlines).

    To counter this, put a space in your conversion string.
    Code:
    scanf(" %c",&o);
    WOW! It worked!!!! Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-19-2016, 08:24 PM
  2. Replies: 0
    Last Post: 03-09-2016, 03:58 PM
  3. Switch-case program
    By saros3578 in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2015, 09:17 AM
  4. Help with functions & switch case in my converting program
    By tlxxxsracer in forum C Programming
    Replies: 3
    Last Post: 04-20-2015, 08:16 AM
  5. Replies: 11
    Last Post: 08-25-2008, 12:01 PM

Tags for this Thread