Correct the code and then something it works for me incorrectly

program corekey; label first,second,third; var a,x,b:real; p:char; begin write('ax+b=0 '); first: write('write a '); read(a); p:='y'; p:='n'; second: if a=0 then write('Banned 0! Input a again?(y/n) '); read(p); if p='y' then goto first; if (p<>'y')and(p<>'n') then goto second; write('write b '); read(b); x:=-b/a; write(x); third: write('Begining again? (y/n) '); read(p); if p='y' then goto first; if p='n' then write('Press Enter!'); if(p<>'y') and (p<>'n') then goto third; read; end. 

Closed due to the fact that off-topic participants Kromster , Denis , pavel , tutankhamun , HamSter September 20 '16 at 5:59 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, Denis, pavel, tutankhamun, HamSter
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    It would be very good if you wrote what this code should do - it is so much easier to figure out and find the error! .. - DelphiM0ZG
  • 2
    What exactly is the problem? What is shown incorrect work? write a better task - pj-infest
  • Banned 0 several times! Input a again? (Y / n) and this is not a learning task, I just train what I was asked to have become much easier and work fine, and this is buggy - MaximPro
  • Incorrectness in the output "Press Enter! Write a"? If yes, then write ("Press Enter!") Should appear after if ... goto thrird. In addition to write, there is also a procedure writeln (the first output is "ax + b = 0 write a") - alexlz
  • four
    it was necessary only to put the question a little differently: not “here’s a code for you, make it to the point”, but about “I found a mistake, but I can’t fix it, tell me who’s not difficult” using tags is considered a bad programming style, try to avoid them, or even forget that they exist, it is better to use cycles to write a condition, think about it, help - pj-infest

1 answer 1

Your main mistake is to use tags, but I’m missing it.

When it’s hard to figure out why a program is running, debuggers come to the rescue (step-by-step program execution)

so here we go on step by step:

after reading a = 0, the code is executed

if a = 0 then write ('Banned 0! Input a again? (y / n)');

read (p);

if the condition is fulfilled, you only have WRITE executed, the reading of r will be performed anyway, these two lines need to be taken in the BEGIN ... END operator brackets ....

if a = 0 then begin write ('Banned 0! Input a again? (y / n)'); read (p); end; go further when it comes to terms

if p = 'y' then goto first;

the first time it is not executed, so the code goes to the conditions

if (p <> 'y') and (p <> 'n') then goto second;

and here already, P has an incomprehensible value of why it is executed, and if the code is executed again from First to checks or from Second to checks, they are performed again and again, therefore the value of P does not change - that’s what a kind of looping turns out to be.

Output: When executing the code after the label, change the value of P to what neutral

Real output: use a loop with a post condition.

Here is the code without this repetition:

 program corekey; label first,second,third; var a,x,b:real; p:char; begin write('ax+b=0 '); first: p:='n'; writeln('write a '); readln(a); second : if a=0 then begin write('Banned 0! Input a again?(y/n) '); read(p); end; if p='y' then goto first; if (p<>'y')and(p<>'n') then goto second; write('write b '); read(b); x:=-b/a; write(x); third: writeln ('Begining again? (y/n) '); readln(p); if p='y' then goto first; if p='n' then write('Press Enter!'); if(p<>'y') and (p<>'n') then goto third; read; end. 
  • excuse me, besides this problem was different, and a special thank you to you - MaximPro
  • so the problem is solved? - pj-infest
  • not really if you type something else instead of y / n, it again displays again 3 repeats Banned 0! Input a again? (Y / n) - MaximPro
  • you get from point A to point B, through all the other letters of the alphabet - pj-infest
  • through the transition operators it is completely incomprehensible what, how and where? and at the same time also need to perform a bunch of checks that are buggy. Operator perchod is used in modern programming only where it is impossible to do without it, that is, in assembler, but in pascal it can always be replaced with the cycle Repeat ... Until (condition) and at the same time avoid a whole heap of checks if you decide to professionally do programming, then I advise you to make friends with the cycles, because at work you can get “scolding” for the transition operators - pj-infest