Hey people! Ive had to right a simulation to the game buzz-fizz..this is what the game is... One person starts of by calling out '1' then each player adds one to the previous players number and calls out that number unless it divides excatly by three or five.. If it divides by three he calls out "Buzz" and if it divides by five he calls out "fizz" instead of the number. If it divides by both three and five he calls out "buzz-fizz".

Now then the problem i'm having is the formatting of it!

This is my code for the game:

program BuzzFizz (input,output);


uses
crt;


var
number : integer;


begin {main program};
Clrscr;
for number := 1 to 100 do
begin


if (number mod 5 = 0) and (number mod 3 = 0 )
then begin
writeln (' ' , "Buzz - Fizz!", ' ')
end
else begin
if number mod 3 = 0
then begin
writeln (' ' , "Buzz!", ' ')
end;
if number mod 5 = 0
then begin
writeln (' ' , "Fizz!", ' ')
end;


if (number mod 5 <> 0) and (number mod 3 <> 0)
then begin
write (' ' , number:10, ' ');
end;
end;
end;
end.


Can someone please help me so that its readable on the screen and set out nicely..dont really have a clue about formatting

Dani AI

Generated

— The easiest way to get tidy, readable output is to make each loop iteration produce exactly one printable item (either the number, "Buzz", "Fizz" or "Buzz-Fizz") and then print that item into a fixed-width field so columns line up. The original code runs separate tests and can print more than once per number; building a single output string per number avoids that. Also, as noted, Pascal string literals use single quotes.

program BuzzFizz;
uses crt;
var
  n, col: integer;
  outStr: string;
begin
  clrscr;          { optional }
  col := 0;
  for n := 1 to 100 do
  begin
    outStr := '';
    if (n mod 3 = 0) then outStr := outStr + 'Buzz';
    if (n mod 5 = 0) then
    begin
      if outStr <> '' then outStr := outStr + '-';
      outStr := outStr + 'Fizz';
    end;

    if outStr <> '' then
      write(outStr:9)   { fixed column for words }
    else
      write(n:4);       { fixed column for numbers }

    inc(col);
    if col = 10 then
    begin
      writeln;          { new row after 10 columns }
      col := 0;
    end
    else
      write(' ');       { gap between columns }
  end;
  writeln;
end.

Notes and quick tips:

  • Building outStr ensures a single print per number and yields the "Buzz-Fizz" hyphenation cleanly.
  • Adjust the numeric and text widths (4 and 9 above) to fit your console; smaller widths pack more columns.
  • If your compiler does not have CRT, remove uses crt and clrscr.
  • For stepwise debugging, reduce the loop to 1..20 and replace the column logic with writeln(...) to see one item per line.

First, strings in Pascal are delimited with single quotes ('), not double quotes (").

Second, you can consolidate your writelns into one string
from
writeln(' ', 'Buzz', ' ');

to
writeln(' Buzz ');

Finally, if you change the final write to writeln, change the width specifier to 5, change 'Buzz - Fizz' to 'Buzz-Fizz', it comes out looking a lot better.

HTH

Daaave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.