Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   y it is giving eroor :(:( (http://www.go4expert.com/forums/y-giving-eroor-t14492/)

anny** 11Oct2008 00:00

y it is giving eroor :(:(
 
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();

      } 


oogabooga 11Oct2008 21:31

Re: y it is giving eroor :(:(
 
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!

ahamed101 12Oct2008 09:26

Re: y it is giving eroor :(:(
 
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.

anny** 12Oct2008 18:35

Re: y it is giving eroor :(:(
 
thaanxx alot my program is running now !!

shabbir 13Oct2008 12:46

Re: y it is giving eroor :(:(
 
Quote:

Originally Posted by anny**
thaanxx alot my program is running now !!

If you found the answer helpful add to the reputation using the http://www.go4expert.com/images/buttons/reputation.gif reputation icon beside the post


All times are GMT +5.5. The time now is 13:19.