November 13th, 2006, 06:11 PM

Pascal help
I'm trying to make a program that will add numbers up that are entered until the user enters equal, e.g.
Enter number
1
Enter operator
+
Enter number
1
Enter operator
=
Answer: 2
However, with what i have so far the answer is usual 0, whatever i enter. This is the code I have so far and I am completely stuck. Can anyone help?
Code:
program calc2 (input, output);
uses wincrt;
var
operator: char;
number: real;
total: real;
procedure initialise;
begin
number:= 0;
total:= 0;
end;
procedure input;
begin
repeat
writeln('Enter the number you wish to use');
readln(number);
writeln('Enter the operator');
readln(operator);
case operator of
'+': total:= total + number;
'': total:= total  number;
'*': total:= total * number;
'/': total:= total / number;
end;
until operator = '=';
end;
procedure output;
begin
writeln('The final result of your calculation(s) is ',total:0:2,'.')
end;
begin
initialise;
input;
output;
end.
November 13th, 2006, 09:21 PM

Uhm, to start with, either you didn't indent your code, or else the positioning got lost despite your using the code tags. If it had been indented, then something must be funny with how it got copied; if you didn't indent it in the first place, then please start indenting your code, even for a simple program like this. It can make all the difference in the world for readability. I know that to a beginner, indentation can make code look like some form of bizarre free verse, but once you get used to it, it save a great deal of effort in trying to understand a program.
Pascal Code:
program calc2 (input, output);
uses wincrt;
var
operator: char;
number: real;
total: real;
procedure initialise;
begin
number:= 0;
total:= 0;
end;
procedure input;
begin
repeat
writeln('Enter the number you wish to use');
readln(number);
writeln('Enter the operator');
readln(operator);
case operator of
'+': total:= total + number;
'': total:= total  number;
'*': total:= total * number;
'/': total:= total / number;
end;
until operator = '=';
end;
procedure output;
begin
writeln('The final result of your calculation(s) is ', total:0:2, '.')
end;
begin
initialise;
input;
output;
end.
I would start by adding an otherwise clause to the case statement, to check against bad input (you'll need to add a dummy '=' case as well, so that it doesn't complain about that):
Pascal Code:
case operator of
'+': total:= total + number;
'': total:= total  number;
'*': total:= total * number;
'/': total:= total / number;
'=': { do nothing } ;
otherwise
writeln('That is not one of the options, please try again.');
end;
If there is an input problem, this should bring it to light right away. (As it happens, I didn't get any such issues when I compiled and ran the code using FreePascal  though I did need to rename 'operator' as 'op', since the Object Pascal dialect implemented by FreePascal uses that as a keyword  but I'm not sure what compiler you are using, and it's better to check these things anyway.)
I might add that as it is, the order of operations probably isn't quite what you intended. In effect, you are using reverse Polish notation on a twoelement stack, but since you only push the first element followed by the first operator, you are applying it to whatever was already there  zero, in this case. furthermore, the final value you enter before the equal sign gets ignored entirely. That is to say, if you enter '2', '*', '4', '+', '3', '=', then what you get is
0 * 2 = 0
0 + 4 = 4
3 = no action
so that the final value in total is 4, rather than 11 as you would probably expect. HTH.
Last edited by ScholRLEA; November 14th, 2006 at 11:37 AM.