Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

HELP!!!!!!!!!!!!!!Counting Var / Number

stevengreen22stevengreen22 Posts: 4Member
Hi,

I've been struggling with this for hours now.
Can someone please help / point me in the right direction.

It was set out as a boolean excercise but I've amended it a little by using procedures instead, this is minor as I shoudl jsut be able to revert it, I was trying to eliminate the global variables.

[code]
procedure AskNumber;
var numcheck:integer;
begin
writeln('What number/s would you like to check?');
readln(numcheck);
posi:=0; negi:=0; zero:=0;

if numcheck>0 then writeln('This is a positive number');
readln(posi);posit:=posi; {read as last number input, not counting}
if numcheck<0 then write('This is a negative number');
// read(negi); negit:=negit+1;
if numcheck=0 then write('This number is Zero');
// read(zero);zerot:=zero+1;

end;

procedure AskTotal;

var count,totnum:integer;
begin
writeln('How many numbers will you like to check? ');
readln(totnum);

for count:= 1 to totnum do AskNumber //or 0 to totnum-1
end;

begin {main program}
AskTotal;

writeln(posit,negit,zerot);
{up to 33750}
end.
[/code]

I need the program to decided whether a number is positive etc, this works fine. The part I'm stuck on is asking it to 'count' the number of pos/neg etc and to then display that number at the end.

The read line that i have is reading the number as an integer and not as a (+1) to be added. I've played arounfg with it so much but can't fget the fking thing to work.
Help!

Comments

  • quikcarlquikcarl Posts: 59Member
    I've borrowed your code and made some alterations to get what I think you want.
    [code][color=Green] var
    posit,negit,zerot : integer;[/color]
    procedure AskNumber;
    var numcheck:integer;
    begin
    writeln('What number/s would you like to check?');
    readln(numcheck);
    [color=Red](* posi:=0; negi:=0; zero:=0; *)[/color]
    [color=Green]if numcheck>0 then begin
    writeln('This is a positive number');
    posit := posit + 1
    end;[/color](* readln(posi);posit:=posi; {read as last number input, not counting} *)
    [color=Green]if numcheck<0 then begin
    write('This is a negative number');
    negit := negit + 1
    end;[/color] // read(negi); negit:=negit+1;
    [color=Green]if numcheck=0 then begin
    write('This number is Zero');
    zerot := zerot + 1
    end[/color] // read(zero);zerot:=zero+1;

    end;

    procedure AskTotal;

    var count,totnum:integer;
    begin
    writeln('How many numbers will you like to check? ');
    readln(totnum);

    for count:= 1 to totnum do AskNumber //or 0 to totnum-1
    end;

    begin {main program}
    [color=Green] posit := 0;
    negit := 0;
    zerot := 0;[/color]
    AskTotal;

    writeln(posit,negit,zerot);
    {up to 33750}
    end.[/code]
  • DaiMitnickDaiMitnick Posts: 77Member
    EDIT: Ignore post, seems Carl addressed these issues in the time I was away from my PC.




    Haven't done Pascal in a few years so forgive my ignorance. First, is this line:

    readln(posi);posit:=posi;

    actually part of the if statement? To me it looks like it will be run whether the number is positive or not. Whether it is or isn't it looks like you're setting it to 0 each time, because of this line:

    posi:=0; negi:=0; zero:=0;

    As I said, I'm rusty so perhaps I'm wrong. Also, where are posit, zerot and negit declared, because if they are not global then (I believe) they will go out of scope each time you call the procedure and so it will not return any values to your main procedure.

    Regards, Dai


    ------------------------------------------
    Do or do not, there is no try. |
    ------------------------------------------
Sign In or Register to comment.