Hello. I'm utterly stuck on this one part of my school assignment (for pascal). We are asked to make a calculator like program which calculates integer values only. The program has to read character by character from an input file and if something like an 'a' or # is entered, then an error message will popup telling the user they can't enter that in the input.

The problem I'm having right now is that I can't seem to properly convert the characters to integers and have them add/multiply/subtract/divide up with eachother correctly. If I input an equation with two numbers like: 1+2 or 120/30 or 1000+300 then it works just fine but if I go: 1+2+3 or 1+20+4 or 1+2+3+4 then I get crazy numbers like 1975 or 17245.

This is what my program looks like so far:

```
program bbb (input,output, infile);
const
blank = ' ';
var
infile : text;
c : char;
eqn, equation : string[20];
digit1, digit2, digit3, digit4 : integer;
previousnotblank, currentnotblank, legalinteger, previousnum : boolean;
{*****************}
procedure convert;
begin
digit1:=ord(c)-ord('0');
end; { convert }
{****************}
procedure multidigits;
begin
digit1:= digit1*10;
digit3:= ord(c)-ord('0');
digit1:= digit1+digit3;
previousnum:=legalinteger;
end; { multidigits }
{****************}
procedure multidigits2;
begin
digit2:= digit2*10;
digit4:= ord(c)-ord('0');
digit2:= digit2+digit4;
previousnum:=legalinteger;
end; { multidigits }
{*****************}
procedure convert2 ;
begin
legalinteger:= (ord(c)<=ord('9')) and (ord(c)>=ord('0'));
if c='-' then
begin
read(infile,c);
if (ord(c)<=ord('9')) and (ord(c)>=ord('0')) then
begin
digit2:=ord('0')-ord(c);
previousnum:=legalinteger;
end;
end;
while legalinteger and (c<>'+') do
begin
if legalinteger and previousnum then
multidigits2
else
if legalinteger then
begin
digit2:=ord(c)-ord('0');
writeln('convert2: digit2= ',digit2);
previousnum:=legalinteger;
end;
if not eoln(infile) then
read(infile,c)
else
legalinteger:=false;
end;
if (ord(c)>ord('9')) or (ord(c)<ord('0')) then
begin
writeln('error');
if (c=' ') then
writeln("There can't be an operation sign at the end.");
equation:='notcorrect';
end;
end; { convert2 }
{****************}
procedure plus;
begin
read(infile,c);
currentnotblank:= c <> blank;
previousnum:=false;
if not currentnotblank then
begin
while (c = blank) and not eoln(infile) do
begin
read(infile,c);
end;
convert2;
digit1:=digit1+digit2;
end;
if currentnotblank then
begin
convert2;
digit1:= digit1+digit2;
end;
end; { plus }
{****************}
begin
assign(infile, 'input.txt');
writeln('Enter equation: ');
readln(eqn);
rewrite(infile);
write(infile,eqn);
writeln(infile);
reset(infile);
while not eof(infile) do
begin
previousnum:=false;
while not eoln(infile) do
begin
read(infile,c);
currentnotblank:= c<>blank;
if currentnotblank then
begin
if (ord(c)>ord('9')) or (ord(c)<ord('*')) or (c='.') or (c='\') then
begin
writeln(' There is an error within the equation ');
equation:= 'notcorrect';
end;
legalinteger:= (ord(c)<=ord('9')) and (ord(c)>=ord('0'));
if legalinteger and previousnum then
multidigits
else
if legalinteger then
begin
convert;
previousnum:=legalinteger;
end;
if (c='+') then
plus
else
if (c='*') then
multi
else
if (c='/') then
divide
else
if (c='-') then
subtract;
end;
end; {while not eoln}
if equation = 'notcorrect' then
write('There is no answer.')
else
write('The answer is: ',digit1);
writeln;
readln(infile);
end; {while not eof}
end.
```

I left out the subtract, divide and multiply procedures since they look exactly like the plus procedure.

I think the source of the problem is probably with the while-loop under convert2. With an equation like 1+2+3+4, the program seems to be running smoothly when it reads 1, +, and 2, but once it reads the second +, it doesn't call up the plus procedure and instead tries to convert + into an integer.

Any help would be greatly appreciated!!!