Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why my code is not working plz help !!!!

+5 votes
asked Apr 11, 2021 by ismail andrey (470 points)
// ***** CALCULATOR *****

#include <stdio.h>

int main()
{
    int num1,num2;
    char ch,a,b,c,d;
    printf("Enter the 1st number : ");
    scanf("%d",&num1);
    printf("Enter the 2nd number : ");
    scanf("%d",&num2);

    printf("1. Enter 'a' for addition\n");
    printf("2. Enter 'b' for minus\n");
    printf("3. Enter 'c' for multipoltion\n");
    printf("4. Enter 'd' for divition\n");
    scanf("%c",&ch);

    a = '+';
    b = '-';
    c = '*';
    d = '/';

    if(ch == 'a') {
        printf("Ans is %d\n",num1 + num2);
    }

    else if(ch == 'b') {
        printf("Ans is %d\n",num1 - num2);
    }

    else if(ch == 'e') {
        printf("Ans is %d\n",num1 * num2);
    }

    else if(ch == 'd') {
        printf("Ans is %d\n",num1 / num2);
    }

    else {
        printf("Incorrect input\n");
    }

    return 0;
}

13 Answers

+1 vote
answered Apr 11, 2021 by U S (230 points)

As you have used to scanf functions for num1 and num2, the third scanf function is not read because of '\n' left by the previous scanf functions. 

To make your code work just use scanf(" %c",&ch); instead of this scanf("%c",&ch);

The space before %c let scanf() ignore the '\n' left by previous scanf()s.

commented May 31, 2021 by bikash123-c (270 points)
#include<stdio.h>
#include<conio.h>
int main()
{
    int a,b,c,d,e,f,now;
    printf(" Enter your 1st number :");
    scanf(" %d",&a);
    printf(" Enter your 2nd number  :");
    scanf(" %d",&b);
      printf(" Enter your case ");
      scanf(" %d",&now);
   c=a*b;
   d=a+b;
   e=a-b;
   f=a/b;
   
   switch(now)
   
   {
       case 1:
       printf(" multiplication =%d",c);
       break;
       case 2:
       printf("addition =%d",d);
       break;
       case 3:
       printf(" subtraction =%d",e);
       break;
       case 4:
       printf(" division =%d",f);
       break;
       default:
       printf(" Error ");
   }
    
    
    
    return 0;
}
+3 votes
answered Apr 12, 2021 by Peter Minarik (63,340 points)
edited May 20, 2021 by Peter Minarik

This is a bit tricky.

When reading characters, scanf could be a bit weird. You want to get rid of any white-space character to read the correct input, so you should add a space just before the formatting to tell scanf to ignore them:

scanf(" %c",&ch);

Now your code works correctly. (Assuming you are interested in integer division only and no real numbers.)

commented May 31, 2021 by bikash123-c (270 points)
else if(ch == 'e') {
        printf("Ans is %d\n",num1 * num2);
    }
 here is your mistake,there should be c instead of e
0 votes
answered Apr 16, 2021 by Dillan Pereira (140 points)
/* To Solve simple computational Problems*/
#include<stdio.h>
int main()
{
    int op1,op2,result;
    int ch;
    printf("Enter the Operand Value\n");
    scanf("%d%d",&op1,&op2);
    printf("Enter your choice\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n 5. Modulus\n\n");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:
        result=op1+op2;
        printf("Result=%d",result);
        break;
        
        case 2:
        result=op1-op2;
        printf("Result=%d",result);
        break;
        
        case 3:
        result=op1*op2;
        printf("Result=%d",result);
        break;
        
        case 4:
        if(op2!=0)
        {
            result=op1/op2;
            printf("Result is %d\n",result);
            break;
        }
        else
        {
            printf("Divide by Zero error");
        }
        break;
        
        case 5:
        result=op1%op2;
        printf("Result is %d\n",result);
        break;
        
        default:
        printf("Invalid Choice\n");
        break;
    }
}
commented Apr 19, 2021 by Peter Minarik (63,340 points)
NO! THIS IS A TERRIBLE IDEA!

scanf("%d",&ch); forces an int (4 or 8 bytes depending on 32/64 bit architecture) into a 1 byte char. You are going to overwrite the memory followed by `ch`.

Consider the following example with execution:

#include <stdio.h>

int main()
{
    char ch1 = 0;
    char ch2 = 0;
    char ch3 = 0;
    char ch4 = 0;
    
    printf("Enter a number: ");
    scanf("%d", &ch1);
    printf("[%hhu][%hhu][%hhu][%hhu]\n", ch1, ch2, ch3, ch4);
    return 0;
}

Output:

main.c:19:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘char *’ [-Wformat=]
Enter a number: 12345678
[78][97][188][0]

As you can see, the following variables got all polluted by forcing a larger variable into ch1 than it can handle. Even the compiler warns about the type mismatch.
0 votes
answered May 4, 2021 by VIBHAV PANDEY (140 points)

firstly you should accept character for opeartor, and then for scanf to read it you can use the following:

scanf(" %c",&operator);

Here is the full code I have written and is working well:

#include <stdio.h>

int main()
{
    int opd1,opd2,result;
    char op1;
    
    printf("Enter the operands\n");
    scanf(" %d%d",&opd1,&opd2);
    
    printf("Enter the operator\n");
    scanf(" %c",&op1);
    
    switch(op1)
 {   
    case '+':
    result = opd1+opd2;
    break;
    
    case '-':
    result=opd1-opd2;
    break;
    
    case '*':
    result=opd1*opd2;
    break;
    
    case'/':
    result=opd1/opd2;
    break;
    
    default:
    printf("Invalid operation\n");
    
 }
 printf("The result is %d ",result);

    

    return 0;
}

0 votes
answered May 11, 2021 by rohan ag (1,270 points)
#include <stdio.h>

int main()
{
    int n1,n2;
    char ch;
    printf("enter two numbers");
    scanf("%d %d",&n1,&n2);
    printf("enter + for add - for sub * multiply and / for division");
    scanf(" %c",&ch);
    
    switch(ch)
    {
        case '+' : printf("addition = %d",n1+n2);
                   break;
        case '-' : printf("substraction = %d",n1-n2);
                   break;
        case '*' : printf("mul = %d",n1*n2);
                   break;
        case '/' : printf("division = %d",n1/n2);
                   break;
    }

    return 0;
}
0 votes
answered May 19, 2021 by 5 G-16 Jain Bharggav Mukul (350 points)
#include <stdio.h>
int main() {
    char operator;
    double first, second;
    printf("Enter an operator (+, -, *,/,): ");
    scanf("%c", &operator);
    printf("Enter two operands: ");
    scanf("%lf %lf", &first, &second);

    switch (operator) {
    case '+':
        printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
        break;
    case '-':
        printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
        break;
    case '*':
        printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
        break;
    case '/':
        printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
        break;
        // operator doesn't match any case constant
    default:
        printf("Error! operator is not correct");
    }

    return 0;
}
0 votes
answered May 19, 2021 by Tejash Chaudhary (140 points)
Use switch case and then you can see how much easy to solve this
0 votes
answered May 24, 2021 by Rohan (180 points)

Your Question Can Be Answered In Two Solutions:

1st : Use  scanf(" %c",&ch); instead of  scanf("%c",&ch);

2nd: If You Are Getting Problem in Multiplication Section It's Because You Had Put   else if(ch == 'e')   While You Have To Put   else if(ch == 'c')

0 votes
answered May 24, 2021 by Rohan (180 points)
You  Can Try This Code Also :

// ***** CALCULATOR *****

#include <stdio.h>

int main()
{
    int num1,num2,a,b,c,d;
    char ch;
    printf("Enter the 1st number : ");
    scanf("%d",&num1);
    printf("Enter the 2nd number : ");
    scanf("%d",&num2);

    printf("1. Enter 'a' for addition\n");
 
    printf("2. Enter 'b' for minus\n");
    scanf("%c",&ch);
    printf("3. Enter 'c' for multipoltion\n");
  
    printf("4. Enter 'd' for divition\n");
    scanf("%c",&ch);

    a = '+';
    b = '-';
    c = '*';
    d = '/';

    if(ch == 'a') {
        printf("Ans is %d\n",num1 + num2);
    }

    else if(ch == 'b') {
        printf("Ans is %d\n",num1 - num2);
    }

    else if(ch == 'e') {
        printf("Ans is %d\n",num1 * num2);
    }

    else if(ch == 'd') {
        printf("Ans is %d\n",num1 / num2);
    }

    else {
        printf("Incorrect input\n");
    }

    return 0;
}
0 votes
answered May 25, 2021 by 37_Yash Kumar Dhawan (140 points)
The scanf thing must be clear to you by now. However, there's another fault. Instead of comparing (ch=='a') you should rather consider (ch==a), as in your case {a} is a variable but when you compare like (ch=='a'), then {a} is treated as a character itself.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...