Forum > Beginners

simple problem: count digits of number

(1/9) > >>

chickpea:
Hi there,

Here's my approach to counting the digits of a number in pascal.
It does work, but as soon as I enter a number with more than 5 digits the program returns a 5.
I don't get why. If anyone could give me a hint that'd be great.
Comments and program names in German, but I suppose you get the idea.

program ZifferAnzahl (input, output);

type
tNatZahl = 0..maxint;

var
inZahl : tNatZahl;

function countZiff (inZahl : tNatZahl) : tNatZahl;

{ gibt Anzahl der Ziffern von inZahl zurÃ¼ck }

var
i,
j : tNatZahl;

begin

{ initialisierung }

i := inZahl;
j := 0;

{ inZahl so oft durch 10 Teilen, bis 0 erreicht wird }

while i > 0 do
begin
i := i div 10;
j := j + 1
end;

countZiff := j
end; { countZiff }

begin
writeln ('Bitte Zahl eingeben');
writeln(countZiff(inZahl))
end. { ZifferAnzahl }

rvk:
You are probably in TP mode. {\$mode TP}
There maxint is a 2 byte integer.
Which is maximum 32767 (see, 5 digits)

Add {\$mode objfpc} just below program and your Integer will be 4 bytes.

B.T.W. you can check this by adding the lines

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---writeln(sizeof(inZahl));writeln(maxint);
Of course you can also do this instead of changing mode:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  tNatZahl = 0..99999999;

chickpea:
Great, that was the error indeed. Thanks for explaining.

Working correctly when change to

tNatZahl = 0..999999999;

Cheers

Bart:
Perhaps: tNatZahl = QWord;

Another dummy approach  O:-)

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses sysutils; function countZiff (inZahl : tNatZahl) : tNatZahl;begin  Result := Length(IntToStr(inZahl));end;
or

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Math; function countZiff (inZahl : tNatZahl) : tNatZahl;begin  Result := Floor(Log10(inZahl));end;
Bart

furious programming:
@chickpea: you don't need a custom function, just use some helpers from SysUtils unit:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Count := MyIntVar.ToString.Length;

[#] Next page

Go to full version