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 :::::

#include <conio.h>

      #include <math.h>

      #include <stdio.h>

    #define PI 3.14159265


      int main()

      {



       float firstNumber, secondNumber;

      float resultSqrt, resultSin, resultCos, resultTan;

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

      }

Recommended Answers

All 7 Replies

The %c specifier doesn't ignore leading whitespace. And for future reference, it's %c, not %ch. %ch looks for a single character and then 'h'.

You need to flush the input buffer before you get the next input...

...
	printf("\n ENTER OPERATOR SIGN : ");
	char ch;
	while((ch = getc(stdin)) != EOF && ch != '\n');
	scanf("%c", &operation);
...

And you are missing %d, add that else you will not get the answer...

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

And like Narue said, its %c and not %ch...

>You need to flush the input buffer before you get the next input...
What if there's nothing to flush from the buffer before input?

> while((ch = getc(stdin)) != EOF && ch != '\n'); What's more likely to occur first, the END OF FILE or the ENTER key?

> scanf("%c", &operation); I think it is time for you to start learning how to check the return of scanf().
Better yet. Outgrow that function.

>You need to flush the input buffer before you get the next input...
What if there's nothing to flush from the buffer before input?

I said, we need to flush the input before getting the next input... of course the programmer would know when is the next input...

> while((ch = getc(stdin)) != EOF && ch != '\n'); What's more likely to occur first, the END OF FILE or the ENTER key?

Good question... probably, anything can halt this loop... and probably clear the input stream...

> scanf("%c", &operation); I think it is time for you to start learning how to check the return of scanf().
Better yet. Outgrow that function.

Yes, we should check the return value of scanf for a robust code... but here i was just pointing out his mistakes...

>What's more likely to occur first, the END OF FILE or the ENTER key?
What's more likely to have a significant impact on performance, an extra comparison, or an I/O operation? Also note that EOF is returned for more than just end-of-file. It's a generic failure code for getc. It's a common convention to check for failure before checking for validity.

>Better yet. Outgrow that function.
I still use scanf regularly. Have I stunted my growth? :icon_rolleyes:

>What's more likely to occur first, the END OF FILE or the ENTER key?
What's more likely to have a significant impact on performance, an extra comparison, or an I/O operation? Also note that EOF is returned for more than just end-of-file. It's a generic failure code for getc. It's a common convention to check for failure before checking for validity.

Still the question remains. Which one it is most likely to be encounter? And reversing the order of comparison doesn't rob anything in the way of a possible returned failure.

>Better yet. Outgrow that function.
I still use scanf regularly. Have I stunted my growth? :icon_rolleyes:

I am not a pediatrician, couldn't say.:icon_rolleyes:

>Still the question remains. Which one it is most likely to be encounter?
That's application dependent. You clearly think that a newline will be more common, but that may not be the case. Consider a situation where the the output of another program is piped to this program (after suitable changes to make it a proper filter), and that output is a single expression terminated by end-of-file.

>And reversing the order of comparison doesn't rob
>anything in the way of a possible returned failure.
It doesn't change the functionality of the program, no, but it does something akin to the practice of writing the constant first in a conditional test:

if ( EOF == getchar() )

By far the convention is to write the constant on the right side, and going against convention can make readers stumble.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.