delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/30/06:01:10

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Why won't this work?
Date: Sun, 29 Mar 1998 20:11:50 -0500
Organization: Two pounds of chaos and a pinch of salt.
Lines: 58
Message-ID: <351EF156.552C@cs.com>
References: <351E8C26 DOT 2608F112 AT netrover DOT com>
NNTP-Posting-Host: ppp241.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Nicolas Blais wrote:
> 
> Hi,
> Check the code below, can't figure out why this doesn't work, I mean,
> should it exit when I enter exit?  Also, when I type spaces into my
> string, it creates as many ":" as I inputed spaces.  Can anyone fix
> this?
> 
> getcommand(void)
> {
> char *p;
> char *com;
>  for (p = ":"; *p;p++)
>  put_char(*p, 10);
>  scanf("%s",com);
>  if (strcmp(com, "exit") == 0)        exit(0);
>  getcommand();
>  return 1;
> }

This code is very strange.  In fact, it makes no sense whatsoever.  Why
would you declare a pointer variable, set it to point to a static string
(":"), and then try to increment it?  Why would you try to write
characters into it, or whatever it is you appear to be doing with
put_char( *p, 10 ).  I've never seen that function before.  You are also
passing 'com' to scanf() without allocating space to store its data. 
Finally, you appear to be invoking this function recursively which is a
horrible idea.

If I understand your code correctly, you are trying to print a colon for
a prompt, read a string via scanf(), and check to see if the string is
"exit".  If it is, you want to leave the program, if not, you want to go
back and read another string.  Okay, here's some code to do that:

int getcommand( void )
{
    char com[100];

    while ( 1 )
    {
        putchar( ':' );
        scanf( "%s", com );
        if ( strcmp( com, "exit" ) == 0 )
            exit( 0 );
    }
    return 1;  /* this code will never be reached */
}

If I'm missing the point, please tell me what you are really trying to
do.

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Courage is the complement of fear.  |
|       aka Fighteer I       | A man who is fearless cannot be      |
|   mailto:fighteer AT cs DOT com   | courageous.  (He is also a fool.)"   |
| http://www.cs.com/fighteer |                    - Lazarus Long    |
---------------------------------------------------------------------

- Raw text -


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