we can only use getchar, putchar, no arrays, no scanf for the calculations. printf is ok.
so this program continously loops around the first if statement never processing the rest of the information.. how do i stop it and allow it to run the actualy calculation.
the user is supposed to put in " 3 + 5" and then the program outputs "3 + 5 = 8"
I am really stuck on it.
That is what I got.
Code:
#include<stdio.h>
#include<ctype.h>
int addition(int,int);
int subtraction(int,int);
int multiplication(int ,int);
double division(int,int);
int module(int,int);
int convert(int);
int main()
{
int num1,num2,ch,test=0,operation;
char answer;
do
{
printf(" Please enter your expresion\n");
while((ch=getchar())!='+' && ch!='-' && ch!='*' && ch!='/' && ch!='%')
{
if(test==1)
{
if(ch!=' ' && ch!='\t')
{
putchar(ch);
test=0;
}
}
else
{
if(ch==' ' || ch=='\t')
test=1;
else
{
putchar(ch);
test=1;
}
}
if(ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='%')
operation=ch;
}
putchar(operation);
putchar('\n');
printf("Would you lie to continue?(Y/N) ");
scanf("%c",&answer);
getchar();
} while(answer=='y');
return 0;
}
int addition(int num1,int num2)
{
return num1+num2;
}
int subtraction(int num1,int num2)
{
return num1-num2;
}
int multiplication(int num1,int num2)
{
return num1*num2;
}
double division(int num1, int num2)
{
return num1/num2;
}
int module(int num1,int num2)
{
return num1%num2;
}
int convert(int ch)
{
int sum;
sum=0;
while(((ch=getchar())!=' ') && ch!='\n')
{
if(!isdigit(ch))
break;
sum=sum*10 + ch-'0';
}
return sum;
}