y it is giving eroor :(:(

anny**'s Avatar, Join Date: Oct 2008
Light Poster
see buddies i make a program in c but it take enter 1st number then ask for opearator and then directly tell that wrong opearator iam tired but i cant find where is problem
here it is :::::
PHP Code:

#include <conio.h>

      #include <math.h>

      #include <stdio.h>

    #define PI 3.14159265


      
int main()

      {



       
float firstNumbersecondNumber;

      
float resultSqrtresultSinresultCosresultTan;

      
char operation ,boolSqrt;




printf("TITLE My Calculator Program.");





printf(" *****************************************\n" );

      
printf("  Simple Calculator!\n" );

      
printf (" *****************************************\n\n");


      
printf" OPERATORS : +, -, *, /, !, S, C, T\n");

       
printf" \n");




      
printf (" + = ADD\n - = SUBTRACT\n * = Multiplication\n ");

      
printf ("/ = Division\n ! = SQAURE ROOT\n S = SIN\n ");

      
printf ("C = COS\n T = TAN\n");



      
printf(" \n ************************************************\n");

      
printf(" NOTE : YOU CAN USE X AND x FOR MULTIPLICATION!\n");

      
printf(" ************************************************\n\n");



      
printf"\n ENTER FIRST NUMBER : ");

      
scanf("%f",& firstNumber);



      
printf("\n ENTER OPERATOR SIGN : ");

      
scanf("%ch",& operation);



 
resultSqrt sqrt (firstNumber);



resultSin sin (firstNumber*PI/180);



      
resultCos cos (firstNumber*PI/180);



      
resultTan tan (firstNumber*PI/180);






  switch(
operation)

      {

      case 
'+':

      
printf("\n ENTER SECOND NUMBER : ");

      
scanf("%f",& secondNumber);

      
printf("\n Answer = "firstNumber secondNumber);

      break;



      case 
'-':

      
printf("\n ENTER SECOND NUMBER : ");

      
scanf("%f",& secondNumber);
       
printf("\n Answer = ",firstNumber secondNumber);

      break;



      case 
'*':

  case 
'x':

      case 
'X':

      
printf("\n ENTER SECOND NUMBER : ");

      
scanf("%f", & secondNumber);

      
printf("\n Answer = ",firstNumber secondNumber);

      break;



      case 
'/':

      
printf("\n ENTER SECOND NUMBER : ");

      
scanf("%f", & secondNumber);

      
printf("\n Answer = ",firstNumber secondNumber);

      break;


      case 
'!':

      
printf("\n ANSWER = " resultSqrt);

      break;



      case 
'S':

      case 
's':

      
printf("\n ANSWER = " resultSin);

      break;



      case 
'C':

      case 
'c':

      
printf("\n ANSWER = " ,resultCos);

      break;



      case 
'T':

      case 
't':

      
printf("\n ANSWER = " ,resultTan);

      break;


      default:

      
printf("\n WRONG MATHMATICAL OPERATOR!");

      break;



      }

      
getch();

      } 
0
oogabooga's Avatar
Ambitious contributor
You need to change this:
scanf("%ch",& operation);
to this:
scanf(" %c", &operation);
I'm not sure what the 'h' was doing in there, but you definitely need a space before the %c
to eat up the newline after entering the first number.

Also, to print the answer you need to change this (and all similar lines):
printf("\n Answer = ", firstNumber + secondNumber);
to something like this
printf("\n Answer = %f", firstNumber + secondNumber);
(You forgot to put in the format specifier.)

Moreover, you really need to fix the spacing of your code!
0
ahamed101's Avatar, Join Date: Sep 2008
Go4Expert Member
Anny, Please correct the mistakes pointed out in the previous post by oogabooga. Along with that you need to flush the input stream before taking the next input...

Code: c
char ch;
while((ch = getchar()) != EOF && ch != '\n');

Regards,
Ahamed.
0
anny**'s Avatar, Join Date: Oct 2008
Light Poster
thaanxx alot my program is running now !!
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by anny**
thaanxx alot my program is running now !!
If you found the answer helpful add to the reputation using the reputation icon beside the post