# C programing DOUBT



## VINSTAR (Nov 24, 2007)

i write a C program to perform the functions of a calculator,
as this

#include<stdio.h>
#include<conio.h>
int sum(int,int);
int dif(int,int);
int pro(int,int);
float div(int,int);
void main()
{
 int a,b;
 char o;
 clrscr();
*printf("enter the numbers ");
 scanf("%d%d",&a,&b);
 printf("enter the operator(+,-,*,/)");
 scanf("%c",&o);
 getchar();*
 if(o=='+')
  printf("sum=%d",sum(a,b));
 else if(o=='-')
  printf("difference=%d",dif(a,b));
 else if(o=='*')
  printf("product=%d",pro(a,b));
 else if(o=='/')
  printf("%d/%d=%f",a,b,div(a,b));
 else
  printf("ERROR!!!");
 getch();
}
int sum(int a,int b)
{
 int r;
 r=a+b;
 return(r);
}
int dif(int a,int b)
{
 int r;
 r=a-b;
 return(r);
}
int pro(int a,int b)
{
 int r;
 r=a*b;
 return(r);
}
float div(int a,int b)
{
 float r;
 r=(float)a/b;
 return(r);
}

its not working. There is no error when compile.
i cannot input the operator(+,_,*,/).
but when i change the order of the program, ie 
input the operator first and then input the numbers
then the program is working fine. like this,,

#include<stdio.h>
#include<conio.h>
int sum(int,int);
int dif(int,int);
int pro(int,int);
float div(int,int);
void main()
{
 int a,b;
 char o;
 clrscr();
* printf("enter the operator(+,-,*,/)");
 scanf("%c",&o);
 getchar();
 printf("enter the numbers ");
 scanf("%d%d",&a,&b);*
 if(o=='+')
  printf("sum=%d",sum(a,b));
 else if(o=='-')
  printf("difference=%d",dif(a,b));
 else if(o=='*')
  printf("product=%d",pro(a,b));
 else if(o=='/')
  printf("%d/%d=%9.2f",a,b,div(a,b));
 else
  printf("ERROR!!!");
 getch();
}
int sum(int a,int b)
{
 int r;
 r=a+b;
 return(r);
}
int dif(int a,int b)
{
 int r;
 r=a-b;
 return(r);
}
int pro(int a,int b)
{
 int r;
 r=a*b;
 return(r);
}
float div(int a,int b)
{
 float r;
 r=(float)a/b;
 return(r);
}

Why is it happening so? i am using Turbo C++ 3.0
in windows xp sp2. Anybody PLS HLP ME.


----------



## webgenius (Nov 25, 2007)

use this statement after reading the numbers:
fflush(stdin);

include the header stdlib.h for this to work. Compile the program again n let me know.


----------



## fun2sh (Nov 26, 2007)

yep use

```
fflush(stdin);
```
after the first scanf statement
or else the scanf reads some garbage values from n stores it in O so u gettin that error!


----------



## manubatham20 (Nov 28, 2007)

You are asking for solution without giving that which error is generated. Please specify the error.


----------



## deepak.krishnan (Nov 29, 2007)

why r you using an extra getchar function after getting all the inputs from the user??? i dont think it is needed there...


----------



## ayush_chh (Dec 2, 2007)

yes there is no need to use *getchar()* but using *scanf()* for accepting characters is not recommended......the problem with your program is that buffer is blocked....so u may use *fflush(stdin);* before *scanf()* for clearing input buffer....or u can use this code

```
printf("enter the numbers ");
scanf("%d%d",&a,&b);
printf("enter the operator(+,-,*,/)");
o=getche();
getch();
if(o=='+')
```


----------



## VINSTAR (Dec 5, 2007)

THANKS TO ALL
Anybody pls help me to get the ebook copy of "LET US C" by yeshwant kanitkar


----------

