Jump to content

#include <stdio.h>

int main(){

   float iNum1, iNum2, Result;

   char Operation;

printf("\n Put the first number: ");

scanf("%f", &iNum1);

 

printf("\n what is the operator:");

scanf("%c", &Operation);

 

printf("\n Put the second number: ");

scanf("%f", &iNum2);

 

switch (Operation)

{

case '+':

Result=iNum1+iNum2;

break;

 

case'-':

if(iNum1>iNum2){

   Result=iNum1-iNum2;

}

else{

   Result=iNum2-iNum1;

}

break;

 

case'*':

Result=iNum2*iNum1;

break;

 

case '/':

if(iNum1>iNum2)

{

Result=iNum1/iNum2;

}

else{

   Result=iNum2/iNum1;

}

break;

 

default:

printf:("\n give a valid operator for the opration to proceed");

 

}

 

printf("\n the result is: %f", Result);

return 0;

}

 

after compiling this source code through the vs code terminal with the gcc command i proceeded to run the executable file and then i am able to put the values of both the operand numbers(iNum1 &iNum2) but it is not letting me to enter the character or the operator variable. the whole swtich statement is depending on the value of the operator variables content so as that is not having any value the result is coming out to be 0. so what to do.

 

Link to comment
https://linustechtips.com/topic/1402653-help-to-find-the-problem-in-the-code/
Share on other sites

Link to post
Share on other sites

It reads the return from entering the 2nd number as the character. The quick and dirty way to fix this would be to duplicate line 9, though there's definitely a better way to do this (I'm not great with pure C). 

 

Also USE CODE BLOCKS. It makes everything way easier to read. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×