Article View: alt.comp.lang.learn.c-c++
Article #395Re: Default input value in C
From: Jack Klein
Date: Mon, 11 Jan 1999 00:00
Date: Mon, 11 Jan 1999 00:00
100 lines
3696 bytes
3696 bytes
On 10 Jan 1999 21:55:16 GMT, "Dan Akunis" <akunis@glx.chief.co.il>
wrote:
> Here is the C code for your BASIC script:
>
> #include<stdio.h>
> #include<conio.h>
There is no conio.h header in the C programming language. It's OK to
discuss it in the Borland and MS-DOS newsgroups but it is off-topic in
alt.comp.lang.learn.c-c++.
> #include<string.h>
>
> void main()
> {
> char x[50],text[50]="This is default text";
> int x1=0,amount=50;
> printf("\n");
> printf("Enter something if you like! <%s> ",text);
> gets(x);
Never NEVER NEVER use gets(). It is the most dangerous function in
the entire standard C library. Using it is a mark of the
Klewless(tm).
What happens when the user types 51 characters into your 50 character
array? Or 100? Or 200?
Answer: BCD (Burn, Crash, Die).
> if (strlen(x))
If gets() or most other string input functions fail, the contents of
the buffer are undefined, and might not represent a valid string.
This could cause strlen() to crash, particularly on a protected mode
operating system.
> strcpy(text,x);
> printf("Enter a value <%d> ",amount);
> scanf("%d",&x1);
> if (x1 != 0)
> amount=x1;
> printf("\n");
> printf("The value of text$ now = %s\n",text);
> printf("The amount = %d\n",amount);
> getch();
> }
>
> mbt4w <DontlikeSpam@overthere.com> wrote in article
> <36990041.3BADD052@overthere.com>...
> > Hi,
> >
> > I would like to allow defaults to user input in C.
> > I'm using Borland C++ 4.52 on Win98. I'm trying to learn
> > standard C programming or has close as possible even though
> > I'm using a C++ package. It seems the input commands I've
> > tried so far will not allow an 'empty' entry.
> >
> > What I would like to happen is :
> > � Prompt the user for an input while displaying a default entry
> > � If the user presses only the enter key use the default entry
> > � If another value was entered, used it.
> >
> > Below is a basic program that does this.
> > How do I do it in C?
> >
> > text$ = "This is default text"
> > amount = 50
> > PRINT
> > PRINT "Enter something if you like! <"; text$; "> ";
> > INPUT x$
> > IF x$ <> "" THEN text$ = x$
> > PRINT "Enter a value. <"; amount; "> ";
> > INPUT x
> > IF x <> 0 THEN amount = x
> > PRINT
> > PRINT "The value of text$ now = "; text$
> > PRINT "The amount ="; amount
> >
> >
> > Thanks
> >
<Jack>
Dan's basic idea is sound, in that you evaluate the input in a second
variable and only replace the real value if the new one is good.
For a safe replacement for gets() which removes the '\n' at the end of
the string, see http://home.att.net/~jackklein/ctips01.html#safe_gets.
</Jack>
--
Do not email me with questions about programming.
Post them to the appropriate newsgroup.
Followups to my posts are welcome.
Technical Details
Message-ID:
<36a086fc.602273583@netnews.worldnet.att.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<36990041.3BADD052@overthere.com> <01be3ce3$e7d21460$1f3585c2@default>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: f8d38,6ac36e1493d03328 X-Google-Attributes: gidf8d38,public X-Google-Thread: 161995,6ac36e1493d03328 X-Google-Attributes: gid161995,public X-Google-Thread: 114592,6ac36e1493d03328 X-Google-Attributes: gid114592,public From: jackklein@att.net (Jack Klein) Subject: Re: Default input value in C Date: 1999/01/11 Message-ID: <36a086fc.602273583@netnews.worldnet.att.net> X-Deja-AN: 431125203 Content-Transfer-Encoding: 8bit References: <36990041.3BADD052@overthere.com> <01be3ce3$e7d21460$1f3585c2@default> Content-Type: text/plain; charset=ISO-8859-1 Organization: AT&T WorldNet Services Mime-Version: 1.0 Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.language,comp.os.msdos.language