Look at my code here, my do while loop got some sort of problem but i dont know how to fix it. The problem is when i loop again, it will not return my answer to zero first so that the answer previous will not affect the answer after the loop.
Code:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int main()
{
char txt[200];
char temp[50][50];
char opt[200];
char par[200];
float num[200];
int keeploop = 0;
do{
int i=0, j=0, k=0, n=0, m=0, xyz = 0;
int yee = 0;
float ans = 0;
int asd = 0;
printf("?\n");
gets(txt);
i = strlen(txt);
for(j=0;j<i;j++)
{
if(txt[j] == '(' || txt[j] == ')')
{
par[xyz] = txt[j]; xyz++;
}
else if(!(txt[j]=='+' || txt[j]=='-' || txt[j]=='*' || txt[j]=='/' || txt[j]=='^'))
{
temp[m][k] = txt[j]; k++;
}
else
{
opt[n] = txt[j]; n++;
num[m] = atof(temp[m]); m++;
k=0;
}
}
num[m] = atof(temp[m]); m++;
asd = strlen(opt);
for(j=0;j<asd;j++)
{
if(opt[j]=='^')
{
ans = pow(num[j],num[j+1]);
for(yee=j;yee<asd+1;yee++)
{
num[yee]=num[yee+1];
}
for(yee=j;yee<asd;yee++)
{
opt[yee] = opt[yee+1];
}
num[j] = ans; j--;
}
}
for(j=0;j<asd;j++)
{
if(opt[j]=='*')
{
ans = num[j] * num[j+1];
for(yee=j;yee<asd+1;yee++)
{
num[yee]=num[yee+1];
}
for(yee=j;yee<asd;yee++)
{
opt[yee] = opt[yee+1];
}
num[j] = ans; j--;
}
if(opt[j]=='/')
{
ans = num[j] / num[j+1];
for(yee=j;yee<asd+1;yee++)
{
num[yee]=num[yee+1];
}
for(yee=j;yee<asd;yee++)
{
opt[yee] = opt[yee+1];
}
num[j] = ans; j--;
}
}
for(j=0;j<asd;j++)
{
if(opt[j]=='+')
{
ans = num[j] + num[j+1];
for(yee=j;yee<asd+1;yee++)
{
num[yee]=num[yee+1];
}
for(yee=j;yee<asd;yee++)
{
opt[yee] = opt[yee+1];
}
num[j] = ans; j--;
}
if(opt[j]=='-')
{
ans = num[j] - num[j+1];
for(yee=j;yee<asd+1;yee++)
{
num[yee]=num[yee+1];
}
for(yee=j;yee<asd;yee++)
{
opt[yee] = opt[yee+1];
}
num[j] = ans; j--;
}
}
printf("%f\n\n",ans);
printf("%f\n",num[0]);
printf("%f\n",num[1]);
printf("%f\n",num[2]);
printf("%f\n",num[3]);
printf("opt\n\n",ans);
printf("%c\n",opt[0]);
printf("%c\n",opt[1]);
printf("%c\n",opt[2]);
}while(keeploop != 1);
getchar();
}
example first time loop i enter 123+456, then it return 579 as answer, then again i enter 5+6, it return different answer which is 1179. How do i fix this problem?