Why is this pascal code giving me a runtime error 106?

Status
Not open for further replies.

Zer000

Ars Tribunus Militum
1,665
I have this assignment where I have to write a program that balances your checkbook. It is supposed to input a letter followed by a real number, where the letter is a transaction code and the number is the amount of the transaction. Anyway, all I have right now is the code to enter the char and the real and quit if the char is 'e'. It works fine the first time, but then the second time around it gives me a runtime error 106 right after I enter the char. As I understand it, 106 means you are trying to put a non-number into a number, but thats not what I'm doing. I'm stumped, any help?

Code:
Program checking;
var balance, amount, test : real;
done : boolean;
trans : char;

procedure getTransaction;
begin
	writeln('Enter a transaction:');
	read(trans, amount);
	if trans = 'e' then
		done := true;
end;

begin
	done := false;	
	writeln('Enter the beginning balance:');
	readln(balance);
	while not done do begin
		getTransaction;
		writeln(trans, amount:10:2);
	end;
end.
And the output:
Code:
F:\Pascal>checking
Enter the beginning balance:
455
Enter a transaction:
c 400
c    400.00
Enter a transaction:
d 500
Runtime error 106 at 0x0040107E
  0x0040107E
  0x0040110B
  0x00401199
 
I'll admit I haven't done pascal in a long long time, but this sounds like the common "enter stayed in the buffer" problem. Have you tried a string reading function instead of reading a single char? Or maybe looping on reading the char until you get a valid input for that, and then reading the real. Just make sure the char read isn't the enter and the char you type isn't read as the real. Another thing to try would be not to enter the char the second time around (only the real), and see what it does.

Edit: If I'm way off base, forget all that :D
 
Status
Not open for further replies.