Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: replace gets

Reply
Thread Tools

Re: replace gets

 
 
LibraryUser
Guest
Posts: n/a
 
      08-28-2003
amanayin wrote:
>
> I have modified a program that i did not write the modifiaction
> is as follows:
>
> original function
> void getdata(char *n, char *a1, char *a2, char *a3)
> {
> printf("Enter name: ");
> gets(n);
> printf("Enter address 1: ");
> gets(a1);
> printf("Enter address 2: ");
> gets(a2);
> printf("Enter address 3: ");
> gets(a3);
> }

.... snip ...
>
> it works but is there an easier way to do it whole program below


The easiest way IMO is to use ggets (at
<http://cbfalconer.home.att.net/download/ggets.zip> )

and then your function becomes:

void getdata(char *n, char *a1, char *a2, char *a3)
{
printf("Enter name: "); fflush(stdout);
ggets(n);
printf("Enter address 1: "); fflush(stdout);
ggets(a1);
printf("Enter address 2: "); fflush(stdout);
ggets(a2);
printf("Enter address 3: "); fflush(stdout);
ggets(a3);
} /* getdata */

You will have to #include "ggets.h" and link in ggets.o etc. You
should also remember to free the various strings when done with
them, since ggets assigns the memory needed via malloc.

The fflush statements are needed to ensure that the prompt is
output before the input function is executed. This also applies
to your original.

You can also check the return value of ggets. Non-zero means EOF
or error.

--
Replies should be to the newsgroup
cbfalconerX-Mozilla-Status: 0009- Thu Aug 28 09:17:08 2003
X-Mozilla-Status: 0801
X-Mozilla-Status2: 00000000
FCC: /C|/Program Files/Netscape/Users/default/mail/sentnews
X-Mozilla-News-Host: news.msln.net
Message-ID: <(E-Mail Removed)>
Date: Thu, 28 Aug 2003 09:17:07 -0400
From: LibraryUser <(E-Mail Removed)>
Organization: Lincoln Memorial Library
X-Mozilla-Draft-Info: internal/draft; vcard=0; receipt=0;
uuencode=0; html=0; linewidth=0
X-Mailer: Mozilla 4.7 [en]C-NSCPCD (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.lang.c,comp.programming
Followup-To: comp.programming
Subject: Re: Data structure
References: <(E-Mail Removed) >
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Peter wrote:
>
> I am looking for what may be a good data structure to use for
> a given problem.
>
> I have to randomly insert,delete and lookup some objects - all
> three being equally probable. The number of objects is in the
> thousands. The keys are non integers ( but I can probably hash
> them to integers if need be).
>
> To start with I have used a hash based approach. A lot of people
> where I work use splay trees but I don't think that would be a
> good choice in my situation


You are OT for c.l.c, but comp.programming would be suitable. At
any rate, my hashlib library will handle the operations you
describe very nicely. See:

<http://cbfalconer.home.att.net/download/>

FUPs set to comp.programming

--
Replies should be to the newsgroup
Chuck Falconer, on vacation
 
Reply With Quote
 
 
 
 
Simon Biber
Guest
Posts: n/a
 
      08-30-2003
"LibraryUser" <(E-Mail Removed)> wrote:
> The easiest way IMO is to use ggets (at
> <http://cbfalconer.home.att.net/download/ggets.zip> )
>
> and then your function becomes:
>
> void getdata(char *n, char *a1, char *a2, char *a3)
> {
> printf("Enter name: "); fflush(stdout);
> ggets(n);
> printf("Enter address 1: "); fflush(stdout);
> ggets(a1);
> printf("Enter address 2: "); fflush(stdout);
> ggets(a2);
> printf("Enter address 3: "); fflush(stdout);
> ggets(a3);
> } /* getdata */


What? ggets expects a pointer to pointer to char. And it is
not reasonable to just ggets(&n); etc, because how will the
changed value of the variables n, a1, a3, a3 be returned to
the caller of getdata?

Make it:
void getdata(char **n, char **a1, char **a2, char **a3)
{
printf("Enter name: "); fflush(stdout);
ggets(n);
printf("Enter address 1: "); fflush(stdout);
ggets(a1);
printf("Enter address 2: "); fflush(stdout);
ggets(a2);
printf("Enter address 3: "); fflush(stdout);
ggets(a3);
} /* getdata */

But then of course the caller of getdata must be modified
to send the addresses of 'pointer to char' variables,
rather than allocate the arrays itself.

--
Simon.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Return of gets gets John Joyce Ruby 0 04-23-2007 01:38 PM
gets gets John Joyce Ruby 2 03-26-2007 04:00 PM
XMLHttpRequest gets data to display in element but data gets appended not replaces previous! libsfan01 Javascript 5 12-20-2006 06:25 AM
Not only the selected HREF gets surrounded, but the whole row gets surrounded Stefan Mueller HTML 5 07-10-2006 11:53 AM
My Canon SD110 gets stolen, what to replace it with? Tim Digital Photography 4 08-27-2004 06:31 PM



Advertisments