Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
string Posted by alaatrash on 10 Nov 2006 at 5:59 PM
hello.. I have s:string; and I want to know if the first char is number
because I want to make a condition.. so I wrote

.....................................
uses wincrt;
var first,s:string;
BEGIN
write('F(x)= ');
readln(s);
first:= copy(s,1,1);
if not (first in ['1'..'100']) then
.................................

but an error happend... : (ordinal expresion expected)..
what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
ALA
Report
Re: string Posted by zibadian on 10 Nov 2006 at 11:40 PM
: hello.. I have s:string; and I want to know if the first char is number
: because I want to make a condition.. so I wrote
:
: .....................................
: uses wincrt;
: var first,s:string;
: BEGIN
: write('F(x)= ');
: readln(s);
: first:= copy(s,1,1);
: if not (first in ['1'..'100']) then
: .................................
:
: but an error happend... : (ordinal expresion expected)..
: what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
: ALA
:
In can only be used with char and integer types, not with strings. Also how can 1 character (first) equal 3 characters ('100'). Getting the first character of a string can also much faster, because a string is basically an array of char. Thus you can use the array notation to get the first character:
  first := s[1];

where first is of the char type. Then you can use the in operator:
  if not (First in ['1'..'9']) then // No strings, but chars

Report
Re: string Posted by PP2005 on 11 Nov 2006 at 2:33 AM
:
:   if not (First in ['1'..'9']) then // No strings, but chars
: 



I guess you forgot to include '0', so the code would be:

  if not (First in ['0'..'9']) then // No strings, but chars


Report
Re: string Posted by zibadian on 12 Nov 2006 at 1:21 AM
: :
: :   if not (First in ['1'..'9']) then // No strings, but chars
: : 

:
:
: I guess you forgot to include '0', so the code would be:
:
:
:   if not (First in ['0'..'9']) then // No strings, but chars
: 

:
:
I didn't forget, because alaatrash's code also started at 1, and not at 0.
Report
Re: string Posted by PP2005 on 12 Nov 2006 at 4:37 AM
: I didn't forget, because alaatrash's code also started at 1, and not at 0.
:

You're right, but his code also ended with 100, so I figured he was trying to check if it was a number not only between 1 and 9...
Report
Re: string Posted by king0deu on 11 Nov 2006 at 4:51 AM
: hello.. I have s:string; and I want to know if the first char is number
: because I want to make a condition.. so I wrote
:
: .....................................
: uses wincrt;
: var first,s:string;
: BEGIN
: write('F(x)= ');
: readln(s);
: first:= copy(s,1,1);
: if not (first in ['1'..'100']) then
: .................................
:
: but an error happend... : (ordinal expresion expected)..
: what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
: ALA
:

Why don't you use this function:
val(first,x,code)


with x is a variable of integer (or real)
code will be 0 if the first is a number, and 1 if is isn't

That's simple!
Report
Re: string Posted by alaatrash on 13 Nov 2006 at 5:35 PM
thank you all.. I'll take your advices

Report
Re: string Posted by alaatrash on 14 Nov 2006 at 5:24 PM
: : hello.. I have s:string; and I want to know if the first char is number
: : because I want to make a condition.. so I wrote
: :
: : .....................................
: : uses wincrt;
: : var first,s:string;
: : BEGIN
: : write('F(x)= ');
: : readln(s);
: : first:= copy(s,1,1);
: : if not (first in ['1'..'100']) then
: : .................................
: :
: : but an error happend... : (ordinal expresion expected)..
: : what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
: : ALA
: :
:
: Why don't you use this function:
val(first,x,code)

:
: with x is a variable of integer (or real)
: code will be 0 if the first is a number, and 1 if is isn't
:
: That's simple!
:
but what is the use of the variable x?

Report
Re: string Posted by zibadian on 14 Nov 2006 at 9:15 PM
: : : hello.. I have s:string; and I want to know if the first char is number
: : : because I want to make a condition.. so I wrote
: : :
: : : .....................................
: : : uses wincrt;
: : : var first,s:string;
: : : BEGIN
: : : write('F(x)= ');
: : : readln(s);
: : : first:= copy(s,1,1);
: : : if not (first in ['1'..'100']) then
: : : .................................
: : :
: : : but an error happend... : (ordinal expresion expected)..
: : : what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
: : : ALA
: : :
: :
: : Why don't you use this function:
val(first,x,code)

: :
: : with x is a variable of integer (or real)
: : code will be 0 if the first is a number, and 1 if is isn't
: :
: : That's simple!
: :
: but what is the use of the variable x?
:
:
That's the converted value. See help files for more info.
Report
Re: string Posted by king0deu on 16 Nov 2006 at 4:21 AM
: : : : hello.. I have s:string; and I want to know if the first char is number
: : : : because I want to make a condition.. so I wrote
: : : :
: : : : .....................................
: : : : uses wincrt;
: : : : var first,s:string;
: : : : BEGIN
: : : : write('F(x)= ');
: : : : readln(s);
: : : : first:= copy(s,1,1);
: : : : if not (first in ['1'..'100']) then
: : : : .................................
: : : :
: : : : but an error happend... : (ordinal expresion expected)..
: : : : what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
: : : : ALA
: : : :
: : :
: : : Why don't you use this function:
val(first,x,code)

: : :
: : : with x is a variable of integer (or real)
: : : code will be 0 if the first is a number, and 1 if is isn't
: : :
: : : That's simple!
: : :
: : but what is the use of the variable x?
: :
: :
: That's the converted value. See help files for more info.
:

No need to care 'bout it, the important thing is that code is the condition to check if first is a number
And you should take Zibadian's advice,too
Report
Re: string Posted by zibadian on 16 Nov 2006 at 6:33 AM
: : : : : hello.. I have s:string; and I want to know if the first char is number
: : : : : because I want to make a condition.. so I wrote
: : : : :
: : : : : .....................................
: : : : : uses wincrt;
: : : : : var first,s:string;
: : : : : BEGIN
: : : : : write('F(x)= ');
: : : : : readln(s);
: : : : : first:= copy(s,1,1);
: : : : : if not (first in ['1'..'100']) then
: : : : : .................................
: : : : :
: : : : : but an error happend... : (ordinal expresion expected)..
: : : : : what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
: : : : : ALA
: : : : :
: : : :
: : : : Why don't you use this function:
val(first,x,code)

: : : :
: : : : with x is a variable of integer (or real)
: : : : code will be 0 if the first is a number, and 1 if is isn't
: : : :
: : : : That's simple!
: : : :
: : : but what is the use of the variable x?
: : :
: : :
: : That's the converted value. See help files for more info.
: :
:
: No need to care 'bout it, the important thing is that code is the condition to check if first is a number
: And you should take Zibadian's advice,too
:
The in-operator is much faster and takes less memory than the conversion. This might become very important if you need to perform this check very often, or the memory load must be as low as possible.



 

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
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.