Thread: Pascal help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    1
    Rep 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.
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    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 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.
    Last edited by Schol-R-LEA; November 14th, 2006 at 10:37 AM.
    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 Short Understanding the C/C++ Preprocessor
    Taming Python A 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

IMN logo majestic logo threadwatch logo seochat tools logo