TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: TOM TORFS
from: DAVID VAN HOOSE
date: 1998-04-01 15:19:00
subject: reading input from stdin (was: I`m an id15:19:0004/01/98

DVH> char name[40];
DVH> gets(name);
DVH> name[40]='\0';
TT> That still leaves the problem that you're writing to memory that
TT> isn't yours. Your array is only 40 characters long and you try to
TT> access the 41st element!
Actually it is correct.  I don't know where you see anything wrong, but
I have been making programs with that method for a while.
Here is how I thought of it:
cin.get(var, array); automatically throws a '\0' NULL character. ASCII 0
to the end of the array after it receives input, so I though that if
gets(); didn't, that I would.  So I stuck a piece of code to the end
which puts the null character where it is supposed to. Remember, a NULL
character when outputing a string signifies the end of the string.
Anything afterwards is ignored.  I have tried this method with BCOS2,
MS-VC++, and the GNU compilers. Works great. \0 is one character by
the way, just like \n and \t.  They are macros.
PEACE!
-Dave
1:3612/333
333:1/1
davidvh@ibm.net
--- PCBoard (R) v15.3/100
---------------
* Origin: Destiny BBS: LightBar Central (1:3612/333)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.