## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14004

This Forum Only

HELP!!!!!!!!!!!!!!Counting Var / Number Posted by stevengreen22 on 22 Mar 2011 at 9:24 AM
Hi,

I've been struggling with this for hours now.

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.

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

if  numcheck>0 then writeln('This is a positive number');
if  numcheck<0 then write('This is a negative number');
if  numcheck=0 then write('This number is Zero');

end;

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

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

begin {main program}

writeln(posit,negit,zerot);
{up to 33750}
end.

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!

Re: HELP!!!!!!!!!!!!!!Counting Var / Number Posted by quikcarl on 22 Mar 2011 at 11:04 AM
I've borrowed your code and made some alterations to get what I think you want.
var
posit,negit,zerot : integer;
var numcheck:integer;
begin
writeln('What number/s would you like to check?');
(*        posi:=0; negi:=0; zero:=0; *)
if  numcheck>0 then begin
writeln('This is a positive number');
posit := posit + 1
if  numcheck<0 then begin
write('This is a negative number');
negit := negit + 1
if  numcheck=0 then begin
write('This number is Zero');
zerot := zerot + 1

end;

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

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

begin {main program}
posit := 0;
negit := 0;
zerot := 0;

writeln(posit,negit,zerot);
{up to 33750}
end.

Re: HELP!!!!!!!!!!!!!!Counting Var / Number Posted by DaiMitnick on 22 Mar 2011 at 11:19 AM
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:

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. |
------------------------------------------

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic