Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Problem with a program Posted by Meroosher on 7 Oct 2008 at 9:05 AM
I want to make a program that switches between 3 letters which are at the left, middle and right, each one of them has a number.
One of the letters in the sides is 0. I want to place the 0 in the middle.
Here is my code :
PROGRAM bla;
USES WINCRT;
VAR
  L:integer; {Left}
  R:integer; {Right}
  M:integer; {Middle}
  T:integer; {Temporary}
BEGIN
  writeln('Please enter three number, one of them is zero.(it musnt be in the middle)');
  writeln ('Please enter left');
  readln (L);
  writeln ('Please enter right');
  readln (R);
  writeln ('Please enter middle');
  readln (M);
  T:=R;
  R:=M;
  M:=T;
  writeln ('');
  writeln ('Here are the numbers after one switch: ');
  writeln ('Left: ',L);
  writeln ('Right: ',R);
  writeln ('Middle: ',M);
  if (M=0) then
  writeln ('The 0 is now at the middle')
  {Nothing or END. nothing really works...}
  else
  T:=L;
  L:=M;
  M:=T;
  writeln ('');
  writeln ('Here are the numbers after two swithces: ');
  writeln ('Left: ',L);
  writeln ('Right: ',R);
  writeln ('Middle: ',M);
  writeln ('');
  writeln ('The 0 is now at the middle');
END.


My problem is:
I want to make the program stop once the 0 is in the middle. I tried to add "END." after the then statement but it didn't work. Any ideas?
Thanks.
Report
Re: Problem with a program Posted by Actor on 7 Oct 2008 at 10:57 AM
Try "HALT" or "EXIT"

If you are going to leave the writeln as part of the then, then don't forget to enclose the writeln and the halt with begin and end, otherwise the compiler will not know what to do with the else. Actually, as you have written your program, you don't need he else, but you still need begin and end.
     if (M=0) then begin
          writeln ('The 0 is now at the middle') ;
          halt
     end
     else


Here's more food for thought. What happens if your user does not input valid data?

Yor require that the user input one zero and that it not be in the middle. What does your program do if the user gets it wrong and he puts the zero in the middle?

What happens if he does not enter a zero? What if he enters more than one?

One final thought: when I first read your code I read "musnt" as "must". I suggest you change this to "must not".


Report
Re: Problem with a program Posted by Meroosher on 7 Oct 2008 at 11:54 AM
: Try "HALT" or "EXIT"
:

If I write one of them I get an error.
Report
Re: Problem with a program Posted by Actor on 7 Oct 2008 at 1:01 PM
: : Try "HALT" or "EXIT"
: :
:
: If I write one of them I get an error.
:
The best option is to surround the rest of your code with begin and end and let it be the else part.

PROGRAM bla;
USES WINCRT;
VAR
  L:integer; {Left}
  R:integer; {Right}
  M:integer; {Middle}
  T:integer; {Temporary}
BEGIN
  writeln('Please enter three number, one of them is zero.(it musnt be in the middle)');
  writeln ('Please enter left');
  readln (L);
  writeln ('Please enter right');
  readln (R);
  writeln ('Please enter middle');
  readln (M);
  T:=R;
  R:=M;
  M:=T;
  writeln ('');
  writeln ('Here are the numbers after one switch: ');
  writeln ('Left: ',L);
  writeln ('Right: ',R);
  writeln ('Middle: ',M);
  if (M=0) then
       writeln ('The 0 is now at the middle')
   else begin
     T:=L;
     L:=M;
     M:=T;
     writeln ('');
     writeln ('Here are the numbers after two swithces: ');
     writeln ('Left: ',L);
     writeln ('Right: ',R);
     writeln ('Middle: ',M);
     writeln ('');
     writeln ('The 0 is now at the middle');
  end 
END.
Report
Re: Problem with a program Posted by Meroosher on 7 Oct 2008 at 1:14 PM
: : : Try "HALT" or "EXIT"
: : :
: :
: : If I write one of them I get an error.
: :
: The best option is to surround the rest of your code with
: begin and end and let it be the else part.
:
:
: 
: PROGRAM bla;
: USES WINCRT;
: VAR
:   L:integer; {Left}
:   R:integer; {Right}
:   M:integer; {Middle}
:   T:integer; {Temporary}
: BEGIN
:   writeln('Please enter three number, one of them is zero.(it musnt be in the middle)');
:   writeln ('Please enter left');
:   readln (L);
:   writeln ('Please enter right');
:   readln (R);
:   writeln ('Please enter middle');
:   readln (M);
:   T:=R;
:   R:=M;
:   M:=T;
:   writeln ('');
:   writeln ('Here are the numbers after one switch: ');
:   writeln ('Left: ',L);
:   writeln ('Right: ',R);
:   writeln ('Middle: ',M);
:   if (M=0) then
:        writeln ('The 0 is now at the middle')
:    else begin
:      T:=L;
:      L:=M;
:      M:=T;
:      writeln ('');
:      writeln ('Here are the numbers after two swithces: ');
:      writeln ('Left: ',L);
:      writeln ('Right: ',R);
:      writeln ('Middle: ',M);
:      writeln ('');
:      writeln ('The 0 is now at the middle');
:   end 
: END.
: 
:

It worked, thanks!

Report
Re: Problem with a program Posted by Actor on 7 Oct 2008 at 4:08 PM
: : Try "HALT" or "EXIT"
: :
:
: If I write one of them I get an error.
:
What error are you getting? What compiler are you using?

Report
Re: Problem with a program Posted by Meroosher on 8 Oct 2008 at 5:00 PM
: : : Try "HALT" or "EXIT"
: : :
: :
: : If I write one of them I get an error.
: :
: What error are you getting? What compiler are you using?
:
:

I'm not getting any error. It's just not stopping after if finds the 0 in the beggining.

And I'm using TPW7.
Report
Re: Problem with a program Posted by Actor on 10 Oct 2008 at 12:31 AM
: : : : Try "HALT" or "EXIT"
: : : :
: : :
: : : If I write one of them I get an error.
: : :
: : What error are you getting? What compiler are you using?
: :
: :
:
: I'm not getting any error. It's just not stopping after if finds the
: 0 in the beggining.
:
: And I'm using TPW7.
:
Can you post the code that doesn't stop?
Report
Re: Problem with a program Posted by Meroosher on 10 Oct 2008 at 2:48 PM
Oops, double posted.
Report
Re: Problem with a program Posted by Meroosher on 10 Oct 2008 at 2:49 PM
: : : : : : Try "HALT" or "EXIT"
: : : : : :
: : : : :
: : : : : If I write one of them I get an error.
: : : : :
: : : : What error are you getting? What compiler are you using?
: : : :
: : : :
: : :
: : : I'm not getting any error. It's just not stopping after if finds the
: : : 0 in the beggining.
: : :
: : : And I'm using TPW7.
: : :
: : Can you post the code that doesn't stop?
: :
:
:
: PROGRAM first;
: USES WINCRT;
: VAR
:   L:integer; {Left}
:   R:integer; {Right}
:   M:integer; {Middle}
:   T:integer; {Temporary}
: BEGIN
:   writeln('There are three letters on a desk.');
:   writeln('One at the left side. One at the middle, and one at the right side of the desk.');
:   writeln('On each letter there is a number.');
:   writeln('The purpose of this program is: ');
:   writeln('Make the 0 numbered letter be placed at the middle.');
:   writeln('');
:   writeln('Please enter the number of the left letter');
:   readln (L);
:   writeln ('Please enter the number of the right letter');
:   readln (R);
:   writeln ('Please enter the number of the middle letter');
:   readln (M);
:   T:=R;
:   R:=M;
:   M:=T;
:   writeln ('');
:   writeln ('Here are the numbers after one switch: ');
:   writeln ('Left: ',L);
:   writeln ('Right: ',R);
:   writeln ('Middle: ',M);
:   if (M=0) then
:   writeln ('The 0 is now at the middle')
:   EXIT {or HALT? I also tried with ; and . after  but it didn't work}
:   else 
:    T:=L;
:    L:=M;
:    M:=T;
:    writeln ('');
:    writeln ('Here are the numbers after two swithces: ');
:    writeln ('Left: ',L);
:    writeln ('Right: ',R);
:    writeln ('Middle: ',M);
:    writeln ('The 0 is now at the middle');
: END.
:
:

Report
Re: Problem with a program Posted by Actor on 12 Oct 2008 at 1:26 AM
: : : : : : : Try "HALT" or "EXIT"
: : : : : : :
: : : : : :
: : : : : : If I write one of them I get an error.
: : : : : :
: : : : : What error are you getting? What compiler are you using?
: : : : :
: : : : :
: : : :
: : : : I'm not getting any error. It's just not stopping after if finds the
: : : : 0 in the beggining.
: : : :
: : : : And I'm using TPW7.
: : : :
: : : Can you post the code that doesn't stop?
: : :
: :
: :
: : PROGRAM first;
: : USES WINCRT;
: : VAR
: :   L:integer; {Left}
: :   R:integer; {Right}
: :   M:integer; {Middle}
: :   T:integer; {Temporary}
: : BEGIN
: :   writeln('There are three letters on a desk.');
: :   writeln('One at the left side. One at the middle, and one at the right side of the desk.');
: :   writeln('On each letter there is a number.');
: :   writeln('The purpose of this program is: ');
: :   writeln('Make the 0 numbered letter be placed at the middle.');
: :   writeln('');
: :   writeln('Please enter the number of the left letter');
: :   readln (L);
: :   writeln ('Please enter the number of the right letter');
: :   readln (R);
: :   writeln ('Please enter the number of the middle letter');
: :   readln (M);
: :   T:=R;
: :   R:=M;
: :   M:=T;
: :   writeln ('');
: :   writeln ('Here are the numbers after one switch: ');
: :   writeln ('Left: ',L);
: :   writeln ('Right: ',R);
: :   writeln ('Middle: ',M);
: :   if (M=0) then
: :   writeln ('The 0 is now at the middle')
: :   EXIT {or HALT? I also tried with ; and . after  but it didn't work}
: :   else 
: :    T:=L;
: :    L:=M;
: :    M:=T;
: :    writeln ('');
: :    writeln ('Here are the numbers after two swithces: ');
: :    writeln ('Left: ',L);
: :    writeln ('Right: ',R);
: :    writeln ('Middle: ',M);
: :    writeln ('The 0 is now at the middle');
: : END.
: :
: :
:
:
Are you saying that this code compiles? I would not expect it to due to there being no semicolon after the writeln statement in the IF construct. On my computer it does not compile.

Report
Re: Problem with a program Posted by Phat Nat on 15 Oct 2008 at 10:47 PM
:
: PROGRAM bla;
  {...}
  writeln ('Please enter middle');
  readln (M);
  T:=R;
  R:=M;
  M:=T;
  writeln ('');
  writeln ('Here are the numbers after one switch: ');
  {...}
END.
:

Also note that the original value of T will be lost as T becomes R and then when M:=T; comes, it is actually M:=R; If you want to rotate, you will need a fourth variable to use as a temporary storage for T.

Phat Nat
Report
Re: Problem with a program Posted by Actor on 16 Oct 2008 at 9:20 AM
: :
: : PROGRAM bla;
:   {...}
:   writeln ('Please enter middle');
:   readln (M);
:   T:=R;
:   R:=M;
:   M:=T;
:   writeln ('');
:   writeln ('Here are the numbers after one switch: ');
:   {...}
: END.
: :
:
: Also note that the original value of T will be lost as T becomes R
: and then when M:=T; comes, it is actually M:=R; If you want to
: rotate, you will need a fourth variable to use as a temporary
: storage for T.
:
: Phat Nat

T is the fourth variable as you can see from this fragment of his code.
VAR
  L:integer; {Left}
  R:integer; {Right}
  M:integer; {Middle}
  T:integer; {Temporary}

And he's not rotating. He's switching one end and then, if the first switch did not do the job, he switches the other end.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.