Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 13th, 2006, 06:11 PM
 Chewbob
Registered User

Join Date: Oct 2006
Posts: 1
Time spent in forums: 25 m 20 sec
Reputation Power: 0
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
=

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');

writeln('Enter the 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.```

#2
November 13th, 2006, 09:21 PM
 Schol-R-LEA
Commie Mutant Traitor

Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,800
Time spent in forums: 1 Month 2 Weeks 3 Days 2 h 27 m 30 sec
Reputation Power: 1569
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:
 Original - 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:
 Original - 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 two-element 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.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. \$4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : November 14th, 2006 at 11:37 AM.

 Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Pascal help