Recent

Author Topic: Length function not working?  (Read 3645 times)

TheOddOne

  • Limited User (Deny Edit/Delete existing posts)
  • Jr. Member
  • *
  • Posts: 77
  • Hehe
    • SpawnScape614
Length function not working?
« on: May 29, 2015, 04:58:53 pm »
Code: [Select]

  Procedure GetPlayerNames;
    Begin
    Repeat
     Write('Enter player one name: ');
     Readln(FirstPlayer);
     If Length(FirstPlayer) = 0 Then
       Writeln('No, the player name must be more than l character')
     Else
       Begin
     Write('Enter player two name: ');
     Readln(SecondPlayer);
     If Length(SecondPlayer) = 0 Then
       Writeln('No, the player name must be more than l character')
     End;
    Until (Length(FirstPlayer > 0)) And (Length(SecondPlayer > 0));
   End;   


I want to ensure that the player enters a name that is obviously more than one character to ensure that the program does not crash. For some strange reason, this isn't working.
ShowMessage('Yes');

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: Length function not working?
« Reply #1 on: May 29, 2015, 05:03:53 pm »
If you want to check that the length of a Player is MORE than 1 character you should use
Code: [Select]
If Length(FirstPlayer) < 2 Then
//...
If Length(SecondPlayer) < 2 Then
Later on
Code: [Select]
Until (Length(FirstPlayer > 1)) And (Length(SecondPlayer > 1));

(and not "= 0" and "> 0", as you have now)

TheOddOne

  • Limited User (Deny Edit/Delete existing posts)
  • Jr. Member
  • *
  • Posts: 77
  • Hehe
    • SpawnScape614
Re: Length function not working?
« Reply #2 on: May 29, 2015, 05:06:09 pm »
Code: [Select]

  Procedure GetPlayerNames;
    Begin
    Repeat
     Write('Enter player one name: ');
     Readln(FirstPlayer);
     If Length(FirstPlayer) = 0 Then
       Writeln('No, the player name must be more than l character')
     Else
       Begin
     Write('Enter player two name: ');
     Readln(SecondPlayer);
     If Length(SecondPlayer) = 0 Then
       Writeln('No, the player name must be more than l character')
     End;
    Until (Length(FirstPlayer > 0)) And (Length(SecondPlayer > 0));
   End;   


I want to ensure that the player enters a name that is obviously more than one character to ensure that the program does not crash. For some strange reason, this isn't working.


Thanks, that actually made sense, however regardless, I'm retrieving this compilation error:


Quote

COMP1.lpr(43,33) Error: Incompatible types: got "ShortInt" expected "AnsiString"
ShowMessage('Yes');

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: Length function not working?
« Reply #3 on: May 29, 2015, 05:09:11 pm »
I can't see how you defined your variables.

A string is not the same as an integer (or shortint).

What line is line 43?

(Maybe you defined FirstPlayer as Shortint and then you can't use Length(FirstPlayer); )

For some strange reason, this isn't working.
Always make sure to describe the exact problem you're having.

"It's not working" is not exactly a clear problem-description.
« Last Edit: May 29, 2015, 05:11:07 pm by rvk »

TheOddOne

  • Limited User (Deny Edit/Delete existing posts)
  • Jr. Member
  • *
  • Posts: 77
  • Hehe
    • SpawnScape614
Re: Length function not working?
« Reply #4 on: May 29, 2015, 05:10:20 pm »
Code: [Select]

  Procedure GetPlayerNames;
    Begin
    Repeat
     Write('Enter player one name: ');
     Readln(FirstPlayer);
     If Length(FirstPlayer) = 0 Then
       Writeln('No, the player name must be more than l character')
     Else
       Begin
     Write('Enter player two name: ');
     Readln(SecondPlayer);
     If Length(SecondPlayer) = 0 Then
       Writeln('No, the player name must be more than l character')
     End;
    Until (Length(FirstPlayer > 0)) And (Length(SecondPlayer > 0));
   End;   


I want to ensure that the player enters a name that is obviously more than one character to ensure that the program does not crash. For some strange reason, this isn't working.


Thanks, that actually made sense, however regardless, I'm retrieving this compilation error:


Quote

COMP1.lpr(43,33) Error: Incompatible types: got "ShortInt" expected "AnsiString"


Hmm:


Code: [Select]
    FirstPlayer , SecondPlayer : String;

They're both strings.
ShowMessage('Yes');

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: Length function not working?
« Reply #5 on: May 29, 2015, 05:11:55 pm »
What line is line 43?

For some strange reason, this isn't working.
Always make sure to describe the exact problem you're having.

"It's not working" is not exactly a clear problem-description.

If you get an compile error, please point to the exact line.
(I can't locate it in the code you gave because you didn't give the complete code)

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Length function not working?
« Reply #6 on: May 29, 2015, 05:14:00 pm »
Quote
Until (Length(FirstPlayer > 0)) And (Length(SecondPlayer > 0));
You are using the closing bracket at the wrong place:

Until (Length(FirstPlayer) > 0) And (Length(SecondPlayer) > 0);

TheOddOne

  • Limited User (Deny Edit/Delete existing posts)
  • Jr. Member
  • *
  • Posts: 77
  • Hehe
    • SpawnScape614
Re: Length function not working?
« Reply #7 on: May 29, 2015, 06:10:07 pm »
Quote
Until (Length(FirstPlayer > 0)) And (Length(SecondPlayer > 0));
You are using the closing bracket at the wrong place:

Until (Length(FirstPlayer) > 0) And (Length(SecondPlayer) > 0);


Oh I just assumed it needed to go to the end. I was thinking what else could possibly be wrong with it. Thank you!
ShowMessage('Yes');

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Length function not working?
« Reply #8 on: May 29, 2015, 06:19:10 pm »
"Length" is a function and "FirstPlayer" is its argument. Therefore, it must be "Length(FirstPlayer)".

marcio2003

  • Jr. Member
  • **
  • Posts: 69
Re: Length function not working?
« Reply #9 on: May 29, 2015, 07:30:50 pm »
Code: [Select]
COMP1.lpr(43,33) Error: Incompatible types: got "ShortInt" expected "AnsiString"
Please check the line 43 in column 33. it is in that place the error.
Lazarus 2.0.10 Windows 10 64bits

 

TinyPortal © 2005-2018