Thread: Logical error in Switch Statement..

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    Logical error in Switch Statement..

    Iam facing a problem in Switch statement. The problem is that i will ask to integer from the user & if the user want to "+" , "-" or "*" operator the switch case will work but in my coing it only ask the two interger values from the user but did not take any input from the user that he want "+" ,"-" or "*" with the operator.
    it simply skip it & go to the default case..
    The compliler is not showing any error in th ebelow program my out put is also given below...please guide me how where is the logical error...

    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    int num1,num2,res;
    char operator;
    clrscr();
    printf("\n\tPlease enter 1st integer : ");
    scanf("%d",&num1);
    printf("\n\tPlease enter 2nd integer : ");
    scanf("%d",&num2);
    printf("\n\tEnter what u want to Manupulate ");
    scanf("%c",&operator);

    switch(operator)
    {
    case '+':
    printf("\n\tThe result is %d,%c,%d=%d",num1,operator,num2,res);
    break;
    case '-':
    printf("\n\tThe result is %d,%c,%d=%d",num1,operator,num2,res);
    break;
    case '*':
    printf("\n\tThe result is %d,%c,%d=%d",num1,operator,num2,res);
    break;
    default:
    printf("\n\tNothing...");
    }

    getch();
    }

    *****************OUT PUT OF THE PROGRAM**************

    Please enter 1st integer : 5

    Please enter 2nd integer : 5

    Enter what u want to Manupulate
    Nothing...

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A few bits of reading for you:

    First this

    then this

    and this

    and also this <-- This one will fix your problem, but there are better ways of getting a number from the user.

    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    Re: Logical error in Switch Statement..

    Originally posted by gardenair
    Iam facing a problem in Switch statement. The problem is that i will ask to integer from the user & if the user want to "+" , "-" or "*" operator the switch case will work but in my coing it only ask the two interger values from the user but did not take any input from the user that he want "+" ,"-" or "*" with the operator.
    it simply skip it & go to the default case..
    The compliler is not showing any error in th ebelow program my out put is also given below...please guide me how where is the logical error...

    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    int num1,num2,res;
    char operator;
    clrscr();
    printf("\n\tPlease enter 1st integer : ");
    scanf("%d",&num1);
    printf("\n\tPlease enter 2nd integer : ");
    scanf("%d",&num2);
    printf("\n\tEnter what u want to Manupulate ");
    scanf("%c",&operator);

    switch(operator)
    {
    case '+':
    printf("\n\tThe result is %d,%c,%d=%d",num1,operator,num2,res);
    break;
    case '-':
    printf("\n\tThe result is %d,%c,%d=%d",num1,operator,num2,res);
    break;
    case '*':
    printf("\n\tThe result is %d,%c,%d=%d",num1,operator,num2,res);
    break;
    default:
    printf("\n\tNothing...");
    }

    getch();
    }

    *****************OUT PUT OF THE PROGRAM**************

    Please enter 1st integer : 5

    Please enter 2nd integer : 5

    Enter what u want to Manupulate
    Nothing...

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... and your post was meant to say...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    26
    Change
    scanf("%c",&operator);

    to
    scanf(" %c",&operator);

    I forgot the reason why you do that, but I remember a teacher telling me to do this in a class a few years back. I think its because it stores the press of return from the previous line, but that's just a guess..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  3. Error proofing my switch statement.
    By Shamino in forum C++ Programming
    Replies: 10
    Last Post: 01-04-2008, 04:38 PM
  4. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM