Problem with a program

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 :
[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.[/code]

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.

Comments

  • Try "HALT" or "EXIT"

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

    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".


  • : Try "HALT" or "EXIT"
    :

    If I write one of them I get an error.
  • : : 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 [b]begin[/b] and [b]end[/b] and let it be the [b]else[/b] part.

    [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')
    else [b]begin[/b]
    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');
    [b]end[/b]
    END.
    [/code]
  • : : : 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
    : [b]begin[/b] and [b]end[/b] and let it be the [b]else[/b] part.
    :
    : [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')
    : else [b]begin[/b]
    : 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');
    : [b]end[/b]
    : END.
    : [/code]:

    It worked, thanks!

  • : : Try "HALT" or "EXIT"
    : :
    :
    : If I write one of them I get an error.
    :
    What error are you getting? What compiler are you using?

  • : : : 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.
  • : : : : 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?
  • Oops, double posted.
  • : : : : : : 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?
    : :
    :
    : [code]: 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.[/code]:
    :

  • : : : : : : : 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?
    : : :
    : :
    : : [code]: : 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.[/code]: :
    : :
    :
    :
    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.

  • : [code]: PROGRAM bla;
    {...}
    writeln ('Please enter middle');
    readln (M);
    T:=R;
    R:=M;
    M:=T;
    writeln ('');
    writeln ('Here are the numbers after one switch: ');
    {...}
    END.[/code]:

    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
  • : : [code]: : PROGRAM bla;
    : {...}
    : writeln ('Please enter middle');
    : readln (M);
    : T:=R;
    : R:=M;
    : M:=T;
    : writeln ('');
    : writeln ('Here are the numbers after one switch: ');
    : {...}
    : END.[/code]: :
    :
    : 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.
    [code]
    VAR
    L:integer; {Left}
    R:integer; {Right}
    M:integer; {Middle}
    T:integer; {Temporary}
    [/code]
    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.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories