[comp.lang.pascal] Help me

tpehrson@javelin.sim.es.com (Tim Clinkenpeel) (04/18/91)

I'm getting the error "Type mismatch" in the following:

Procedure writ(txt:string);
 var huh:char;
 begin;
 for x:=1 to length(txt) do begin
  huh:=copy(txt,x,1);              <---error occurs here
  case huh of
   '[' : red;
   ']' : rx;
   else write(huh);
  end; writeln; end;
 end;

this is contrary to the FINE (sarcastic) book i have, _Turbo_Pascal_Trilogy_,
which states:

"...Data can be moved into a char variable using several methods.  The 
 assignments can be made using the assign (:=) operation with a function such
 as Chr(), single character extractions from strings of characters, or..."
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
is there another way to do a single character extraction from a string that i
am overlooking or is the text wrong?

i also tried modifying by having "huh" be an integer and changing the fifth line
to "huh:=ord(copy(txt,x,1));" which results in the error "Ordinal expression
expected".  furthermore, i tried having "huh" be a string, which results in the
same error, only on line 6 (between "huh" and "of" to be exact).

If you can help me with this irksome problem, i would very much enjoy hearing
from you (preferably email).  and, oh, i'm using turbo pascal v5.0.

THANX
-- 
one two! one two! & through & through /=============================/ hear your
the vorpal blade went snicker-snack! / tpehrson@javelin.sim.es.com / death boon 
he left it dead, and with his head  /=====aka: tim clinkenpeel====/ the wail of
he went galumphing back [DISCLAIMER: send SASE to: disclaimer, 84151-0521] DOOM 

IO92203@MAINE.BITNET (Scott Maxell) (04/18/91)

Tim writes:
>I'm getting the error "Type mismatch" in the following:
>
>Procedure writ(txt:string);
> var huh:char;
> begin;
> for x:=1 to length(txt) do begin
>  huh:=copy(txt,x,1);              <---error occurs here
>  case huh of
>   '[' : red;
>   ']' : rx;
>   else write(huh);
>  end; writeln; end;
> end;

   Your type mismatch error is occuring because you are trying to
assign a STRING result (from copy) to a CHARACTER variable. You
can reference portions of a string directly as characters by
using Txt [x]. Your ordinal type expected error was from ORD
trying to get the ORDinal of a STRING result again.

//////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
+---------+ Scott Maxell  -- Bitnet   ->> IO92203 @ maine
|         |               -- Internet ->> IO92203 @ maine.maine.edu
|    O    |
|    |    | "What I need is a computer that will do what I want it to
+---------+ do, not what I tell it to do..."

ehill@ziggy.austin.ibm.com (04/18/91)

In article <1991Apr18.020517.7767@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
>I'm getting the error "Type mismatch" in the following:
>
>Procedure writ(txt:string);
> var huh:char;
> begin;
> for x:=1 to length(txt) do begin
>  huh:=copy(txt,x,1);              <---error occurs here
>  case huh of
>   '[' : red;
>   ']' : rx;
>   else write(huh);
>  end; writeln; end;
> end;
---
The type mismatch happens because the copy() function returns a string not char.
Replace the line where you assign the value to huh with the following:

huh := txt[x];

This works because strings can be subscripted the same as normal arrays.
This should also clear up the ordinal value problems as well.  In Pascal
characters, numerics, and enumerated types are considered ordinal variable
types.

Hope this helps,
---
Ed Hill
Personal Systems Programming, IBM Austin
internet: ehill@wombat.austin.ibm.com

bobb@vice.ICO.TEK.COM (Bob Beauchaine) (04/18/91)

In article <1991Apr18.020517.7767@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
>I'm getting the error "Type mismatch" in the following:
>
>Procedure writ(txt:string);
> var huh:char;
> begin;
> for x:=1 to length(txt) do begin
>  huh:=copy(txt,x,1);              <---error occurs here

  Try huh := txt[i];

  This is the simplest approach. If you're feeling lucky, you
  might try huh := char(copy(txt,x,1)), but my gut feeling is
  that you'll still have the type mismatch error.
   
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ 

Bob Beauchaine bobb@vice.ICO.TEK.COM 

C: The language that combines the power of assembly language with the 
   flexibility of assembly language.

TOMJ@csdserver3.csd.scarolina.edu (Thomas E. Jenkins, Jr.) (04/19/91)

