A C program help with char to int

So far ive been able to read expressions from textfile and calculate them, but only single digit numbers. how can i modify it to read two also? Any help would be much appreciated!
Code:
My input from file:
3+5
4*5
12-8
My outputfile (which did not output the last expression correctly, it's only reading one digit):
Press any key to continue . . .
3+5
Answer: 3 + 5 = 8

4*5
Answer: 4 * 5 = 20

12-8
Answer: 2 - 8 = -6

My code:
#include <stdio.h>
#include <stdlib.h>

int multFunction (int, int);
int divFunction (int, int);
int addFunction (int, int);
int subtractFunction (int, int);
int modFunction (int, int);

int main (void)
{
int iochar, operand1=0, operand2=0;
int count=0;
int symbol=0;
int calc, answer;

while ((iochar=getchar()) != EOF)
{

if (iochar==' ')
{
putchar(iochar);
}
else if (iochar !=' ' && iochar !='\n' && iochar != '+' && iochar != '*' && iochar != '/' && iochar != '%' && iochar != '-' && iochar != '\t')
{
putchar (iochar);
count++;

if (symbol==0)
{
operand1=iochar-48;
}
if (symbol==1)
{

operand2=iochar-48;
}

}

else if (iochar == '+' || iochar == '-' || iochar == '*' || iochar == '/' || iochar == '%')
{
symbol++;
putchar (iochar);
switch (iochar)
{
case '*':
{
calc=1;
break;
}
case '/':
{
calc=2;
break;
}
case '+':
{
calc=3;
break;
}
case '-':
{
calc=4;
break;
}
case '%':
{
calc=5;
break;
}
}
}
else if (iochar == '\n')
{
if (symbol>=2)
{
putchar ('\n');
printf ("Error: There can not be any negatives, or more than one operator per expression. \n \n");
symbol=0;
operand1=0;
operand2=0;
count=0;
answer=0;
}
else
{
putchar ('\n');
printf ("Answer: ");
if (calc==1)
{
answer= multFunction (operand1, operand2);
printf ("%d * %d = %d \n \n", operand1, operand2, answer);
operand1=0;
operand2=0;
count=0;
answer=0;
symbol=0;
}
else if (calc==2)
{
divFunction (operand1, operand2);

operand1=0;
operand2=0;
count=0;
symbol=0;
}
else if (calc==3)
{
answer=addFunction (operand1,operand2);
printf ("%d + %d = %d \n \n", operand1, operand2, answer);
operand1=0;
operand2=0;
count=0;
answer=0;
symbol=0;
}
else if (calc==4)
{
answer=subtractFunction (operand1, operand2);
printf ("%d - %d = %d \n \n", operand1, operand2, answer);
operand1=0;
operand2=0;
count=0;
answer=0;
symbol=0;
}
else if (calc==5)
{
answer=modFunction (operand1, operand2);
printf ("%d %% %d = %d \n \n", operand1, operand2, answer);
operand1=0;
operand2=0;
count=0;
answer=0;
symbol=0;
}
}

}

}
system ("pause");
return 0;
}
int multFunction (int digit1, int digit2)
{
return digit1*digit2;
}
int divFunction (int digit1, int digit2)
{

float result;
result= (float)digit1/digit2;
printf ("%d / %d = %5.3f.\n", digit1, digit2, result);
}
int addFunction (int digit1, int digit2)
{
return digit1+digit2;
}
int subtractFunction (int digit1, int digit2)
{
return digit1-digit2;
}
int modFunction (int digit1, int digit2)
{
return digit1%digit2;
}
your while loop processes one character at a time, but your mechanism for extracting operand 1 & 2 does not cater correctly for more than one charactor.

ie: the code:
1
2
3
4
if (symbol==0)
{
   operand1=iochar-48; 
}


does not apply a "Weight" (a power of 10) to the character being decoded and add to operand - instead it replaces any previous value stored in it.

Either you should keep some state (or derive) of what the weight should be per cycle for building your operands or you could simplify your program to read entire line into string, then find position of math symbol (+,-,*,/).

Once the position of this symbol is found you can store the part before it into a string for operand1 nd the part after into operand2.

To convert your string operands [say strOp1] to the integer operand operand1 you can do the following:

operand1 = atoi(strOp1);
Topic archived. No new replies allowed.