0
votes

I have to make an assignment (in Dutch) for Programming Logic. We have to use Structorizer and than convert/export it into Pascal code (yes, I know, prehistoric! But they say it's like learning "latin", if you learn it you have like a base.)

Now, I already had to figure out several bugs while converting my var's. But the main question I'd like to ask is this:

How come if I type in STOP (where in code it says : Read naam) the while loop actually continues and prints out the next line : write naam ' moet', lidgeld:0:2, ' euro lidgeld betalen.'

Instead of just jump out of the while loop because of the 'STOP' and print out the bottom lines?

In other code, it is fairly easy to check the {} and step by step monitoring it. But here with this code I'm totally lost. I used already an IF statement after read naam to make 'stop' work, but then it still prints out again write naam ' moet', lidgeld:0:2, ' euro lidgeld betalen.'

Pascal Code:

    { Generated by Structorizer 3.26-05 }
program Vereniging;

var
  { TODO: check and accomplish variable declarations }

  leeftijd: integer ; kinderen : integer ; leden : integer ; inkomen : real ; totaal : real ; lidgeld : real ; gemiddelde : real ; naam : string;


begin
  lidgeld := 0;
  leden := 0;
  totaal := 0;
  gemiddelde := 0;
  writeln('Geef naam en voornaam in: ');
  readln(naam);
  writeln;
  writeln('Geef de leeftijd in: ');
  readln(leeftijd);
  writeln;
  writeln('Geef het aantal kinderen ten laste in: ');
  readln(kinderen);
  writeln;
  writeln('Geef het jaarinkomen in: ');
  readln(inkomen);
  while (naam <> 'stop') or (naam <> 'STOP') do
  begin
    lidgeld := 10;
    if (leeftijd > 50) then
    begin
      lidgeld := lidgeld - 2;
      leden := leden + 1;
      totaal := totaal + lidgeld;
      gemiddelde := totaal / leden;
    end;
    if (kinderen > 0) and (kinderen < 6) then
    begin
      lidgeld := lidgeld - kinderen;
      leden := leden + 1;
      totaal := totaal + lidgeld;
      gemiddelde := totaal / leden;
    end
    else
    begin
      lidgeld := lidgeld - 5;
      leden := leden + 1;
      totaal := totaal + lidgeld;
      gemiddelde := totaal / leden;
    end;
    if (inkomen < 12500) then
    begin
      lidgeld := lidgeld - 2.5;
      leden := leden + 1;
      totaal := totaal + lidgeld;
      gemiddelde := totaal / leden;
    end;
    if (leeftijd > 50) and (kinderen >= 5) and (inkomen < 12500) then
    begin
      lidgeld := lidgeld - 8.5;
      leden := leden + 1;
      totaal := totaal + lidgeld;
      gemiddelde := totaal / leden;
    end;
    writeln;
    writeln(naam, ' moet ', lidgeld:0:2, ' euro lidgeld betalen.');
    writeln;
    writeln('Geef naam en voornaam in: ');
    readln(naam);
    writeln;
    writeln('Geef de leeftijd in: ');
    readln(leeftijd);
    writeln;
    writeln('Geef het aantal kinderen ten laste in: ');
    readln(kinderen);
    writeln;
    writeln('Geef het jaarinkomen in: ');
    readln(inkomen);
    writeln;
  end;
  writeln;
  writeln('Het totaal aantal leden : ', leden);
  writeln;
  writeln('Het totaal aantal lidgeld : ', totaal:0:2, ' euro.');
  writeln;
  writeln('Het gemiddelde : ', gemiddelde:0:2, ' euro.');
  writeln;
  writeln('Druk op <ENTER> om het programma af te sluiten');
  readln();
end. 

NSD picture

2

2 Answers

1
votes

The condition on your WHILE...DO loop is wrong - regardless of what you type in, at least one of those two conditions will be true, and the loop will execute (because A OR B is TRUE if either A is TRUE or B is TRUE):

If you type in stop, then the condition (naam <> 'STOP') will be true, and the loop will execute.

If you type in STOP, then the condition (naam <> 'stop') will be true, and the loop will executed.

What you want to do is compare with 'STOP' in a case-insensitive manner, so that regardless of what you type - STOP, stop, Stop, StOp, etc. - the test will be TRUE. Or, alternatively, if you type anything other than some variation on the word stop, the test will be FALSE.

0
votes

I know, prehistoric...

Even if prehistoric, many programming languages still have to learn something from pascal. And many programmers too.

If you really want that, after typing "STOP" in reply to the program's prompt "Geef naam en voornaam in: ", the program stops to ask questions, then you have to add the following line:

if (naam='stop') or (naam='STOP') then break;

exactly after this line from you program:

readln(naam);

Otherwise, as said in the previous answer, you must change the cycle prologue like this:

while (naam<>'stop') AND (naam<>'STOP') do begin...

but this will not prevent the program to continue to ask for "Geef de leeftijd", "Geef het aantal kinderen", "Geef het jaarinkomen in".

Is it clear? The main bug depicted in your question is your understanding of programming, not the pascal language.