Recent

Author Topic: A lot If-Else-If  (Read 10363 times)

NightWing

  • Newbie
  • Posts: 5
A lot If-Else-If
« on: May 08, 2018, 12:03:40 am »
Hello, i got this code and idk why show me that.. I try to make an calculator of bytes to another one.
I can only use nested if & else, already add & delete so much ' ; ' trying to make it work but i don't know how.
{
>> fpc puff.pas                                                                 

Free Pascal Compiler version 3.0.4 [2017/10/02] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling puff.pas
puff.pas(320,5) Fatal: Syntax error, ";" expected but "ELSE" found
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode

}

I'm begginer and no, it's not homework.. I only try to learn & understand pascal.. This forum is my last resource     :(   :(  :'(
(compiled on CentOs' 7 terminal)
« Last Edit: May 21, 2018, 12:36:06 am by NightWing »

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: i don't know why
« Reply #1 on: May 08, 2018, 12:42:52 am »
[…] i got this code […]
OMG. “Someone else gave it to me.” … Sure. (x.X)

I threw the source code into ptop(1). Maybe this'll help you identify the problem. I won't read 300 lines of code doing this for you. Especially since I have difficulties determining “should at this line be an end, or not?” This is up to you.
Yours Sincerely
Kai Burghardt

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: i don't know why
« Reply #2 on: May 08, 2018, 12:56:09 am »
I tried formatting the code, which helps high light where things are a miss
I got so far and ran out of indentation. The case statement is not completed, sorry spent too much time formatting code to try to work out whats wrong. Hopefully you can work out whats wrong, it falls apart around line 298 the 'end' statement is ending the case item.
Code: Pascal  [Select][+][-]
  1. program puff;
  2. uses crt;
  3. var
  4.         mercurio, tierra, marte, jupiter, saturno, urano, neptuno, neptunoB, neptunoK, neptunoM, neptunoG, neptunoT, neptunoP, neptunoMB, neptunoMK, neptunoMM, neptunoMG, neptunoMT, neptunoMP, pluton : int64;
  5.         venus : string; { o char}
  6. begin
  7.   clrscr;
  8.   writeln('Hello there!');
  9.   writeln('Ingrese la cantidad de bytes a convertir: ');
  10.   readln(mercurio);
  11.  
  12.   writeln('');
  13.   writeln('¿A que unidad los quiere convertir?');
  14.   writeln(' k = kilobytes, m = megabytes, g = gigabytes, t = terabytes');
  15.   writeln(' p = petabytes, c = exacto / -v, e = salir ');
  16.   readln(venus);
  17.  
  18.   case venus of
  19.  
  20.     { Convertir a kilobytes }
  21.  
  22.     'k' : begin
  23.             tierra := mercurio div 1024;
  24.             {pluton := tierra mod 1024;}
  25.             writeln('El resultado de convertir ',mercurio,' bytes a kilobytes es: ',tierra,'.');
  26.           end;
  27.  
  28.     { Convertir a megabytes }
  29.  
  30.     'm' : begin
  31.             marte := mercurio div 1048576;
  32.             {pluton := tierra mod 1024;}
  33.             writeln('El resultado de convertir ',mercurio,' bytes a megabytes es: ',marte,'.');
  34.           end;
  35.  
  36.     { Convertir a gigabytes }
  37.  
  38.     'g' : begin
  39.             jupiter := mercurio div 1073741824;
  40.             {pluton := tierra mod 1024;}
  41.             writeln('El resultado de convertir ',mercurio,' bytes a megabytes es: ',jupiter,'.');
  42.           end;
  43.  
  44.     { Convertir a terabytes }
  45.  
  46.     't' : begin
  47.             saturno := mercurio div 1099511627776;
  48.             {pluton := tierra mod 1024;}
  49.             writeln('El resultado de convertir ',mercurio,' bytes a terabytes es: ',saturno,'.');
  50.           end;
  51.  
  52.     { Convertir a petabytes }
  53.  
  54.     'p' : begin
  55.             urano := mercurio div 1125899906842624;
  56.             {pluton := tierra mod 1024;}
  57.             writeln('El resultado de convertir ',mercurio,' bytes a petabytes es: ',urano,'.');
  58.           end;
  59.  
  60.     { Conversion exacta / -v }
  61.  
  62.     'c' : begin
  63.  
  64.             { /*************************************************************************/ }
  65.                                 { Petabytes Root }
  66.             { /*************************************************************************/ }
  67.  
  68.             if (mercurio >= 1125899906842624) then
  69.             begin
  70.               neptunoP := mercurio div 1125899906842624;
  71.               neptunoMP := mercurio mod 1125899906842624;
  72.  
  73.               { Solo para verificar que no se ingreso una cantidad exacta de petabytes }
  74.               if (neptunoMP > 0) then
  75.               begin
  76.                 { Terabyte }
  77.                 if (neptunoMP >= 1099511627776) and (neptunoMP < 1125899906842624) then
  78.                 begin
  79.                   neptunoT := neptunoMP div 1099511627776;
  80.                   neptunoMT := neptunoMP mod 1099511627776;
  81.  
  82.                   { Gigabyte }
  83.                   if (neptunoMT >= 1073741824) then
  84.                   begin
  85.                     { codigo si el residuo es mayor que un terabyte }
  86.                     neptunoG := neptunoMT div 1073741824;               { 1073741024: Valor minimo para ser considerado terabytes }
  87.                     neptunoMG := neptunoMT mod 1073741824;
  88.  
  89.  
  90.                     { falta formula para saber si no es un terabyte exacto }
  91.                     { if MG > 0 then .. && empieza la formula siguiente de megabytes }
  92.  
  93.                     { Megabyte }
  94.                     if (neptunoMG > 1048576) then
  95.                     begin
  96.                       {codigo si el residuo es mayor que un gigabyte}
  97.                       neptunoM := neptunoMG div 1048576;
  98.                       neptunoMM := neptunoMG mod 1048576;
  99.  
  100.                       { Kilobyte }
  101.                       if (neptunoMM >= 1024) then
  102.                       begin
  103.                         {codigo si el residuo es mayor que un megabyte}
  104.                         neptunoK := neptunoMM div 1024;
  105.                         neptunoMK := neptunoMM mod 1024;
  106.  
  107.                         {  Byte }
  108.                         if (neptunoMK < 1024) then
  109.                         begin
  110.                           {codigo si el residuo es mayor que un kilobyte}
  111.                           neptunoB := neptunoMK;
  112.  
  113.                           {neptunoMB := neptunoMK mod 1024;}
  114.                         end;
  115.                       end
  116.  
  117.                       { Byte }
  118.                       else if (neptunoMM < 1024) then
  119.                       begin
  120.                         {codigo si el residuo es mayor que un kilobyte}
  121.                         neptunoB := neptunoMM;
  122.                         {neptunoMB := neptunoMM mod 1024;}
  123.                       End
  124.                     end
  125.  
  126.                     { Kilobyte }
  127.  
  128.                     else if (neptunoMG >= 1024) then
  129.                     begin
  130.                       {codigo si el residuo es mayor que un megabyte}
  131.                       neptunoK := neptunoMG div 1024;
  132.                       neptunoMK := neptunoMG mod 1024;
  133.  
  134.                       { Byte }
  135.  
  136.                       if (neptunoMK < 1024) then
  137.                       begin
  138.                         {codigo si el residuo es mayor que un kilobyte}
  139.                         neptunoB := neptunoMK;
  140.                       End;
  141.                     end
  142.  
  143.                     { Byte }
  144.                     else if (neptunoMG < 1024) then
  145.                     begin
  146.                       neptunoB := neptunoMG;
  147.                     end      
  148.                   end
  149.  
  150.                   { Megabyte }
  151.                   else if (neptunoMT > 1048576) then
  152.                   begin
  153.                     {codigo si el residuo es mayor que un gigabyte}
  154.                     neptunoM := neptunoMT div 1048576;
  155.                     neptunoMM := neptunoMT mod 1048576;
  156.  
  157.                     { Kilobyte }
  158.                     if (neptunoMM >= 1024) then
  159.                     begin
  160.                       {codigo si el residuo es mayor que un megabyte}
  161.                       neptunoK := neptunoMM div 1024;
  162.                       neptunoMK := neptunoMM mod 1024;
  163.  
  164.                       { Byte }
  165.                       if (neptunoMK < 1024) then
  166.                       begin
  167.                         {codigo si el residuo es mayor que un kilobyte}
  168.                         neptunoB := neptunoMK;
  169.                         {neptunoMB := neptunoMK mod 1024;}
  170.                       End;
  171.                     end
  172.  
  173.                     { Byte }
  174.                     else if (neptunoMM < 1024) then
  175.                     begin
  176.                       {codigo si el residuo es mayor que un kilobyte}
  177.                       neptunoB := neptunoMM;
  178.                       {neptunoMB := neptunoMM mod 1024;}
  179.                     end
  180.                   end
  181.  
  182.                   { Kilobyte }
  183.                   else if (neptunoMT > 1024) then
  184.                   begin
  185.                     {codigo si el residuo es mayor que un megabyte}
  186.                     neptunoK := neptunoMT div 1024;
  187.                     neptunoMK := neptunoMT mod 1024;
  188.  
  189.                     {  Byte }
  190.                     if (neptunoMK < 1024) then
  191.                     begin
  192.                       {codigo si el residuo es mayor que un kilobyte}
  193.                       neptunoB := neptunoMK;
  194.                       {neptunoMB := neptunoMK mod 1024;}
  195.                     end;
  196.                   end
  197.  
  198.                   { Byte }
  199.                   else if (neptunoMT < 1024) then
  200.                   begin
  201.                     {codigo si el residuo es mayor que un kilobyte}
  202.                     neptunoB := neptunoMT;
  203.                     {neptunoMB := neptunoMT mod 1024;}
  204.                   end
  205.                 end
  206.  
  207.                 { Gigabytes d Petabytes}
  208.                 else if (neptunoMP > 1073741824) then
  209.                 begin
  210.                   {codigo si el residuo es mayor que un Gigabyte}
  211.                   neptunoG := neptunoMP div 1073741824;
  212.                   neptunoMG := neptunoMP mod 1073741824;
  213.  
  214.                   { Megabyte }
  215.                   if (neptunoMG > 1048576) then
  216.                   begin
  217.                     {codigo si el residuo es mayor que un Megabyte}
  218.                     neptunoM := neptunoMG div 1048576;
  219.                     neptunoMM := neptunoMG mod 1048576;
  220.  
  221.                     { Kilobyte }
  222.                     if (neptunoMM >= 1024) then
  223.                     begin
  224.                       {codigo si el residuo es mayor que un Kilobyte}
  225.                       neptunoK := neptunoMM div 1024;
  226.                       neptunoMK := neptunoMM mod 1024;
  227.  
  228.                       {  Byte }
  229.                       if (neptunoMK < 1024) then
  230.                       begin
  231.                         {codigo si el residuo es mayor que un Byte}
  232.                         neptunoB := neptunoMK;
  233.                         {neptunoMB := neptunoMK mod 1024;}
  234.                       end;
  235.                     end
  236.  
  237.                     {  Byte }
  238.                     else if (neptunoMM < 1024) then
  239.                     {codigo si el residuo es mayor que un kilobyte}
  240.                     neptunoB := neptunoMM;
  241.                     {neptunoMB := neptunoMM mod 1024;}
  242.                   end
  243.                 end
  244.  
  245.                 { Kilobyte }
  246.                 else if (neptunoMG >= 1024) then
  247.                 begin
  248.                   {codigo si el residuo es mayor que un megabyte}
  249.                   neptunoK := neptunoMG div 1024;
  250.                   neptunoMK := neptunoMG mod 1024;
  251.  
  252.                   {  Byte }
  253.                   if (neptunoMK < 1024) then
  254.                   begin
  255.                     {codigo si el residuo es mayor que un kilobyte}
  256.                     neptunoB := neptunoMK;
  257.                     {neptunoMB := neptunoMK mod 1024;}
  258.                   end;
  259.                 end
  260.  
  261.                 { Byte }
  262.                 else if (neptunoMG < 1024) then
  263.                   {codigo si el residuo es mayor que un kilobyte}
  264.                   neptunoB := neptunoMG;
  265.                   {neptunoMB := neptunoMG mod 1024;}
  266.               end
  267.             end
  268.  
  269.             { Megabyte d Petabytes }
  270.             else if (neptunoMP > 1048576) then
  271.             begin
  272.               {codigo si el residuo es mayor que un gigabyte}
  273.               neptunoM := neptunoMP div 1048576;
  274.               neptunoMM := neptunoMP mod 1048576;
  275.  
  276.               { Kilobyte }
  277.               if (neptunoMM >= 1024) then
  278.               begin
  279.                 {codigo si el residuo es mayor que un megabyte}
  280.                 neptunoK := neptunoMM div 1024;
  281.                 neptunoMK := neptunoMM mod 1024;
  282.  
  283.                 {  Byte }
  284.                 if (neptunoMK < 1024) then
  285.                 begin
  286.                   {codigo si el residuo es mayor que un kilobyte}
  287.                   neptunoB := neptunoMK;
  288.                   {neptunoMB := neptunoMK mod 1024;}
  289.                 end;
  290.               end
  291.  
  292.               {  Byte }
  293.               else if (neptunoMM < 1024) then
  294.                 {codigo si el residuo es mayor que un kilobyte}
  295.                 neptunoB := neptunoMM;
  296.                 {neptunoMB := neptunoMM mod 1024;}
  297.             end
  298.     end        // end of case c
  299.  
  300.     { Kilobyte d Petabytes}
  301.     else if (neptunoMP > 1024) then
  302.     begin
  303.       {codigo si el residuo es mayor que un megabyte}
  304.       neptunoK := neptunoMP div 1024;
  305.       neptunoMK := neptunoMP mod 1024;
  306.  
  307.       { Byte }
  308.       if (neptunoMK < 1024) then
  309.       begin
  310.         neptunoB := neptunoMK;
  311.       End;
  312.     end
  313.  
  314.     { Byte d Petabytes }
  315.     else if (neptunoMP < 1024) then
  316.     begin
  317.       {codigo si el residuo es menor que un kilobyte}
  318.       neptunoB := neptunoMP
  319.       {neptunoMB := neptunoMP mod 1024;}
  320.     end
  321.   end       // end of case statment
  322.  
  323.   else if (mercurio <1) then
  324.     writeln('Ha digitado la cantidad de ',mercurio,' bytes, para un numero exacto de ',neptunoP,' Petabytes');
  325. end     // run out of indentation something wrong
  326.  
  327.         { /*************************************************************************/ }
  328.  
  329.         { aqui termina el universo de terabyte y comienza el surgimiento del reino de Terabyte }
  330.  
  331.         { /*************************************************************************/ }
  332.                                 { Terabyte Root }
  333.         { /*************************************************************************/ }
  334.  
  335.                         else if (mercurio >= 1099511627776) then
  336.                         begin
  337.                                 neptunoT := mercurio div 1099511627776;
  338.                                 neptunoMT := mercurio mod 1099511627776;
  339.  
  340.                                 { Solo para verificar que no se ingreso una cantidad exacta de Terabytes }
  341.                                 if (neptunoMT > 0) then
  342.                                 begin
  343.  
  344.                                         { Gigabyte }
  345.                                         if (neptunoMT > 1073741824) then
  346.                                         begin
  347.                                                 {codigo si el residuo es mayor que un terabyte}
  348.                                                 neptunoG := neptunoMT div 1073741824;
  349.                                                 neptunoMG := neptunoMT mod 1073741824;
  350.  
  351.                                                 { Megabyte }
  352.                                                 if (neptunoMG > 1048576) then
  353.                                                 begin
  354.                                                         {codigo si el residuo es mayor que un gigabyte}
  355.                                                         neptunoM := neptunoMG div 1048576;
  356.                                                         neptunoMM := neptunoMG mod 1048576;
  357.  
  358.                                                         { Kilobyte }
  359.                                                         if (neptunoMM >= 1024) then
  360.                                                         begin
  361.                                                                 {codigo si el residuo es mayor que un megabyte}
  362.                                                                 neptunoK := neptunoMM div 1024;
  363.                                                                 neptunoMK := neptunoMM mod 1024;
  364.  
  365.                                                                 {  Byte }
  366.                                                                 if (neptunoMK < 1024) then
  367.                                                                 begin
  368.                                                                         {codigo si el residuo es mayor que un kilobyte}
  369.                                                                         neptunoB := neptunoMK;
  370.                                                                         {neptunoMB := neptunoMK mod 1024;}
  371.                                                                 end;
  372.                                                         end
  373.  
  374.                                                         { Byte }
  375.                                                         else if (neptunoMM < 1024) then
  376.                                                         begin
  377.                                                                 {codigo si el residuo es mayor que un kilobyte}
  378.                                                                 neptunoB := neptunoMM;
  379.                                                                 {neptunoMB := neptunoMM mod 1024;}
  380.                                                         end;
  381.                                                 end
  382.  
  383.                                                 { Kilobyte }
  384.                                                 else if (neptunoMG >= 1024) then
  385.                                                 begin
  386.                                                         {codigo si el residuo es mayor que un megabyte}
  387.                                                         neptunoK := neptunoMG div 1024;
  388.                                                         neptunoMK := neptunoMG mod 1024;
  389.  
  390.                                                         {  Byte }
  391.                                                         if (neptunoMK < 1024) then
  392.                                                         begin
  393.                                                                 {codigo si el residuo es mayor que un kilobyte}
  394.                                                                 neptunoB := neptunoMK;
  395.                                                                 {neptunoMB := neptunoMK mod 1024;}
  396.                                                         end;
  397.                                                 end
  398.  
  399.                                                 {  Byte }
  400.                                                 else if (neptunoMG < 1024) then
  401.                                                         {codigo si el residuo es mayor que un kilobyte}
  402.                                                         neptunoB := neptunoMG;
  403.                                                         {neptunoMB := neptunoMG mod 1024;}
  404.                                                 end;
  405.                                         end
  406.  
  407.                                         { Megabyte }
  408.                                         else if (neptunoMT > 1048576) then
  409.                                         begin
  410.                                                 {codigo si el residuo es mayor que un gigabyte}
  411.                                                 neptunoM := neptunoMT div 1048576;
  412.                                                 neptunoMM := neptunoMT mod 1048576;
  413.  
  414.                                                 { Kilobyte }
  415.                                                 if (neptunoMM >= 1024) then
  416.                                                 begin
  417.                                                         {codigo si el residuo es mayor que un megabyte}
  418.                                                         neptunoK := neptunoMM div 1024;
  419.                                                         neptunoMK := neptunoMM mod 1024;
  420.  
  421.                                                         { Byte }
  422.                                                         if (neptunoMK < 1024) then
  423.                                                         begin
  424.                                                                 {codigo si el residuo es mayor que un kilobyte}
  425.                                                                 neptunoB := neptunoMK;
  426.                                                                 {neptunoMB := neptunoMK mod 1024;}
  427.                                                         end;
  428.                                                 end
  429.  
  430.                                                 {  Byte }
  431.                                                 else if (neptunoMM < 1024) then
  432.                                                 begin
  433.                                                         {codigo si el residuo es mayor que un kilobyte}
  434.                                                         neptunoB := neptunoMM;
  435.                                                         {neptunoMB := neptunoMM mod 1024;}
  436.                                                 end;
  437.                                         end
  438.  
  439.                                         { Kilobyte }
  440.                                         else if (neptunoMT > 1024) then
  441.                                         begin
  442.                                                 {codigo si el residuo es mayor que un megabyte}
  443.                                                 neptunoK := neptunoMT div 1024;
  444.                                                 neptunoMK := neptunoMT mod 1024;
  445.  
  446.                                                 {  Byte }
  447.                                                 if (neptunoMK < 1024) then
  448.                                                 begin
  449.                                                         {codigo si el residuo es mayor que un kilobyte}
  450.                                                         neptunoB := neptunoMK;
  451.                                                         {neptunoMB := neptunoMK mod 1024;}
  452.                                                 end;
  453.                                         end
  454.  
  455.                                         { Byte }
  456.                                         else if (neptunoMT < 1024) then
  457.                                         begin
  458.                                                 {codigo si el residuo es mayor que un kilobyte}
  459.                                                 neptunoB := neptunoMT;
  460.                                                 {neptunoMB := neptunoMT mod 1024;}
  461.                                         end;
  462.                                 end
  463.                                 else
  464.                                 begin
  465.                                         writeln('Ha digitado la cantidad de ',mercurio,' bytes, para un numero exacto de ',neptunoT,' Terabytes');
  466.                                 end;
  467.                         end
  468.  
  469.         { /*************************************************************************/ }
  470.                                 { Gigabytes Root }
  471.         { /*************************************************************************/ }
  472.  
  473.                         else if (mercurio > 1073741824) then
  474.                         begin
  475.                                 {codigo si el residuo es mayor que un terabyte}
  476.                                 neptunoG := mercurio div 1073741824;
  477.                                 neptunoMG := mercurio mod 1073741824;
  478.  
  479.                                 { Solo para verificar que no se ingreso una cantidad exacta de Gigabytes }
  480.                                 if (neptunoMG > 0) then
  481.                                 begin
  482.                                         { Megabyte }
  483.                                         if (neptunoMG > 1048576) then
  484.                                         begin
  485.                                                 {codigo si el residuo es mayor que un gigabyte}
  486.                                                 neptunoM := neptunoMG div 1048576;
  487.                                                 neptunoMM := neptunoMG mod 1048576;
  488.  
  489.                                                 { Kilobyte }
  490.                                                 if (neptunoMM >= 1024) then
  491.                                                 begin
  492.                                                         {codigo si el residuo es mayor que un megabyte}
  493.                                                         neptunoK := neptunoMM div 1024;
  494.                                                         neptunoMK := neptunoMM mod 1024;
  495.  
  496.                                                         {  Byte }
  497.                                                         if (neptunoMK < 1024) then
  498.                                                         begin
  499.                                                                 {codigo si el residuo es mayor que un kilobyte}
  500.                                                                 neptunoB := neptunoMK;
  501.                                                                 {neptunoMB := neptunoMK mod 1024;}
  502.                                                         end;
  503.                                                 end
  504.  
  505.                                                 { Byte }
  506.                                                 else if (neptunoMM < 1024) then
  507.                                                 begin
  508.                                                         {codigo si el residuo es mayor que un kilobyte}
  509.                                                         neptunoB := neptunoMM;
  510.                                                         {neptunoMB := neptunoMM mod 1024;}
  511.                                                 end;
  512.                                         end
  513.  
  514.                                         { Kilobyte }
  515.                                         else if (neptunoMG >= 1024) then
  516.                                         begin
  517.                                                 {codigo si el residuo es mayor que un megabyte}
  518.                                                 neptunoK := neptunoMG div 1024;
  519.                                                 neptunoMK := neptunoMG mod 1024;
  520.  
  521.                                                 {  Byte }
  522.                                                 if (neptunoMK < 1024) then
  523.                                                 begin
  524.                                                         {codigo si el residuo es mayor que un kilobyte}
  525.                                                         neptunoB := neptunoMK;
  526.                                                         {neptunoMB := neptunoMK mod 1024;}
  527.                                                 end;
  528.                                         end
  529.  
  530.                                         {  Byte }
  531.                                         else if (neptunoMG < 1024) then
  532.                                         begin
  533.                                                 {codigo si el residuo es mayor que un kilobyte}
  534.                                                 neptunoB := neptunoMG;
  535.                                                 {neptunoMB := neptunoMG mod 1024;}
  536.                                         end;
  537.                                 end
  538.                                 else
  539.                                 begin
  540.                                         writeln('Ha digitado la cantidad de ',mercurio,' bytes, para un numero exacto de ',neptunoT,' gIGAbytes');
  541.                                 end;
  542.                         end
  543.  
  544.         { /*************************************************************************/ }
  545.                                 { Megabyte Root }
  546.         { /*************************************************************************/ }
  547.  
  548.                         else if (mercurio > 1048576) then
  549.                         begin
  550.                                 {codigo si el residuo es mayor que un gigabyte}
  551.                                 neptunoM := mercurio div 1048576;
  552.                                 neptunoMM := mercurio mod 1048576;
  553.  
  554.                                 { Solo para verificar que no se ingreso una cantidad exacta de petabytes }
  555.                                 if (neptunoMP > 0) then
  556.                                 begin
  557.                                         { Kilobyte }
  558.                                         if (neptunoMM >= 1024) then
  559.                                         begin
  560.                                                 {codigo si el residuo es mayor que un megabyte}
  561.                                                 neptunoK := neptunoMM div 1024;
  562.                                                 neptunoMK := neptunoMM mod 1024;
  563.  
  564.                                                 {  Byte }
  565.                                                 if (neptunoMK < 1024) then
  566.                                                 begin
  567.                                                         {codigo si el residuo es mayor que un kilobyte}
  568.                                                         neptunoB := neptunoMK;
  569.                                                         {neptunoMB := neptunoMK mod 1024;}
  570.                                                 end;
  571.                                         end
  572.  
  573.                                         { Byte }
  574.                                         else if (neptunoMM < 1024) then
  575.                                         begin
  576.                                                 {codigo si el residuo es mayor que un kilobyte}
  577.                                                 neptunoB := neptunoMM;
  578.                                                 {neptunoMB := neptunoMM mod 1024;}
  579.                                         end;
  580.                                 end
  581.                                 else
  582.                                 begin
  583.                                         writeln('Ha digitado la cantidad de ',mercurio,' bytes, para un numero exacto de ',neptunoT,' Gigabytes');
  584.                                 end;
  585.                         end
  586.  
  587.         { /*************************************************************************/ }
  588.                         { Kilobyte Root }
  589.         { /*************************************************************************/ }
  590.  
  591.                         else if (mercurio >= 1024) then
  592.                         begin
  593.                                 {codigo si el residuo es mayor que un megabyte}
  594.                                 neptunoK := mercurio div 1024;
  595.                                 neptunoMK := mercurio mod 1024;
  596.  
  597.                                 {  Byte }
  598.                                 if (neptunoMK < 1024) then
  599.                                 begin
  600.                                         {codigo si el residuo es mayor que un kilobyte}
  601.                                         neptunoB := neptunoMK;
  602.                                         {neptunoMB := neptunoMK mod 1024;}
  603.                                 end;
  604.                         end
  605.  
  606.         { /*************************************************************************/ }
  607.                         { Byte Root }
  608.         { /*************************************************************************/ }
  609.  
  610.                         else if (mercurio < 1024) then
  611.                         begin
  612.                                 {codigo si el residuo es mayor que un kilobyte}
  613.                                 neptunoB := mercurio;
  614.                                 {neptunoMB := mercurio mod 1024;}
  615.                         end
  616.                 {/*******************************************************/}
  617.  
  618.  
  619.                         { Rara ocasion en la que ingresen una cantidad exacta de bytes de un petabyte }
  620.                         else
  621.                         begin
  622.                                 writeln('La conversion de ',mercurio,' bytes es: ',neptunoP,' petabytes exactos.');
  623.                         end;
  624.                 end:
  625.  
  626.                 'e' : begin
  627.                         writeln('Hasta luego!')
  628.                 end;
  629.         end;
  630.  
  631.         writeln('END_OF_FILE__');
  632. end.
  633.  
  634.  
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

NightWing

  • Newbie
  • Posts: 5
Re: i don't know why
« Reply #3 on: May 08, 2018, 03:40:21 pm »
Forget to write the question, in the code use several if nested with else if

How does the ' ; ' law apply in several nested if-else-if ?, nowhere in the wiki did I find an example with more than one 'else if' and in browsers they show the same simple examples

What would be the correct way to place the ';' at the end of each condition?

ie:
Code: Pascal  [Select][+][-]
  1. begin
  2. if (x > y) then
  3.     begin
  4.         write ('X is bigger');
  5.         z := x+y;
  6.         if (z > x) then
  7.             begin
  8.                 writeln('Z is bigger');
  9.             end
  10.         else
  11.             begin
  12.                 writeln('X still bigger');
  13.             end;
  14.     end
  15. else if (x > y) then
  16.     begin
  17.          writeln ('Y is bigger');
  18.      end
  19. else if (x == y) then
  20.     begin
  21.          writeln ('x & y are the same');
  22.      end
  23. else
  24.     begin
  25.         writeln('something wrong happend');
  26.     end;
  27. end.
  28.  

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: i don't know why
« Reply #4 on: May 08, 2018, 04:03:03 pm »
Rules:
- There must be no ; symbol before else
- The total count of begin must be equals to the total count of end

I haven't tested your code. I could be wrong but my eye-inspection told me you forgot a begin for the if clause on the line #290. See the image below:

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: i don't know why
« Reply #5 on: May 08, 2018, 04:07:45 pm »
Hi

You may find it easier if you block your if then ...... else .... with begin end; it makes it easier to follow ie

Code: Pascal  [Select][+][-]
  1. if {Condition A} then
  2. begin
  3.   if {Condition B} then
  4.   begin
  5.     .....
  6.   end   // no ; because else is following
  7.   else
  8.   begin   {Else Section of Condition B}
  9.     if {Condition C} then
  10.     begin
  11.       .....
  12.       if {Condition D} then
  13.       begin
  14.         .......
  15.         if ??????? then
  16.         begin
  17.           ......
  18.         end
  19.         else
  20.         begin
  21.           if ?????????? then
  22.           begin
  23.             ......
  24.           end
  25.           else
  26.           begin
  27.             ......
  28.           end;
  29.         end;
  30.       end   // no ; else following
  31.       else {Else Section of Condition D}
  32.       begin
  33.         if ????????? then
  34.         begin
  35.           ....
  36.         end
  37.         else
  38.         begin
  39.           if ?????????? then
  40.           begin
  41.             .....
  42.           end
  43.           else
  44.           begin
  45.             ......
  46.           end;
  47.         end;
  48.       end; {End Section of Condition D}
  49.     end; {End Section of Condition C}
  50.   end; {End Section of Condition B}
  51. end; {End of Condition A}        
  52.  

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3. uses
  4.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  5.   cthreads,
  6.   {$ENDIF}{$ENDIF}
  7.   Classes
  8.   { you can add units after this };
  9.  
  10. var x:integer=2;
  11.     y:integer=4;
  12.     z:integer=0;
  13.  
  14. begin
  15.   if (x > y) then
  16.   begin
  17.     write ('X is bigger');
  18.     z := x+y;
  19.     if (z > x) then
  20.     begin
  21.       writeln('Z is bigger');
  22.     end
  23.     else
  24.     begin
  25.       writeln('X still bigger');
  26.     end;
  27.   end      // no ; this allows an else statement ie if then else;
  28.   else // i think you want the whole next section executed in th else block; so try using a begin end block
  29.   begin // added
  30.     if (x > y) then
  31.     begin
  32.       writeln ('Y is bigger');
  33.     end
  34.     else
  35.     begin // added
  36.       if (x=y) then  // changed from == to =
  37.       begin
  38.         writeln ('x & y are the same');
  39.       end
  40.       else
  41.       begin
  42.         writeln('something wrong happend');
  43.       end;
  44.     end; // added
  45.   end; // end of else section so ;
  46. end.
  47.  
« Last Edit: May 08, 2018, 05:08:08 pm by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: i don't know why
« Reply #6 on: May 08, 2018, 04:19:50 pm »
Or use colored outline of source structure feature, which is already available since Lazarus 1.8.0. Read more:
http://wiki.freepascal.org/Lazarus_IDE_Tools#Outline

But I personally will try to rewrite the code if the if clause has more than 3 levels, maybe try other logics or move some parts of them to a local nested procedure.
« Last Edit: May 08, 2018, 04:32:07 pm by Handoko »

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: i don't know why
« Reply #7 on: May 08, 2018, 04:58:10 pm »
This line with "==" will surely fail to compile:
Code: Pascal  [Select][+][-]
  1. else if (x == y) then
  2.  

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: i don't know why
« Reply #8 on: May 10, 2018, 11:13:46 am »
Why are you engaging him*her in such a bad design? Nested if-then-elses starting at a level of, prrprprp, four are getting ridiculous. And error-prone, as the OP's question proves.
Yours Sincerely
Kai Burghardt

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: i don't know why
« Reply #9 on: May 10, 2018, 01:51:09 pm »
This code seems to calculate how much Peta/Giga/Mega/Kilo-bytes a certain amount of bytes is?

The names of the variables confuse me: they seem to be the names of planets in the solar system?

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: i don't know why
« Reply #10 on: May 10, 2018, 02:23:15 pm »
This may get you started?

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}
  3. {$h+}
  4. uses
  5.   SysUtils;
  6.  
  7. const
  8.   Kilo = 1024;
  9.   Mega = 1024 * Kilo;
  10.   Tera = 1024 * UInt64(Mega);
  11.   Peta = 1024 * UInt64(Tera);
  12.  
  13. procedure ConvertBytesToUnits(Amount: UInt64; out PetaBytes, TeraBytes, MegaBytes, KiloBytes, Bytes: UInt64);
  14. begin
  15.   PetaBytes := Amount div Peta;
  16.   Amount := Amount mod Peta;
  17.   TeraBytes := Amount div Tera;
  18.   Amount := Amount mod Tera;
  19.   MegaBytes := Amount div Mega;
  20.   Amount := Amount mod Mega;
  21.   KiloBytes := Amount div Kilo;
  22.   Bytes := Amount mod Kilo;
  23. end;
  24.  
  25. var
  26.   Count, PBytes, TBytes, MBytes, KBytes, Bytes: UInt64;
  27. begin
  28.   Count := Peta + 2*Tera + 3*Mega + 4*Kilo + 5;
  29.   ConvertBytesToUnits(Count, PBytes, TBytes, MBytes, KBytes, Bytes);
  30.   writeln(format('%u Bytes equals: %u PetaBytes, %u TeraBytes, %u MegaBytes, %u KiloBytes and %u Bytes',[Count,PBytes, TBytes, MBytes, KBytes, Bytes]));
  31. end.

Ouputs:
Code: [Select]
1101662261253 Bytes equals: 1 PetaBytes, 2 TeraBytes, 3 MegaBytes, 4 KiloBytes and 5 Bytes

Bart

af0815

  • Hero Member
  • *****
  • Posts: 1289
Re: i don't know why
« Reply #11 on: May 10, 2018, 03:03:47 pm »
Hehe, if the guy use this without understanding, he will have an unwanted result :-)
regards
Andreas

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: i don't know why
« Reply #12 on: May 10, 2018, 04:38:43 pm »
Ah, Bart, some sense at last. Rewrite from scratch.

Code such as NightWing was given is a total disaster.
No beginner should be presented with such useless code as that. There is no point formatting it correctly, or improving it or debugging it. Should be binned without hesitation. Multiple nested levels of conditional and case statements, myriads of global variables with insane names, use of crt unit ... Yuk! No wonder Pascal has a bad name in some quarters.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: i don't know why
« Reply #13 on: May 10, 2018, 04:50:48 pm »
We cannot expect beginners to write code like a professional, because he's just a beginner.

Binned away and present him with new code, without telling him what he did wrong, is not very helpful. He was learning about how to properly use if-then-else but showing him to use mod and div, is it helpful?
« Last Edit: May 10, 2018, 04:54:32 pm by Handoko »

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: i don't know why
« Reply #14 on: May 10, 2018, 07:24:53 pm »
Hehe, if the guy use this without understanding, he will have an unwanted result :-)

Care to explain?

Binned away and present him with new code, without telling him what he did wrong, is not very helpful. He was learning about how to properly use if-then-else but showing him to use mod and div, is it helpful?

My piece of code was meant as a starting point for the "exact" case.
So, from my POV it was meant to be helpfull.

But if you think otherwise, please remind me not try to be of help to anyone on this forum anymore.

The non-exact cases are trivial, just divide by the proper base (Peta, Tera etc) to get, say 2090 bytes ~= 2 KB.
(Or use floating point arithmatic to get, say 2561 bytes~= 2.5 KB, whatever you like).

Using tons of if..then..else inside a case statement is just bad design. As others have pointed out, so no need to repeat that argument.
Using magic numbers as constants is bad design.
Using a block of code that is >> 1 screen probably is bad design.
Using variable names that make no sense to the meaning of the code is a WTF in it's own right.

Anyhow: we did not hear from TS again, so it's safe to say this was yet another homework assignment, and now his deadline has past.

Bart

 

TinyPortal © 2005-2018