y it is giving eroor :(:(

Discussion in 'C' started by anny**, Oct 10, 2008.

  1. anny**

    anny** New Member

    Joined:
    Oct 4, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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:

    #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();

          }
     
  2. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    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!
     
  3. ahamed101

    ahamed101 New Member

    Joined:
    Sep 27, 2008
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    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:
    char ch;
    while((ch = getchar()) != EOF && ch != '\n');
    
    Regards,
    Ahamed.
     
  4. anny**

    anny** New Member

    Joined:
    Oct 4, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    thaanxx alot my program is running now !!
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,335
    Likes Received:
    377
    Trophy Points:
    83
    If you found the answer helpful add to the reputation using the [​IMG] reputation icon beside the post
     
    Last edited: Jan 21, 2017

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice