Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help for a poor newbie...

Status
Not open for further replies.

MattShill

Programmer
May 18, 2005
2
0
0
GB
I've been studying Pascal at school and I'm trying to write a text based rpg, so lots of string and integer manipulation. I want to take values from a record I have defined and place them in some universal variables. The user inputs some text which is assigned to a variable. If that text value has already been defined as being a record, I want it to take a value from that record and do stuff to it, if it's not a record, I want it to say something like 'That's not a record'.

Here's a bit of code to show what I mean, instead of setting record data as variables, I'm siply trying to display them:

Code:
program monkeyrecords; 

type monkey=record 
        name : string; 
        legs : integer; 
     end; 

var 

chosen:string; 
monk1:monkey; 

BEGIN 

monk1.name := 'Mr. Monkey'; 
monk1.legs := 5; 

writeln ('choose a monkey'); 
readln (chosen); 

if (chosen)=monkey* 
then begin 

writeln ('Monkey is called'); 
writeln ((chosen).name);** 
writeln ('He has'); 
writeln ((chosen).legs);** 
writeln ('legs'); 
end 

if (chosen)<>monkey* 
begin 
write (chosen); 
writeln ('is not a monkey'); 
end; 

readln; 
END.



*First problem. The standard comparison operators only seem to work with actual values. Here I need to see if the variable is of the type monkey.

**Second problem. I gat 'Illegal Identifier' for the .name part, it probably doesn't like using a variable as the record name.

Any ideas? I've googled for ages and can't find anything, I'm stumped.
 
Well first off I think you need to look at your question and what you are stating, because I don't understand what you are trying to get accomplished.

Second off, data types don't work the way you are trying to get them to work. To your program, they're just pieces of data which for your sake get labeled in different ways. You've labeled monk1 with type monkey. As far as your programming options go, you can't make a test on that assumption.

Next off, if you're wanting to make a comparison by name (only guessing), you need to do something like "if monk1.name = chosen".

Get a clearer idea of what you're trying to accomplish and post back with it.
 
Thanks, I'll check out a few programming guides and see if they shed any light on the subject.

As for my goal, the code I posted was a hastily written demonstration of the way I've using the records, I'll post back with my actual code and situation when I've neatened things up a bit. At the moment it's full of spare bits of code left over from previous attempts as I solving most little bugs through trial and error! I'll spend a while getting it a bit more structured and adding comments and maybe post the procedures in question.

Just to clarify, there's no way of using 'if' to check if a value is of a given type? If not I could use an array of pre-defined records and have a 'case' for each one. However, I was planning on having numerous records (in this case monkeys) with one random attribute (legs), defined in a seperate procedure. The user would then be able to return to the code I posted earlier and retrieve the details of one of these records (monkeys).

Anyway, I'm rambling. As you say, I'll clarify what I'm going for and try and tackle it from a different angle.

Thanks for the help.
 
To test whether or not monk1 is of type monkey, you could use:

IF monk1 IS monkey

and to see if chosen is a monkey:
IF chosen IS monkey

Your variable chosen is declared as a string and so it is not monkey and will not therefore have a name field.

From what I can understand from your code, you should iterate through your monkey records checking the name field of each one against chosen.

Hope this helps.


 
It sounds like what you're wanting is exactly that: an array of records with attributes. If you keep it in memory, all it would take is just a search of the array for the name of your monkey in question and then show the record. Nothing really that complex.

Of course that's what it sounds like to me and it wouldn't hurt posting what you're trying to accomplish as I was saying before.
 
I was doing the same thing when I was younger.If you have any problems you must use your imagination(not only the manuals).There are always bugs in the programs you write.
but if use your head not only your fingers to wrire the souce it will all work out
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top