Board index » delphi » Why Age Check Function Won't Work?

Why Age Check Function Won't Work?

Hi

I am currently experiencing problems with my age check function.
Can someone please take a look at teh source code and tell me what's wrong?

The user enters a age, program will check for typos as well as whether age
is between 16yrs to 44yrs ONLY. If there is an error, error message will
display and user is prompted to re-enter the age.

So far, error message works but age check does not work. When I enter any
valid age value, I still get error message.

Source Code is at the end of post.

Help greatly appreciated.

Thank You. (In Advance)

Regards

Poh San
Email: ps7577...@pacific.net.sg

Procedure Entry_Age;

Var
      Target_Age : String[2];

Begin
          Valid := False;
          Repeat
                       GoToxy(15,9);
                       clreol;
                       Write('Age      : ');
                       Readln(Target_Age);
                       Position := + 1;
                       If (length(Target_Age) = 2) Then
                       Valid := True;
                       While (Valid) And (Position <= length(Target_Age)) Do
                                 Begin
                                           If (Target_Age[Position] in
['0'..'9']) And
                                              (Target_Age[Position] <= '16')
And
                                              (Target_Age[Position] > '45')
Then
                                             Position := Position + 1
                                           Else
                                                  Valid := False;
                                 End;
                                         If (Valid = False) Then
                                            Begin
                                                      GoToxy(30,23);
                                                      Write('InValid Entry,
Age Is Between 16 Yrs To 44 Yrs.');
                                                      GoToxy(30,24);
                                                      Write('Press Any Key
To Continue...');
                                                      Readkey;
                                                      GoToxy(30,24);
                                                      clreol;
                                                      GoToxy(30,23);
                                                      clreol;
                                             End
                                          Else
                                                  Begin
                                                             For position :=
1 To length(Target_Age) Do

Target_Age[position] := upCase(Target_Age[position]);
                                                             GoToxy(26,9);

Writeln(Result[Counter].Age);
                                                  End;
        Until (Valid);
End;

 

Re:Why Age Check Function Won't Work?


In comp.lang.pascal.borland Lim Poh San <ps7577...@pacific.net.sg> wrote:

: I am currently experiencing problems with my age check function.
: Can someone please take a look at teh source code and tell me what's wrong?

: The user enters a age, program will check for typos as well as whether age
: is between 16yrs to 44yrs ONLY. If there is an error, error message will
: display and user is prompted to re-enter the age.

: So far, error message works but age check does not work. When I enter any
: valid age value, I still get error message.

: Var
:       Target_Age : String[2];

:                        Position := + 1;

are you trying to increment Position here (ala C/C++), or do you actually
want it to equal positive 1?  the latter is what will happen..

:                                            If (Target_Age[Position] in
: ['0'..'9']) And
:                                               (Target_Age[Position] <= '16')
: And
:                                               (Target_Age[Position] > '45')
: Then

uhh.. this makes no sense to me.  Target_Age is apparently a string that holds
a two-digit age.  however, Target_Age[Position] only returns ONE character
of the string, just like Array[x] would only return element x of Array.
so comparing one character to '16' or '45' probably isn't what you want to
do.  you should compare the whole 2 letter string Position when you're done
reading it.

-Assassin   assas...@reality.yps.org

Re:Why Age Check Function Won't Work?


In article <7ejs3u$4v...@newton3.pacific.net.sg>,
  "Lim Poh San" <ps7577...@pacific.net.sg> wrote:

Quote
> Hi

> I am currently experiencing problems with my age check function.
> Can someone please take a look at teh source code and tell me what's wrong?

> The user enters a age, program will check for typos as well as whether age
> is between 16yrs to 44yrs ONLY. If there is an error, error message will
> display and user is prompted to re-enter the age.

> So far, error message works but age check does not work. When I enter any
> valid age value, I still get error message.

> Source Code is at the end of post.

I presume the nice formatting is due to the use of tabs, please don't use
them, or expand them before you post...

Quote
> Procedure Entry_Age;

> Var
>       Target_Age : String[2];

> Begin
>           Valid := False;
>           Repeat
>                        GoToxy(15,9);
>                        clreol;
>                        Write('Age      : ');
>                        Readln(Target_Age);
>                        Position := + 1;
>                        If (length(Target_Age) = 2) Then
>                        Valid := True;
>                        While (Valid) And (Position <= length(Target_Age)) Do
>                                  Begin
>                                            If (Target_Age[Position] in
> ['0'..'9']) And
>                                               (Target_Age[Position] <= '16')

Target_Age[Postition] is a single character, you can compare it to '16', but
the comparison may not deliver what you expect.

Quote
> And
>                                               (Target_Age[Position] > '45')

Target_Age[Postition] is a single character, you can compare it to '45' etc...

- Show quoted text -

Quote
> Then
>                                              Position := Position + 1
>                                            Else
>                                                   Valid := False;
>                                  End;
>                                          If (Valid = False) Then
>                                             Begin
>                                                       GoToxy(30,23);
>                                                       Write('InValid Entry,
> Age Is Between 16 Yrs To 44 Yrs.');
>                                                       GoToxy(30,24);
>                                                       Write('Press Any Key
> To Continue...');
>                                                       Readkey;
>                                                       GoToxy(30,24);
>                                                       clreol;
>                                                       GoToxy(30,23);
>                                                       clreol;
>                                              End
>                                           Else
>                                                   Begin
>                                                              For position :=
> 1 To length(Target_Age) Do

> Target_Age[position] := upCase(Target_Age[position]);
>                                                              GoToxy(26,9);

> Writeln(Result[Counter].Age);
>                                                   End;
>         Until (Valid);
> End;

Robert
--
Robert AH Prins
prin...@williscorroon.com

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Why Age Check Function Won't Work?


: uhh.. this makes no sense to me.  Target_Age is apparently a string that holds
: a two-digit age.  however, Target_Age[Position] only returns ONE character
: of the string, just like Array[x] would only return element x of Array.
: so comparing one character to '16' or '45' probably isn't what you want to
: do.  you should compare the whole 2 letter string Position when you're done
: reading it.

err.. that is when you're done CHECKING it for typos.  here's complete code:

-----------------------------------------------------------------
Procedure Entry_Age;

Var
  Target_Age : String[2];
  position:integer;  {don't redeclare here if you've declared}
  valid:boolean;      {these elsewhere}

Begin
 Valid := False;
 Repeat
  GoToxy(15,9);
  clreol;
  Write('Age      : ');
  Readln(Target_Age);
  Position := + 1;   {that's positive 1}
  If (length(Target_Age) = 2) Then
    Valid := True;
  While (Valid) And (Position <= length(Target_Age)) Do
   Begin
    If (Target_Age[Position] in ['0'..'9']) then
     Position := Position + 1
    Else
     Valid := False;
   End;
  if (Target_Age < '16') OR (Target_Age > '44')
   then valid := False;
  If (Valid = False) Then Begin
   GoToxy(30,23);
   Write('InValid Entry, Age Is Between 16 Yrs To 44 Yrs.');
   GoToxy(30,24);
   Write('Press Any Key To Continue...');
   Readkey;
   GoToxy(30,24);
   clreol;
   GoToxy(30,23);
   clreol;
  End
  Else Begin
 { For position :=1 To length(Target_Age) Do
    Target_Age[position] := upCase(Target_Age[position]); }

  {commented out the above - you can't upcase numerical digits,
   as far as i know}

   GoToxy(26,9);
   Writeln(Result[Counter].Age);  {what is this?}

  end;
 Until (Valid);
End;

begin
 clrscr;
 Entry_age;
end.

-------------------------------------------------------------------

now of course this code doesn't handle fun stuff like "044" correctly.
you'd want to use Val() in that case.  but anybody who types zeroes
before their damn age should be horse-whipped..

-Assassin    assas...@reality.yps.org

Re:Why Age Check Function Won't Work?


In article <7ejs3u$4v...@newton3.pacific.net.sg>,
Lim Poh San <ps7577...@pacific.net.sg> wrote:

Quote
>Hi

>I am currently experiencing problems with my age check function.
>Can someone please take a look at teh source code and tell me what's wrong?

>The user enters a age, program will check for typos as well as whether age
>is between 16yrs to 44yrs ONLY. If there is an error, error message will
>display and user is prompted to re-enter the age.

>So far, error message works but age check does not work. When I enter any
>valid age value, I still get error message.

>Source Code is at the end of post.

>Help greatly appreciated.

>Thank You. (In Advance)

>Regards

>Poh San
>Email: ps7577...@pacific.net.sg

>Procedure Entry_Age;

>Var
>      Target_Age : String[2];

Use at least string[3]. Otherwise one can enter 160 and it becomes 16.
Well there is no reason to limit the length in this case, so you could
just use string.

- Show quoted text -

Quote

>Begin
>          Valid := False;
>          Repeat
>                       GoToxy(15,9);
>                       clreol;
>                       Write('Age      : ');
>                       Readln(Target_Age);
>                       Position := + 1;
>                       If (length(Target_Age) = 2) Then
>                       Valid := True;
>                       While (Valid) And (Position <= length(Target_Age)) Do
>                                 Begin
>                                           If (Target_Age[Position] in
>['0'..'9']) And
>                                              (Target_Age[Position] <= '16')
>And
>                                              (Target_Age[Position] > '45')

When on earth are you attempting here. You are comparing a character with
a string. That does not perform any numeric comparison.

Use VAL() to convert the string into a number and then compare.

Osmo

Re:Why Age Check Function Won't Work?


Quote
In article <7ejs3u$4v...@newton3.pacific.net.sg> ps7577...@pacific.net.sg wrote...
> Procedure Entry_Age;
> Var
>    Target_Age : String[2];
> Begin
>    Readln(Target_Age);
>    While (Valid) And (Position <= length(Target_Age)) Do
>       If (Target_Age[Position] in

Target_Age is a string, so Target_Age[Position] will only return a
character, not a string.

Incidentally, I must congratulate you on having the worst indentation
style I've seen in decades.

Mike{*word*106}son, Black Cat Software Factory, Edinburgh, Scotland
fax 0131-271-1551 - Columnated Ruins Domino - Mellotron M400 #996

Re:Why Age Check Function Won't Work?


Quote
Lim Poh San wrote:

> Hi

> I am currently experiencing problems with my age check function.
> Can someone please take a look at teh source code and tell me what's wrong?

> The user enters a age, program will check for typos as well as whether age
> is between 16yrs to 44yrs ONLY. If there is an error, error message will
> display and user is prompted to re-enter the age.

> So far, error message works but age check does not work. When I enter any
> valid age value, I still get error message.

> Source Code is at the end of post.

> Help greatly appreciated.

> Thank You. (In Advance)

> Regards

> Poh San
> Email: ps7577...@pacific.net.sg

> Procedure Entry_Age;

> Var
>       Target_Age : String[2];

... snip ...

Quote
>                                           If (Target_Age[Position] in
> ['0'..'9']) And
>                                        (Target_Age[Position] <= '16')
> And
>                                        (Target_Age[Position] > '45')
> Then
>                                              Position := Position + 1
>                                            Else
>                                                   Valid := False;

... snip

  Target_Age[Position] is only a character, and you are comparing it to
strings ... this will not produce the result you are looking for. I
suggest you read the age in as an integer rather than a string, and then
just have something like :

  if (num >= 16) and (num <= 44) then valid := true;

        - Bob

Re:Why Age Check Function Won't Work?


I'm just a beginning programmer in Pascal, taking it for a college course, but
why wouldn't just something like this work:

program test;
uses WinCrt;

procedure agecheck;

var
   age : integer;
   valid : boolean;

begin
     write('Enter age    : ');
     readln(age);
     IF((age >= 16) AND (age <= 44)) THEN
        writeln('Age is: ', age)
     ELSE
        writeln('Invalid entry: age must be 16-44');
end;

begin {main program}
agecheck;
end.

Quote
Lim Poh San wrote:
> Hi

> I am currently experiencing problems with my age check function.
> Can someone please take a look at teh source code and tell me what's wrong?

> The user enters a age, program will check for typos as well as whether age
> is between 16yrs to 44yrs ONLY. If there is an error, error message will
> display and user is prompted to re-enter the age.

> So far, error message works but age check does not work. When I enter any
> valid age value, I still get error message.

> Source Code is at the end of post.

> Help greatly appreciated.

> Thank You. (In Advance)

> Regards

> Poh San
> Email: ps7577...@pacific.net.sg

> Procedure Entry_Age;

> Var
>       Target_Age : String[2];

> Begin
>           Valid := False;
>           Repeat
>                        GoToxy(15,9);
>                        clreol;
>                        Write('Age      : ');
>                        Readln(Target_Age);
>                        Position := + 1;
>                        If (length(Target_Age) = 2) Then
>                        Valid := True;
>                        While (Valid) And (Position <= length(Target_Age)) Do
>                                  Begin
>                                            If (Target_Age[Position] in
> ['0'..'9']) And
>                                               (Target_Age[Position] <= '16')
> And
>                                               (Target_Age[Position] > '45')
> Then
>                                              Position := Position + 1
>                                            Else
>                                                   Valid := False;
>                                  End;
>                                          If (Valid = False) Then
>                                             Begin
>                                                       GoToxy(30,23);
>                                                       Write('InValid Entry,
> Age Is Between 16 Yrs To 44 Yrs.');
>                                                       GoToxy(30,24);
>                                                       Write('Press Any Key
> To Continue...');
>                                                       Readkey;
>                                                       GoToxy(30,24);
>                                                       clreol;
>                                                       GoToxy(30,23);
>                                                       clreol;
>                                              End
>                                           Else
>                                                   Begin
>                                                              For position :=
> 1 To length(Target_Age) Do

> Target_Age[position] := upCase(Target_Age[position]);
>                                                              GoToxy(26,9);

> Writeln(Result[Counter].Age);
>                                                   End;
>         Until (Valid);
> End;

--
______________________________________________________
Andrew Getka                    Phone: (717)849-8479
York College of PA            Email: age...@ycp.edu
Penn Hall box 228              Troopr...@hotmail.com
York, PA 17405                 ICQ #: 33204865
        http://coyote.ycp.edu/~agetka

Re:Why Age Check Function Won't Work?


In article <3714AAC8.FE3AD...@ycp.edu>,
  "Andrew G." <age...@ycp.edu> wrote:

Quote
> I'm just a beginning programmer in Pascal, taking it for a college course, but
> why wouldn't just something like this work:

> program test;
> uses WinCrt;

> procedure agecheck;

> var
>    age : integer;
>    valid : boolean;

> begin
>      write('Enter age    : ');
>      readln(age);
>      IF((age >= 16) AND (age <= 44)) THEN
>         writeln('Age is: ', age)
>      ELSE
>         writeln('Invalid entry: age must be 16-44');
> end;

> begin {main program}
> agecheck;
> end.

The (obvious) reason for using more than a simple readln to get age is the
fact that readln accepts everything and that the compiler has no way of
knowing that some user is going to enter "seven{*word*249}" when the program expects
a number. Try it with your program...

Quote
> Lim Poh San wrote:

 <snipped>

Robert
--
Robert AH Prins
prin...@williscorroon.com

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Why Age Check Function Won't Work?


:> uses WinCrt;
:>
:> procedure agecheck;
:>
:> var
:>    age : integer;
:> begin
:>      write('Enter age    : ');
:>      readln(age);
:>      IF((age >= 16) AND (age <= 44)) THEN
:>         writeln('Age is: ', age)

: The (obvious) reason for using more than a simple readln to get age is the
: fact that readln accepts everything and that the compiler has no way of
: knowing that some user is going to enter "seven{*word*249}" when the program expects
: a number. Try it with your program...

would you just use ExitProc to trap the error if somebody types inappropriate
characters?

-Assassin

Re:Why Age Check Function Won't Work?


Quote
> :> procedure agecheck;
> :> var age : integer;
> :> begin
> :>   write('Enter age    : '); readln(age);
> :>   IF((age >= 16) AND (age <= 44)) THEN
> :>         writeln('Age is: ', age)

> : The (obvious) reason for using more than a simple readln to get age is the
> : fact that readln accepts everything and that the compiler has no way of
> : knowing that some user is going to enter "seven{*word*249}" when the program expects
> : a number. Try it with your program...

> would you just use ExitProc to trap the error if somebody types inappropriate
> characters?

   That, IMHO, is overkill.  Depending on the application's
_requirements_ (need to force a valid response, ability to produce
meaningful error responses, etc.), there are a number of things which are
simpler and easier; most involve reading in a string variable, using a
string-to-integer conversion, and working with the result.  For example:

Var S      : string;
    Age, N : integer;
begin
  Age := -1;                            { start loop }
  while (Age < 16) or (Age > 44) do
    begin
      write ('Enter Age: '); readln (S); Val (S,Age,N);
      if N > 0 then writeln ('Error: non-numeric data entered')
      else
        if (Age < 16) or (Age > 44) then
          writeln ('Invalid input: Age must be 16-44')
    end;
  writeln (Your Age is: ',Age)
end

Other Threads