Click here to Skip to main content
15,912,021 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>

int main()
{
	int a=0,b=0;
	int sum,diff,mult,R;
	float div;
	char c,ch;

do
{
	printf("\n\n\tENTER THE FIRST operand   ");
	scanf("%d",&a); //takes value for a for eg 7
	printf("\n\n\tENTER THE SECOND operand   ");
	scanf("%d",&b);//takes value for b for eg 4
	printf("\t\n\n_______________________________");
	printf("\t | ENTER THE DESIRED OPERATOR |");
	printf("\t_______________________________");
	printf("\n\n \t\tFOR SUMMATION PRESS +  \n\n \t\tFOR DIFFERENCE PRESS -  \n\n \t\tFOR PRODUCT PRESS * \n\n \t\tFOR DIVISION PRESS / \n\n \t\tTO FIND REMAINDER ONLY PRESS %% \n\n \t\tPRESS 0 TO EXIT\n\n");
	scanf("%s",&c);
	printf("\t\n\n  OPERATION TO BE PERFORMED IS '%c' PRESS ANY KEY TO CONTINUE",c);
	getch();

 if(c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='0')
{
	printf("\n\n |RESULTS|");

	if(c=='-')
	{	
	diff=a-b;
	printf("\n\nDIFFERENCE OF %d AND %d IS : %d \n\n",a,b,diff);// here it should give output as 7-4=3 but it gives 7-0=7 . why is it so
getch();
}  else if(c=='+')
{	
	sum=a+b;
	printf("\n\n\tSUMMATION OF %d AND %d IS : %d \n\n",a,b,sum);
	getch();
} else if(c=='*')
{	
	mult=a*b;
	printf("\n\n\tPORDUCT OF %d AND %d IS : %d \n\n",a,b,mult);
	getch();
} else if(c=='/')
{	
	div=a/(float)b;
	printf("\n\n\tDIVISION OF %d AND %d IS : %f \n\n",a,b,div);
	getch();
} else if(c=='%')
{	
	R=a%b;
	printf("\n\n\tREMAINDER WHEN %d IS DIVIDED BY %d IS : %d \n\n",a,b,R);
	getch();
}
}
else {
	printf("\n\n\n\t\t\t  INVALID OPERATOR ENTER CORRECT OPERATOR ");
	getch();
}

}while(c!= '0');
return 0;
}


What I have tried:

i tried to initialise values at the begning i.e int a=7,b=4;
by doing this it gives correct thing but when we use scanf for getting input from user it doesnt works
Posted
Updated 30-May-17 21:55pm
v3
Comments
Richard MacCutchan 31-May-17 4:22am    
Works fine for me, once I removed all those redundant calls to getch().
dawar budroo 31-May-17 4:26am    
i tried this but still wn i enter the inputs for a and b it assighns b to zero in output


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>

main()
{

int a=0,b=0;
int sum;
char c;




printf("\n\n\tENTER THE FIRST operand ");
scanf("%d",&a);
printf("\n\n\tENTER THE SECOND operand ");
scanf("%d",&b);

printf("\n\n \t\tFOR SUMMATION PRESS + ");

scanf("%s",&c);

if(c=='+')
{


printf("\n\n |RESULTS|");




sum=a+b;
printf("\n\n\tSUMMATION OF %d AND %d IS : %d \n\n",a,b,sum);
getch();

}
else {

printf("\n\n\n\t\t\t INVALID OPERATOR ENTER CORRECT OPERATOR ");
getch();



}
}
dawar budroo 31-May-17 4:28am    
ENTER THE FIRST operand 7


ENTER THE SECOND operand 8


FOR SUMMATION PRESS + +


|RESULTS|

SUMMATION OF 7 AND 0 IS : 7


i get this as output

1 solution

Read the scanf documentationcarefully. Take also a look on the example code to understand the string use.

Your c is a char not a string.

So correct code would be:
C++
scanf("%c",&c);
 
Share this answer
 
Comments
dawar budroo 31-May-17 3:43am    
please run the program and see what is the error ... i am using symbols + - /and others so in c u use %s not %c issue lies in the inputs u give
dawar budroo 31-May-17 3:46am    
printf("\n\n\tENTER THE FIRST operand ");
scanf("%d",&a); //takes value for a for eg 7
printf("\n\n\tENTER THE SECOND operand ");
scanf("%d",&b);//takes value for b for eg 4
............
.......
diff=a-b;
printf("\n\nDIFFERENCE OF %d AND %d IS : %d \n\n",a,b,diff);// here it should give output as 7-4=3 but it gives 7-0=7 . why is it so

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900