Help. Counting problem 
Author Message
 Help. Counting problem

Hello there.  I'm taking my first programming class, and am practicing
writing programming for my wife.  I have this program, but I can't
figure out why my counter isn't working.  Could someone please help?

P.S.  It compiles allright, but just executes an endless loop.

PROGRAM LOVEBUG(INPUT,OUTPUT);

VAR WACKO,
    COUNT : INTEGER;

BEGIN

WRITE('YOU WILL NEED TO ENTER A NUMBER BETWEEN 1-10:');
READLN(WACKO);

COUNT := 0;

BEGIN

IF WACKO <= 3 THEN
        WHILE COUNT < 100 DO
        WRITE('I LOVE YOU BEE!');
             IF WACKO <= 6 THEN
                WHILE COUNT < 100 DO
                WRITE('YOU ARE MY FAVORITE!');
                  IF WACKO <= 10 THEN
                    WHILE COUNT < 100 DO
                    WRITE('KISS ME NOW LOVE!')
                        ELSE WRITE('WRONG NUMBER WIENER!  TRY
AGAIN!');

COUNT := COUNT + 1

END;

END.
                                -Gregg Black RAIN Tech Support



Wed, 18 Jun 1902 08:00:00 GMT  
 Help. Counting problem

Quote:

> ...
> P.S.  It compiles allright, but just executes an endless loop.

Hello Gregg.

Indenting and using begin...end pairs help a lot to see what's
happening.

Quote:
> PROGRAM LOVEBUG(INPUT,OUTPUT);
> VAR WACKO,
>     COUNT : INTEGER;
> BEGIN
>   WRITE('YOU WILL NEED TO ENTER A NUMBER BETWEEN 1-10:');
>   READLN(WACKO);
>   COUNT := 0;
>   BEGIN
>     IF WACKO <= 3 THEN begin
>       WHILE COUNT < 100 DO begin
>         WRITE('I LOVE YOU BEE!');
        end;
>       IF WACKO <= 6 THEN begin
>         WHILE COUNT < 100 DO begin
>           WRITE('YOU ARE MY FAVORITE!');
          end;
>         IF WACKO <= 10 THEN begin
>           WHILE COUNT < 100 DO begin
>             WRITE('KISS ME NOW LOVE!');

            end;
          end

Quote:
>         ELSE WRITE('WRONG NUMBER WIENER!  TRY AGAIN!');
        end;
>       COUNT := COUNT + 1;
      end;
>   END;
> END.

As you can see, the count:=count+1 statement is OUT of any of the loops.
I have added begin..end pairs in lowercase to help you see how does
the compiler understand your code.

--
    "La esperanza es lo ultimo que se pierde. Y es un pena,
    porque si se perdiera lo primero, quiza se hiciera algo
    para solucionar las cosas." - J. Perich



Wed, 18 Jun 1902 08:00:00 GMT  
 Help. Counting problem

Quote:

> Hello there.  I'm taking my first programming class, and am practicing
> writing programming for my wife.  I have this program, but I can't
> figure out why my counter isn't working.  Could someone please help?

> P.S.  It compiles allright, but just executes an endless loop.

> <program snipped>
>                                 -Gregg Black RAIN Tech Support

  Your program is incrimenting COUNT outside of the loop. If I understand
your program correctly you should do this...

PROGRAM LOVEBUG(INPUT,OUTPUT);
VAR WACKO,
    COUNT : INTEGER;
BEGIN
  WRITE('YOU WILL NEED TO ENTER A NUMBER BETWEEN 1-10:');
  READLN(WACKO);
  COUNT := 0;
  BEGIN
    IF WACKO <= 3 THEN
      WHILE COUNT < 100 DO
---->   BEGIN
         WRITE('I LOVE YOU BEE!');
         IF WACKO <= 6 THEN
           WHILE COUNT < 100 DO WRITE('YOU ARE MY FAVORITE!');
         IF WACKO <= 10 THEN
           WHILE COUNT < 100 DO WRITE('KISS ME NOW LOVE!')
             ELSE WRITE('WRONG NUMBER WIENER!  TRY AGAIN!');
         COUNT := COUNT + 1
---->   END;
  END;



Wed, 18 Jun 1902 08:00:00 GMT  
 Help. Counting problem

Quote:

> Hello there.  I'm taking my first programming class, and am practicing
> writing programming for my wife.  I have this program, but I can't
> figure out why my counter isn't working.  Could someone please help?

> P.S.  It compiles allright, but just executes an endless loop.

> PROGRAM LOVEBUG(INPUT,OUTPUT);

> VAR WACKO,
>     COUNT : INTEGER;

> BEGIN

> WRITE('YOU WILL NEED TO ENTER A NUMBER BETWEEN 1-10:');
> READLN(WACKO);

> COUNT := 0;

> BEGIN

> IF WACKO <= 3 THEN
>         WHILE COUNT < 100 DO
>         WRITE('I LOVE YOU BEE!');
>              IF WACKO <= 6 THEN
>                 WHILE COUNT < 100 DO
>                 WRITE('YOU ARE MY FAVORITE!');
>                   IF WACKO <= 10 THEN
>                     WHILE COUNT < 100 DO
>                     WRITE('KISS ME NOW LOVE!')
>                         ELSE WRITE('WRONG NUMBER WIENER!  TRY
> AGAIN!');

> COUNT := COUNT + 1

> END;

> END.
>                                 -Gregg Black RAIN Tech Support

As has been mentioned already - you must have a LOOP and you must be
incrementing your counter inside the LOOP.

PROGRAM LOVEBUG(INPUT,OUTPUT);

  VAR WACKO,
      COUNT : INTEGER;

  BEGIN

   WRITE('YOU WILL NEED TO ENTER A NUMBER BETWEEN 1-10:');
   READLN(WACKO);

   COUNT := 0;

     if (WACKO>10) then WRITE('WRONG NUMBER WIENER!  TRY  AGAIN!')   {
Test for wrong value }
     else begin                                                      {
Proceed of OK }

       While (COUNT<100) do begin                                    {
Begin While LOOP }
         IF WACKO < 4 THEN WRITE('I LOVE YOU BEE!')                  {
Test for each good value }
           ELSE IF WACKO < 7 THEN WRITE('YOU ARE MY FAVORITE!')      {  
\\   }
             ELSE IF WACKO < 11 THEN WRITE('KISS ME NOW LOVE!') ;  
{    \\  }
       COUNT := COUNT + 1 ; { or inc(COUNT) ; }                      {
Increment COUNTER here }
       end ; { While }                                               {
End While Loop }

     end ; { if/else }                                               {
End if/else }
  END. { LOVEBUG }                                                  

--
Val Mehling - anti-spam in effect.
For e-mail reply to:



Wed, 18 Jun 1902 08:00:00 GMT  
 Help. Counting problem

Your inner loops never terminate because count is never incremented in
them.  If you don't want to change the value of count, make another
variable and assign count to it.  That way you can increment a copy of
count and not have to change it.



Wed, 18 Jun 1902 08:00:00 GMT  
 Help. Counting problem

 Try using more lowercase and indentation and your error
will become more obvious.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Counting source problem

2. Help counting records in a DB file

3. HELP ME TO COUNT MACHINE CYCLE

4. Help on counting word from a text data

5. Help needed with reading and counting of characters and words

6. Need help to list all directories and count the number of programs in each

7. Word Counting....HELP?

8. Help needed with reading and counting of characters and

9. getting the count of records in a filtered query

10. Counting Detail records with SQL or Recordcount

11. Counting time fields??

12. programme which counts derivative of simple function

 

 
Powered by phpBB® Forum Software