0
#include<stdio.h>

int main() {
    char operation='f';//Its working fine with int data type
    float n1, n2;
    printf("Enter two operands: \n");
    scanf("%f %f",&n1, &n2);
    
    do{
    printf("Enter an operator (+, -, *, /, f: for exit): \n");
    scanf("%c", &operation);
    
    
    switch(operation)
    {
        case '+':  //Replace '+' by 1 when operation is int data type
            printf("%.1f + %.1f = %.1f\n",n1, n2, n1+n2);
            break;

        case '-':  //Replace '-' by 2 when operation is int data type
            printf("%.1f - %.1f = %.1f\n",n1, n2, n1-n2);
            break;

        case '*':  //Replace '*' by 3 when operation is int data type
            printf("%.1f * %.1f = %.1f\n",n1, n2, n1*n2);
            break;

        case '/':  //Replace '/' by 4 when operation is int data type
            printf("%.1f / %.1f = %.1f\n",n1, n2, n1/n2);
            break;
    case 'f':  //Replace 'f' by 0 when operation is int data type
    break;
        // operator doesn't match any case constant +, -, *, /
        default:
            printf("Error! operator is not correct\n");
    }
    }while(operation!='f');

    return 0;
}

***CONSOLE USING char type**
*
──(kali㉿kali)-[~/Desktop/program]
└─$ gcc test01.c
                                                                             
┌──(kali㉿kali)-[~/Desktop/program]
└─$ ./a.out     
Enter two operands: 
12
5
Enter an operator (+, -, *, /, f for exit): 
Error! operator is not correct
Enter an operator (+, -, *, /, f for exit): 
+
12.0 + 5.0 = 17.0
Enter an operator (+, -, *, /, f for exit): 
Error! operator is not correct
Enter an operator (+, -, *, /, f for exit): 
h
Error! operator is not correct
Enter an operator (+, -, *, /, f for exit): 
Error! operator is not correct
Enter an operator (+, -, *, /, f for exit): 
f
                                                                             
┌──(kali㉿kali)-[~/Desktop/program]
└─$ 

***CONSOLE USING int data type for variable operation***
                                                                            
┌──(kali㉿kali)-[~/Desktop/program]
└─$ gcc test01.c
                                                                             
┌──(kali㉿kali)-[~/Desktop/program]
└─$ ./a.out     
Enter two operands: 
12
4
Enter an operator (1, 2, 3, 4, 0 for exit): 
1
12.0 + 4.0 = 16.0
Enter an operator (1, 2, 3, 4, 0 for exit): 
0
                                                                             
┌──(kali㉿kali)-[~/Desktop/program]
└─$ 

After "Enter an operator (+, -, *, /, f for exit):" it should take the input value using "scanf()", instead it is going to the "default case" and once again it is showing this "Enter an operator (+, -, *, /, f for exit):" and asking for input. But this is not the case when I am using any "char data type" rather it is working when I am using "int data type". Please explain.

I am using it in LINUX base OS, Kali.

ismail
  • 1

0 Answers0