www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/19/01:47:53

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help with scanf()
Date: Fri, 18 Jul 1997 22:01:01 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 65
Message-ID: <33CFE79D.360C@cs.com>
References: <4GFM1XAs28zzEwrp AT netbook DOT demon DOT co DOT uk>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp104.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

James MacDonald wrote:
> 
> At the moment I'm using scanf() - I need to read a value from the
> keyboard and compare it with a generated one made by a program.
> 
> Here's the code :

You need to read a good book on C.  You're making several critical
errors in this code.

> char * enter;

When declaring a buffer to store data, you should use an array.  You're
declaring a pointer but not giving it any storage space to point to. 
If, for some reason, you really do want to use a pointer for this
situation (believe me, you don't), you must malloc() some memory for it
to point to.

> scanf("%s", enter);

Here, scanf() reads a string into the area of memory pointed to by
'enter'.  Since you not only didn't allocate space to store the string,
but also didn't initialize 'enter', this can have any effect up to and
including crashing your program or destroying your hard drive.

Also, scanf( "%s", ... ) only reads a single word at a time; it stops
when it encounters whitespace.  If this is what you want, it's fine. 
Otherwise you will have problems.

> printf("Entered string : %s", enter);
> if (enter == chalcrypt) printf("it worked!");

You can't compare a string and a character, nor can you compare two
strings (it's not clear from your code which you are doing).  If
'chalcrypt' is a character, you must decide which character in 'enter'
to compare it to.  To compare two strings use the strcmp() function.

> For entered string, '(null)' is displayed, and that only.
> What am I doing wrong?

You're lucky it ran at all.  Apparently 'enter' is NULL when it's
allocated from the stack (although it could be anything), and since
you're running under Win95 you didn't get an error when you tried to
read data into that memory location.  If you had been running under raw
DOS with cwsdpmi, your program would have crashed.

> Is there an easier way to read the keyboard?
> I've tried gets() but I've got stuck there too :(

Probably because of the same problem:  you're not allocating memory to
store the string in.  BTW, gets() reads an entire line, not just a
single word, so its behavior may be very different from scanf().

I would send you some sample code, but without a better idea of what
you're trying to do in the first place, it wouldn't be very helpful. 
You should go back to your C references and study the use of strings
more.

-- 
---------------------------------------------------------------------
|      John M. Aldrich       |"A competent and self-confident person|
|       aka Fighteer I       | is incapable of jealousy in anything.|
|   mailto:fighteer AT cs DOT com   | Jealousy is invariably a symptom of  |
| http://www.cs.com/fighteer | neurotic insecurity."  - Lazarus Long|
---------------------------------------------------------------------

- Raw text -


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