>I'm getting the error "Type mismatch" in the following:
>
>Procedure writ(txt:string);
> var huh:char;
> begin;
> for x:=1 to length(txt) do begin
>  huh:=copy(txt,x,1);              <---error occurs here
    |    |______________  Result type STRING
    |_____  TYPE CHAR

   case txt [ x ] of      < -----  Try this
>   '[' : red;
>   ']' : rx;
>   else write(huh);
>  end; writeln; end;
> end;
>

tom
+--------------------------------------------------------------------------+
|  Thomas E. Jenkins, Jr.   Programmer, University of South Carolina CSD   |
+--------------------------------------------------------------------------+
| BITNET         :  C0361@UNIVSCVM.BITNET  |  CSDNET  :  tomj/csdserver3   |
| INTERNET       :  TOMJ@csdserver3.csd.scarolina.EDU          {PREFERRED} |
|                :  C0361@univscvm.csd.scarolina.EDU  |  129.252.43.30     |
| FROM Compuserv :  INTERNET:TOMJ@csdserver3.csd.scarolina.EDU {PREFERRED} |
|                :  INTERNET:C0361@univscvm.csd.scarolina.EDU              |
+--------------------------------------------------------------------------+

nmouawad@watmath.waterloo.edu (Naji Mouawad) (04/19/91)

In article <1991Apr18.020517.7767@javelin.sim.es.com tpehrson@javelin.sim.es.com writes:
I'm getting the error "Type mismatch" in the following:

Procedure writ(txt:string);
 var huh:char;
 begin;
 for x:=1 to length(txt) do begin
  huh:=copy(txt,x,1);              <---error occurs here
  case huh of
   '[' : red;
   ']' : rx;
   else write(huh);
  end; writeln; end;
 end;

You should typecast string into char by issing the command
  huh := char(copy(txt,x,1));

But you don't need to do so, for you could replace

   case huh of

by case txt[i] of 

--Naji.
-- 
     -------------------------------------------------------------------
    | Naji Mouawad  |          nmouawad@watmath.waterloo.edu            |
    |  University   |---------------------------------------------------|
    | Of Waterloo   |   "The Stranger in us is our most familiar Self"  |

marcos@allanon.UUCP (Marcos Della) (05/21/91)

nmouawad@watmath.waterloo.edu (Naji Mouawad) writes:
> In article <1991Apr18.020517.7767@javelin.sim.es.com tpehrson@javelin.sim.es.
> I'm getting the error "Type mismatch" in the following:
> 
> Procedure writ(txt:string);
>  var huh:char;
>  begin;
>  for x:=1 to length(txt) do begin
>   huh:=copy(txt,x,1);              <---error occurs here
>   case huh of
>...

Try changing the case to:
   CASE txt[x] OF
or the previous line to:
   huh := txt[x];

Marcos


____________________________________________________________________________
Marcos R. Della                           | Did you ever wonder if there was
allanon!marcos@petunia.CalPoly.EDU (home) | more to the universe than this?
mdella@polyslo.CalPoly.EDU       (school) |
marcos.della@f185.n125.z1.fidonet.org     |         ...I didn't...

granoff@vaxwrk.enet.dec.com (Mark H. Granoff) (05/22/91)

>nmouawad@watmath.waterloo.edu (Naji Mouawad) writes:
>> In article <1991Apr18.020517.7767@javelin.sim.es.com tpehrson@javelin.sim.es.
>> I'm getting the error "Type mismatch" in the following:
>> 
>> Procedure writ(txt:string);
>>  var huh:char;
>>  begin;
>>  for x:=1 to length(txt) do begin
>>   huh:=copy(txt,x,1);              <---error occurs here
>>   case huh of
>>...

For starters, check that your parameters to Copy() are in the correct order. 
But more likely the problem is that huh is a Char and Copy() returns a string.
You might try

	huh := txt[x];

or eliminate this all together and use

	case txt[x] of

(I think that will work.)

Hope this helps,

-Mark

--

---------------------------------------------------------------------------
Mark H. Granoff   |    Enterprise Integration Services/Engineering VAXworks
---------------------------------------------------------------------------
Digital Equipment Corporation | Internet: granoff@vaxwrk.enet.dec.com
129 Parker Street             | Usenet  : ...!decwrl!vaxwrk.enet!granoff
PKO2-1/M21                    | AT&T    : +1 508 493 4512
Maynard, MA 01754             | FAX     : +1 508 493 2240
---------------------------------------------------------------------------
Opinions herein are my own and do not necessarily reflect those of Digital.
---------------------------------------------------------------------------