Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Please help a confused newbie

Rodimus PrimeRodimus Prime Member Posts: 51
Hi,

Iv'e just started with Pascal and I'm doing some easy simple programs. Iv'e got some C++ experience, so for this problem I decided to use if/then/else in a way that seems logical to me.
Only thing is, when I type a number larger than 32767, the program doesn't produce the correct output.
Can somebody plz help me out?

[code]
begin

Write('Enter an integer: ');
ReadLn(InputInteger);

WriteLn;
clrscr;

begin
if InputInteger <= 32767 then WriteLn('Integer is: ', InputInteger);

if InputInteger > 32767 then WriteLn('Invalid integer!, try again');

ReadLn;
end
end.
[/code]

Comments

  • zibadianzibadian Member Posts: 6,349
    : Hi,
    :
    : Iv'e just started with Pascal and I'm doing some easy simple programs. Iv'e got some C++ experience, so for this problem I decided to use if/then/else in a way that seems logical to me.
    : Only thing is, when I type a number larger than 32767, the program doesn't produce the correct output.
    : Can somebody plz help me out?
    :
    : [code]
    : begin
    :
    : Write('Enter an integer: ');
    : ReadLn(InputInteger);
    :
    : WriteLn;
    : clrscr;
    :
    : begin
    : if InputInteger <= 32767 then WriteLn('Integer is: ', InputInteger);
    :
    : if InputInteger > 32767 then WriteLn('Invalid integer!, try again');
    :
    : ReadLn;
    : end
    : end.
    : [/code]
    :
    Did you type-cast the InputInteger as integer or as a word. In TP the standard integer is a 16bit signed integer. This would give it a maximum of 32767 and give you probably an overflow into the sign bit.
  • Rodimus PrimeRodimus Prime Member Posts: 51
    : : Hi,
    : :
    : : Iv'e just started with Pascal and I'm doing some easy simple programs. Iv'e got some C++ experience, so for this problem I decided to use if/then/else in a way that seems logical to me.
    : : Only thing is, when I type a number larger than 32767, the program doesn't produce the correct output.
    : : Can somebody plz help me out?
    : :
    : : [code]
    : : begin
    : :
    : : Write('Enter an integer: ');
    : : ReadLn(InputInteger);
    : :
    : : WriteLn;
    : : clrscr;
    : :
    : : begin
    : : if InputInteger <= 32767 then WriteLn('Integer is: ', InputInteger);
    : :
    : : if InputInteger > 32767 then WriteLn('Invalid integer!, try again');
    : :
    : : ReadLn;
    : : end
    : : end.
    : : [/code]
    : :
    : Did you type-cast the InputInteger as integer or as a word. In TP the standard integer is a 16bit signed integer. This would give it a maximum of 32767 and give you probably an overflow into the sign bit.
    :

    Hi,

    thanks for your help. Iv'e used InputInteger as type integer. When I enter 123456 say for input, it doesn't print the invalid value message, but it gives me a value like -8765 or something. Maybe you tried to tell me this, so should I change InputInteger to another type or something?

    Thanks
  • zibadianzibadian Member Posts: 6,349
    : : : Hi,
    : : :
    : : : Iv'e just started with Pascal and I'm doing some easy simple programs. Iv'e got some C++ experience, so for this problem I decided to use if/then/else in a way that seems logical to me.
    : : : Only thing is, when I type a number larger than 32767, the program doesn't produce the correct output.
    : : : Can somebody plz help me out?
    : : :
    : : : [code]
    : : : begin
    : : :
    : : : Write('Enter an integer: ');
    : : : ReadLn(InputInteger);
    : : :
    : : : WriteLn;
    : : : clrscr;
    : : :
    : : : begin
    : : : if InputInteger <= 32767 then WriteLn('Integer is: ', InputInteger);
    : : :
    : : : if InputInteger > 32767 then WriteLn('Invalid integer!, try again');
    : : :
    : : : ReadLn;
    : : : end
    : : : end.
    : : : [/code]
    : : :
    : : Did you type-cast the InputInteger as integer or as a word. In TP the standard integer is a 16bit signed integer. This would give it a maximum of 32767 and give you probably an overflow into the sign bit.
    : :
    :
    : Hi,
    :
    : thanks for your help. Iv'e used InputInteger as type integer. When I enter 123456 say for input, it doesn't print the invalid value message, but it gives me a value like -8765 or something. Maybe you tried to tell me this, so should I change InputInteger to another type or something?
    :
    : Thanks
    :
    I tried to tell you that. In Pascal every number variable type (integer or real) has a minimum and a maximum value, based on the memory it occupies. I memory serves me well, you should use the LongInt type (32bits signed integer) for those kinds of numbers. I don't have TP here, but the maximum ranges for integers are in the help files.
  • adibicheaadibichea Member Posts: 46
    [b][red]This message was edited by adibichea at 2004-3-11 12:49:0[/red][/b][hr]
    if you realy want to see if number is betwin -32768..32767 you could try this:
    [code]
    var
    s: string;
    r: real;
    c: integer;
    begin
    readln(s);
    if s <> '' then begin
    val(s,r,c);
    if c = 0 then begin
    if (r>=-32768)and(r<=32767) then writeln('Number Integer')
    else writeln('Number Not Integer');
    end
    else
    writeln('Is not a number! Please input a number :)');
    end;
    end.
    [code]


    good luck!


Sign In or Register to comment.