1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

y it is giving eroor :(:(

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

  1. anny**

    anny** New Member

    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

    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

    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

    thaanxx alot my program is running now !!
     
  5. shabbir

    shabbir Administrator Staff Member

    If you found the answer helpful add to the reputation using the [​IMG] reputation icon beside the post
     

Share This Page