I have been working on this for days and it's the most annoying error. For some reason, everything works correctly the first time through, as you can see from the sample output. However, if you go through a second time (as the input is from the command line, i.e. if you enter one command, hit enter, then enter another one), it messes up. The first number to be interpreted always appears to be 0. However, I realized this is because for some reason in my bufferString, I believe the first character is a newline character. If you look at my output of the bufferString, there is obviously a newline before what is the correct number.

Also, the val function is returning an error after the first pass at location 1 in the string, which would be the first character(what I'm thinking is the new line character).

program interpreter(input, output);

type
	stackPointer = ^tStack;
	tStack = record
		elem : real;
                nextRecord: stackPointer
                end;

var
	readIn: char;
	bufferString: string;
	top, next: real;
	ready, endOfLine: boolean;
	currElement: stackPointer;
	errorMessage: integer;

procedure push;
var temp:stackPointer;
begin
	new(temp);
	temp^.nextRecord:=currElement;
	temp^.elem:=next;
	currElement:=temp;
end;

procedure pop;
var temp:stackPointer;
begin
	temp:=currElement;
	currElement:=currElement^.nextRecord;
	top:=temp^.elem;
	dispose(temp);
end;

procedure plus1;
var
	sum: real;
begin
	sum:=top+next;
	writeln(top, ' + ', next, ' = ', sum);
	next:=sum;
end;

procedure minus;
var
	difference: real;
begin
	difference:=top-next;
	writeln(top, ' - ', next, ' = ', difference);
	next:=difference;
end;

procedure multiply;
var
	product: real;
begin
	product:=top * next;
	writeln(top, ' * ', next, ' = ', product);
	next:=product;
end;

procedure divide;
var
	quotient: real;
begin
	quotient:= top / next;
	writeln(top, ' / ', next, ' = ', quotient);
	next:=quotient;
end;

begin
        bufferString:='';
        endOfLine:=false;
	while not eof(input) do
	begin
                writeln('bufferString before1:', bufferString,'\');
		read(readIn);


		if (readIn = ' ') or eoln(input) or eof(input) then
		begin

                        writeln('readIn=',readIn,';)');
			if eoln(input) then
			begin
                                writeln('What');
                                bufferString := readIn;

                                read(readIn);
                                endOfLine:=true;
                        end;

			if bufferString='+' then
			begin
                                writeln('+');
				pop;
				plus1;
			end

			else if bufferString='-' then
			begin
                                writeln('-');
				pop;
				minus;
			end

			else if bufferString='*' then
			begin
                                writeln('*');
				pop;
				multiply;
			end

			else if bufferString='/' then
			begin
                                writeln('/');
				pop;
				divide;
			end

			else

                        begin

                                push;

                                val(bufferString, next, errorCode);



			end;

			bufferString:='';

		end
		else
                begin

                        bufferString:= bufferString + readIn;
		end;
                                        if endOfLine=true then
                        begin
                                writeln('Answer = ', next);
                                endOfLine:=false;
                        end
	end;
end.

input:

1 2 +
bufferString before1:\
bufferString before1:1\
readIn= ;)
bufferString before1:\
bufferString before1:2\
readIn= ;)
bufferString before1:\
readIn=+;)
What
+
1.000000000000000E+000 + 2.000000000000000E+000 = 3.000000000000000E+000
Answer = 3.000000000000000E+000
bufferString before1:\

then it gives me a prompt, so I input:

2 3 +

output:

3 4 +
bufferString before1:
\
bufferString before1:
2\
readIn= ;)
bufferString before1:\
bufferString before1:3\
readIn= ;)
bufferString before1:\
readIn=+;)
What
+
0.000000000000000E+000 + 3.000000000000000E+000 = 3.000000000000000E+000
Answer = 3.000000000000000E+000
bufferString before1:\

I hope one of you can help me as a professor is letting me redo this. I have tried so hard to get it to work and can't get any help on campus due to finals.

not urgent. We may have time to look at it this time 2010.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.