www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/27/16:27:54

Comments: Authenticated sender is <mert0407 AT sable DOT ox DOT ac DOT uk>
From: "George Foot" <george DOT foot AT merton DOT oxford DOT ac DOT uk>
To: Mikhail Yakshin <myakshin AT glasnet DOT ru>
Date: Thu, 27 Aug 1998 21:23:46 +0000
MIME-Version: 1.0
Subject: Re: Another newbie question
Reply-to: george DOT foot AT merton DOT oxford DOT ac DOT uk
CC: djgpp AT delorie DOT com
Message-Id: <E0zC8bg-00011k-00@sable.ox.ac.uk>

On 27 Aug 98 at 22:09, Mikhail Yakshin wrote:

> Can someone explain me the basics of string operations? Preferably the
> differences of string handling of C vs Borland Pascal.

I doubt it!  That's far too deep a question.  In C, strings are just 
arrays of characters, conventionally terminated by a zero character, 
(i.e. '\0', byte value 0).  The operation of the string functions in 
the C library is sometimes quite subtle, and you really should find a 
good book and learn from there.  If you can't afford a good book, you 
could try some web based C tutorials.  Try this:

    http://www.swcp.com/~dodrill/

There's a C tutorial there with chapters on strings, arrays and 
pointers.

> 1) How can I add characters in "chat-mode" to my string? I've tried
> strcat but haven't figured out how it works :(

I presume you can detect the first press of Enter.  After that, once
per game loop you need to check for characters in the keyboard buffer
(use the `keypressed' function) and, if there are any, add them to
your string.  Maintain a pointer to the terminating zero byte of the
string, and when new characters come in, write the character over the
zero byte, put a new zero byte just after the character, and increase
the pointer to point to this new zero byte.  All through this you 
should check that your string is not longer than the memory you 
allocated for it.

> 2) How can I capture only alphanumeric keys and "hide" that user pressed it
> from the rest of program (i.e. when user presses "a" in chat-mode, "a"
> should be added to chat-string, but not "A"ttack order executed).

It depends how you're reading your "attack order" command.  If you're 
using readkey for that then there'll be no problem -- when you 
readkey out of the keyboard buffer, the keypress is gone.

If you're using key[KEY_A] then you'll need extra conditions that say 
that when in chat mode, all commands are disabled.

> 3) Can I have an array of strings that I can show on screen as "last
> messages buffer"? How to add and scroll this array?

It's not simple, especially if you don't understand how C strings
work.  Use an array of pointers to char, each one pointing to a
recently spoken string.  Cycle the array when a new string comes in
and point the last pointer in the array to this string.  Don't
forget to free up the spoken strings when they fall off the top of
the list.  Alternatively you could use a linked list to store an
arbitrary number of spoken strings, but that's even more complicated.

-- 
george DOT foot AT merton DOT oxford DOT ac DOT uk

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019