SunQuest
           Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old November 13th, 2006, 05:11 PM
Chewbob Chewbob is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 1 Chewbob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
=
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.

Reply With Quote
  #2  
Old November 13th, 2006, 08:21 PM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,082 Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 10 h 16 m 50 sec
Reputation Power: 446
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
  1. program calc2 (input, output);
  2.   uses wincrt;
  3.   var
  4.     operator: char;
  5.     number: real;
  6.     total: real;
  7.  
  8.   procedure initialise;
  9.   begin
  10.     number:= 0;
  11.     total:= 0;
  12.   end;
  13.  
  14.   procedure input;
  15.   begin
  16.     repeat
  17.       writeln('Enter the number you wish to use');
  18.       readln(number);
  19.  
  20.       writeln('Enter the operator');
  21.       readln(operator);
  22.  
  23.       case operator of
  24.         '+': total:= total + number;
  25.         '-': total:= total - number;
  26.         '*': total:= total * number;
  27.         '/': total:= total / number;
  28.       end;
  29.  
  30.     until operator = '=';
  31.   end;
  32.  
  33.   procedure output;
  34.   begin
  35.     writeln('The final result of your calculation(s) is ', total:0:2, '.')
  36.   end;
  37.  
  38. begin
  39.   initialise;
  40.   input;
  41.   output;
  42. 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
  1.       case operator of
  2.         '+': total:= total + number;
  3.         '-': total:= total - number;
  4.         '*': total:= total * number;
  5.         '/': total:= total / number;
  6.         '=':    { do nothing }  ;
  7.         otherwise
  8.           writeln('That is not one of the options, please try again.');
  9.       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 10:37 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Pascal help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway