I tried a very simple code to add and subtract integer values using functions.
The code is listed below. When the user inputs a value for the 2nd Integer (int b in this case) it is automatically changed to a zero after the character input.. why does this happen ? The same code perfectly works when integers are substituted by floats.
Code:
#include<stdio.h>
#include <conio.h>
int Add(int a,int b);
int Sub(int a,int b);
int main()
{
int a;
int b;
char typ;
printf("\n Value A:");
scanf("%d",&a);
printf("\n Value B:");
scanf("%d",&b);
printf("\n Which? + or - : ");
scanf("%s",&typ);
switch(typ)
{
case '+':printf("\n The Anser is: %d\n",Add(a,b));
break;
case '-':printf("\n The Anser is: %d\n",Sub(a,b));
break;
}
getch();
return 0;
}
int Add(int a,int b)
{
int add;
add=a+b;
return add;
}
int Sub(int a,int b)
{
int sub;
sub=a-b;
return sub;
}