delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/05/26/16:15:05

From: Jack Klein <jackklein AT spamcop DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Problem w/Scanf
Message-ID: <v730ht4g2e1huqbejfpf1fdt5sp0dfrbqr@4ax.com>
References: <fREP6.6639$xM2 DOT 2800101 AT typhoon2 DOT ba-dsg DOT net>
X-Newsreader: Forte Agent 1.8/32.548
MIME-Version: 1.0
Lines: 82
Date: Sat, 26 May 2001 20:11:27 GMT
NNTP-Posting-Host: 12.84.8.21
X-Complaints-To: abuse AT worldnet DOT att DOT net
X-Trace: bgtnsc04-news.ops.worldnet.att.net 990907887 12.84.8.21 (Sat, 26 May 2001 20:11:27 GMT)
NNTP-Posting-Date: Sat, 26 May 2001 20:11:27 GMT
Organization: AT&T Worldnet
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Sat, 26 May 2001 02:49:47 GMT, "W. Gwinn" <Lithiemgod AT hotmail DOT com>
wrote in comp.os.msdos.djgpp:

> When i compile and run this code i have the following problem:
> 
> the printprop line at the end of editprop function will show what is typed
> at the scanf("%s%,prop.name); line.
> All the other lines though, will not change. the data in the ints stays the
> same as before the function runs.
> if, however, one does
> prop.purccost=**any number not purccost** , then it will show in the
> printprop, so i know that the data is passing prperly to the print function.
> it behaves the same whether i use %i %hi %hd or %d in the scanf line
> 
> Any help would be greatly appreiciated.
> Thank you
> 
> struct propertydata
>         {
>                 char name[30];
>                  int purccost,houscost,group,improvement,recnum,ownedby;
>                 bool mortgaged;
>                  int rent[6];
>         }
> void printprop(propertydata prop)
> {
> printf("\n\nName of Property:\t\t\t%s\nRent (no house): \t\t\t%i\nRent (1
> house): \t\t\t%i\nRent (2 houses): \t\t\t%i\nRent (3 houses): \t\t\t%i\nRent
> (4 houses): \t\t\t%i\nRent (Hotel): \t\t\t\t%i\nCost  of  house:
> \t\t\t%i\nCost of
> property:\t\t\t%i\n",prop.name,prop.rent[0],prop.rent[1],prop.rent[2],prop.r
> ent[3],prop.rent[4],prop.rent[5],prop.houscost,prop.purccost);
> };
> void editprop(propertydata &prop)
> {
> printf("\n\t\tRecord #%i.",prop.recnum);
> printf("\nName:     \t %s\t",prop.name);
>     scanf("%s",prop.name);
> printf("\nCost      \t%i\t",prop.purccost);
>      scanf("%d",prop.purccost);
> printf("\nHouse Cost\t%i\t",prop.houscost);
>     scanf("%d",prop.houscost);
> printf("\nColor Group\t%i\t",prop.group);
>     scanf("%d",prop.group);
> printf("\nRents:\nNo Houses:\t%i\t",prop.rent[0]);
>     scanf("%d",prop.rent[0]);
> printf("\n1 Houses:\t%i\t",prop.rent[1]);
>     scanf("%d",prop.rent[1]);
> printf("\n2 Houses:\t%i\t",prop.rent[2]);
>     scanf("%d",prop.rent[2]);
> printf("\n3 Houses:\t%i\t",prop.rent[3]);
>     scanf("%d",prop.rent[3]);
> printf("\n4 Houses:\t%i\t",prop.rent[4]);
>     scanf("%d",prop.rent[4]);
> printf("\nHotel   :\t%i\t",prop.rent[5]);
>     scanf("%d",prop.rent[5]);
> printprop(prop);
> };

How do you know that any of your scanf() calls succeed?  There is a
reason that scanf() returns an int value, why don't you use it?

Note also that scanf("%s") without a length specifier is an outright
bug, in the same category as gets(), because if the input is longer
than the buffer there will be a memory overwrite and undefined
behavior.

I would suggest that you take a look at the FAQ for comp.lang.c, link
in my signature, and learn why scanf() is such an extremely poor and
difficult function to use for interactive user input.

If you must use scanf(), you _absolutely must_ add maximum length
parameters to all "%s" conversions, and check each and every call for
errors.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

- Raw text -


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