delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/08/28/21:45:19

From: "Kenton W. Mellott" <melken AT co DOT tds DOT net>
Newsgroups: comp.os.msdos.djgpp
References: <HfZ2b.3213$cQ1 DOT 755278 AT kent DOT svc DOT tds DOT net> <hO%2b.16285$8i2 DOT 341 AT newsread2 DOT news DOT atl DOT earthlink DOT net> <tHc3b.3480$cQ1 DOT 829301 AT kent DOT svc DOT tds DOT net> <200308280125 DOT h7S1PFP5020856 AT envy DOT delorie DOT com> <bika4j$nbn$1 AT antares DOT lu DOT erisoft DOT se> <3F4E8FD4 DOT AD6096D8 AT phekda DOT freeserve DOT co DOT uk>
Subject: Re: Simple program. Strange results.
Lines: 94
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <9by3b.3625$cQ1.943860@kent.svc.tds.net>
Date: Fri, 29 Aug 2003 01:41:25 GMT
NNTP-Posting-Host: 69.21.9.75
X-Complaints-To: abuse AT tds DOT net (TDS.NET Help Desk 1-888-815-5992)
X-Trace: kent.svc.tds.net 1062121285 69.21.9.75 (Thu, 28 Aug 2003 20:41:25 CDT)
NNTP-Posting-Date: Thu, 28 Aug 2003 20:41:25 CDT
Organization: TDS.NET Internet Services www.tds.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"Richard Dawe" <rich AT phekda DOT freeserve DOT co DOT uk> wrote in message
news:3F4E8FD4 DOT AD6096D8 AT phekda DOT freeserve DOT co DOT uk...
> Hello.
>
>
> & is used to indicate that a variable is passed by reference in
> declarations/definitions in C++. E.g.:
>
> ---Start test.cpp---
> #include <cstdio>
> #include <cstdlib>
>
> using namespace std;
>
> void
> blargh (int& ref)
> {
>    ref = 12;
> }
>
> int
> main (void)
> {
>    int a = 5;
>
>    blargh(a);
>    printf("%d\n", a);
>    return EXIT_SUCCESS;
> }
> ---End test.cpp---
>
> No idea if that will compile. But if it does, it should print 12 when it
runs.
> blargh modifies the int passed by reference.
>
> I like C++'s Standard Template Library (STL) - type-safe generic container
> classes. Saves you having to do generic list classes with macros in C,
which
> is just horrible. (I strongly dislike C macros most of the time.)
>
> Bye, Rich =]
>
> -- 
> Richard Dawe [ http://www.phekda.freeserve.co.uk/richdawe/ ]



Compiles fine and runs as described.

Perhaps you can help me grasp the trouble I'm having understanding and using
some previously suggested code.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
     char buf[100], *nl;
     float x, y;
     int nchar;
     puts("Please enter a string.");
     fgets(buf, sizeof buf, stdin);
     if ((nl = strchr(buf, '\n')))
->         *nl = 0;
I read this as: if strchr makes  nl  anything but zero then make it equal to
zero.
Why?  What other folloing statement uses it?
Also, why us a pointer to  nl  used, instead of plain  nl.
     printf("you just entered: \"%s\".\n", buf);


     puts("Please enter a floating point number.");
     fgets(buf, sizeof buf, stdin);
     sscanf(buf, "%f", &x);
     printf("you just entered: '%g'.\n", x);


     puts("Please enter 2 floating point numbers and a string.");
     fgets(buf, sizeof buf, stdin);
     if ((nl = strchr(buf, '\n')))
         *nl = 0;
>     sscanf(buf, "%f %f %n", &x, &y, &nchar);
The program errors somewhere in here if I enter to few values.
Is there a way to capture the error and warn and let the user retry?

     printf("you just entered: '%g','%g', \"%s\".\n", x, y, buf + nchar);
     return 0;
}




- Raw text -


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