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;
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: