PDA

View Full Version : Help with simple program



Fabiano
20-07-2011, 01:12 AM
my English is poor but that's okay ... :)

I'm doing a free introductory course to the Pascal

I need to do a program that I type the time in seconds and it shows in days, hours, minutes and seconds but this happening the following error, some results appear negative

this is the code



program tempo;
uses crt;

var

aux, aux1, aux2, s, m, h, d : integer;
t : longint;

begin
clrscr;
writeln('******* Digite o tempo em segundos *******');
readln(t);

d := t div (86400);
aux := t mod (86400);
h := aux div (3600);
aux1 := aux mod (3600);
m := aux1 div 60;
aux2 := aux1 mod 60;
s := aux2;

writeln('Dias ', d);
writeln('Horas', h);
writeln('Minutos ', m);
writeln('Segundos ', s);
readln;
end.

for example if I type 150000

Dias = 1
Horas = 0
Minutos = -32
Segundos = -16

thanks

User137
20-07-2011, 01:48 PM
What compiler are you using? By quick test it seems you need to declare everything as longint, or type convert them when using with variable t.

By quick test with Lazarus i can get that happen only if i do this:
aux, aux1, aux2, s, m, h, d : smallint;

So with your compiler, integer = smallint which doesn't understand values over 32767.