DIGIT-ARC read-only archive · snapshot: July 23, 2026

C programing DOUBT

7 posts · originally from https://geek.digit.in/community/threads/c-programing-doubt.74047/

VINSTAR
VINSTAR Original post #1
Nov 24, 2007 at 10:01 PM
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 at 12:43 AM
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
fun2sh #3
Nov 26, 2007 at 1:26 PM
yep use
Code:
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!
M
Nov 28, 2007 at 7:00 PM
You are asking for solution without giving that which error is generated. Please specify the error.
deepak.krishnan
Nov 29, 2007 at 1:53 PM
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 at 12:41 AM
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
Code:
printf("enter the numbers ");
scanf("%d%d",&a,&b);
printf("enter the operator(+,-,*,/)");
o=getche();
getch();
if(o=='+')
VINSTAR
VINSTAR #7
Dec 5, 2007 at 5:50 PM
THANKS TO ALL
Anybody pls help me to get the ebook copy of "LET US C" by yeshwant kanitkar